Startseite frei gestalten, Neuigkeiten-Block mit Kategorien-Filter (0.26.0)
Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 5m21s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 4m55s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 4m40s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 4m42s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 7m25s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 7m57s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 6m16s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 5m34s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m14s

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Friederich Loheide 2026-09-27 09:38:18 +00:00
parent 6290e09074
commit 2c96fcce1c
151 changed files with 1515 additions and 317 deletions

View file

@ -0,0 +1,68 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
/**
* Block „Neuigkeiten“: aus der einzelnen Kategorie („category“) wird der neue Filter
* („filter“ = include, „categories“ = [slug]) – in Seiten, Bausteinen und Beiträgen.
*/
return new class extends Migration
{
private const COLUMNS = [
'pages' => ['content', 'draft', 'scheduled_content'],
'content_snippets' => ['content'],
'news_posts' => ['content'],
];
public function up(): void
{
foreach (self::COLUMNS as $table => $columns) {
if (! Schema::hasTable($table)) {
continue;
}
DB::table($table)->orderBy('id')->each(function ($row) use ($table, $columns) {
$changes = [];
foreach ($columns as $column) {
$content = json_decode((string) ($row->{$column} ?? ''), true);
if (is_array($content) && $this->convert($content)) {
$changes[$column] = json_encode($content, JSON_UNESCAPED_UNICODE);
}
}
if ($changes) {
DB::table($table)->where('id', $row->id)->update($changes);
}
});
}
}
/**
* @param array<mixed> $node
*/
private function convert(array &$node): bool
{
$changed = false;
if (($node['type'] ?? null) === 'news.latest' && is_array($node['data'] ?? null) && array_key_exists('category', $node['data'])) {
$slug = (string) $node['data']['category'];
unset($node['data']['category']);
$node['data'] += $slug !== ''
? ['filter' => 'include', 'categories' => [$slug]]
: ['filter' => 'all', 'categories' => []];
$changed = true;
}
foreach ($node as &$child) {
if (is_array($child) && $this->convert($child)) {
$changed = true;
}
}
return $changed;
}
};

View file

@ -1,26 +1,75 @@
<script setup>
import { computed } from 'vue';
import { Link } from '@inertiajs/vue3';
import { ChevronRight } from '@lucide/vue';
import { ArrowRight, ChevronRight } from '@lucide/vue';
import CategoryLabel from '../components/CategoryLabel.vue';
import NewsCard from '../components/NewsCard.vue';
import NewsImage from '../components/NewsImage.vue';
import NewsListItem from '../components/NewsListItem.vue';
import { formatShortDate } from '@/lib/format';
defineProps({ block: { type: Object, required: true } });
/**
* Block „Neuigkeiten“: Kacheln, Liste, große Vorschau (untereinander, in breiten
* Bereichen nebeneinander) oder Aufmacher mit Liste. Link „Alle Beiträge“ führt
* zur passend gefilterten Übersicht.
*/
const props = defineProps({ block: { type: Object, required: true } });
const posts = computed(() => props.block.props?.posts ?? []);
const layout = computed(() => props.block.data.layout ?? 'cards');
const boxed = computed(() => ['large', 'featured'].includes(layout.value));
const moreLabel = computed(() => props.block.data.more_label ?? 'Alle anzeigen');
</script>
<template>
<section>
<div v-if="block.data.title" class="mb-3 flex items-center justify-between gap-4">
<h3 class="text-lg font-semibold">{{ block.data.title }}</h3>
<Link v-if="block.props?.more_url" :href="block.props.more_url" class="inline-flex items-center gap-0.5 text-[13px] font-medium text-brand-text hover:underline">
Alle anzeigen <ChevronRight class="size-3.5" />
<section :class="boxed ? '@container rounded-lg border bg-card p-5' : ''">
<div v-if="block.data.title || (!boxed && moreLabel)" class="mb-4 flex items-center justify-between gap-4">
<h3 v-if="block.data.title" class="text-lg font-semibold">{{ block.data.title }}</h3>
<Link v-if="!boxed && moreLabel && block.props?.more_url" :href="block.props.more_url" class="inline-flex shrink-0 items-center gap-0.5 text-[13px] font-medium text-brand-text hover:underline">
{{ moreLabel }} <ChevronRight class="size-3.5" />
</Link>
</div>
<div v-if="block.data.layout === 'list'" class="divide-y rounded-lg border bg-card px-4">
<NewsListItem v-for="post in block.props?.posts ?? []" :key="post.id" :post="post" />
<!-- Große Vorschau -->
<div v-if="layout === 'large'" class="grid gap-6 @3xl:grid-cols-3">
<Link v-for="post in posts" :key="post.id" :href="`/neuigkeiten/${post.slug}`" class="group grid content-start gap-3">
<NewsImage :src="post.cover_url ?? post.thumb_url" :alt="post.title" class="rounded-md" />
<div class="min-w-0">
<CategoryLabel :category="post.category" />
<h4 class="mt-1 line-clamp-2 text-base leading-snug font-semibold group-hover:underline">{{ post.title }}</h4>
<p v-if="post.teaser" class="mt-1 line-clamp-2 text-[13px] text-muted-foreground">{{ post.teaser }}</p>
<p class="mt-1.5 text-xs text-muted-foreground">{{ formatShortDate(post.published_at) }}</p>
</div>
</Link>
</div>
<!-- Aufmacher und Liste -->
<div v-else-if="layout === 'featured'" class="grid gap-4">
<Link v-if="posts[0]" :href="`/neuigkeiten/${posts[0].slug}`" class="group grid content-start gap-3">
<NewsImage :src="posts[0].cover_url ?? posts[0].thumb_url" :alt="posts[0].title" class="rounded-md" />
<div>
<CategoryLabel :category="posts[0].category" />
<h4 class="mt-1 line-clamp-2 text-lg leading-snug font-semibold group-hover:underline">{{ posts[0].title }}</h4>
<p v-if="posts[0].teaser" class="mt-1 line-clamp-3 text-sm text-muted-foreground">{{ posts[0].teaser }}</p>
</div>
</Link>
<div v-if="posts.length > 1" class="divide-y border-t">
<NewsListItem v-for="post in posts.slice(1)" :key="post.id" :post="post" />
</div>
</div>
<div v-else-if="layout === 'list'" class="divide-y rounded-lg border bg-card px-4">
<NewsListItem v-for="post in posts" :key="post.id" :post="post" />
</div>
<div v-else class="grid gap-5 @xl:grid-cols-2 @4xl:grid-cols-3">
<NewsCard v-for="post in block.props?.posts ?? []" :key="post.id" :post="post" />
<NewsCard v-for="post in posts" :key="post.id" :post="post" />
</div>
<p v-if="!(block.props?.posts ?? []).length" class="text-sm text-muted-foreground">Keine Beiträge.</p>
<p v-if="!posts.length" class="text-sm text-muted-foreground">Keine Beiträge.</p>
<Link v-if="boxed && moreLabel && block.props?.more_url" :href="block.props.more_url" class="mt-5 inline-flex items-center gap-1.5 text-sm font-medium text-brand-text hover:underline">
{{ moreLabel }} <ArrowRight class="size-4" />
</Link>
</section>
</template>

View file

@ -11,6 +11,7 @@ defineProps({
posts: { type: Object, required: true },
categories: { type: Array, required: true },
activeCategory: { type: String, default: null },
filter: { type: String, default: null },
canWrite: { type: Boolean, default: false },
});
</script>
@ -27,7 +28,7 @@ defineProps({
<Link
href="/neuigkeiten"
class="rounded-pill border px-3.5 py-1.5 text-[13px] font-medium transition-colors"
:class="!activeCategory ? 'border-brand bg-brand text-brand-foreground' : 'bg-card hover:bg-accent'"
:class="!activeCategory && !filter ? 'border-brand bg-brand text-brand-foreground' : 'bg-card hover:bg-accent'"
preserve-scroll
>
Alle
@ -44,6 +45,11 @@ defineProps({
</Link>
</nav>
<p v-if="filter" class="-mt-3 mb-5 flex flex-wrap items-center gap-2 text-[13px] text-muted-foreground">
<span class="rounded-pill border border-brand bg-brand-soft px-3 py-1 font-medium text-brand-soft-foreground">{{ filter }}</span>
<Link href="/neuigkeiten" class="hover:text-foreground hover:underline">Filter entfernen</Link>
</p>
<div v-if="posts.data.length" class="grid gap-5 sm:grid-cols-2 lg:grid-cols-3 2xl:grid-cols-4">
<NewsCard v-for="post in posts.data" :key="post.id" :post="post" />
</div>

View file

@ -21,11 +21,18 @@ class NewsController extends Controller
$categories = NewsCategory::query()->visibleTo($request->user())->orderBy('sort')->orderBy('name')->get(['id', 'name', 'slug']);
$category = $categories->firstWhere('slug', $request->query('kategorie'));
// Mehrere Kategorien bzw. alle außer bestimmten (Link „Alle Beiträge“ der Startseite)
$list = fn (string $param) => $categories->whereIn('slug', array_filter(explode(',', (string) $request->query($param, ''))))->values();
$only = $category ? collect() : $list('kategorien');
$without = $category ? collect() : $list('ohne');
$posts = NewsPost::query()
->published()
->visibleTo($request->user())
->with('category')
->when($category, fn ($query) => $query->where('category_id', $category->id))
->when($only->isNotEmpty(), fn ($query) => $query->whereIn('category_id', $only->pluck('id')))
->when($without->isNotEmpty(), fn ($query) => $query->where(fn ($q) => $q->whereNull('category_id')->orWhereNotIn('category_id', $without->pluck('id'))))
->latest('published_at')
->paginate(12)
->withQueryString();
@ -34,6 +41,11 @@ class NewsController extends Controller
'posts' => NewsPostResource::collection($posts),
'categories' => $categories->map(fn (NewsCategory $c) => ['name' => $c->name, 'slug' => $c->slug]),
'activeCategory' => $category?->slug,
'filter' => match (true) {
$only->isNotEmpty() => 'Nur: '.$only->pluck('name')->implode(', '),
$without->isNotEmpty() => 'Ohne: '.$without->pluck('name')->implode(', '),
default => null,
},
'canWrite' => $request->user()->can('create', NewsPost::class),
]);
}

View file

@ -80,21 +80,33 @@ class NewsPlugin extends Plugin
);
$this->app->make(Blocks::class)->add(BlockType::make('news.latest', 'Neuigkeiten', 'newspaper', 'dynamic', 'news::LatestNews')
->description('Die neuesten Beiträge – optional nur aus einer Kategorie.')
->description('Die neuesten Beiträge – aus allen, ausgewählten oder allen außer bestimmten Kategorien.')
->fields([
Field::text('title', 'Überschrift', 150, 'Neuigkeiten'),
Field::select('category', 'Kategorie', fn () => ['' => 'Alle Kategorien'] + NewsCategory::query()->orderBy('sort')->pluck('name', 'slug')->all(), ''),
Field::select('filter', 'Kategorien', ['all' => 'Alle Kategorien', 'include' => 'Nur die ausgewählten', 'exclude' => 'Alle außer den ausgewählten'], 'all'),
Field::multiselect('categories', 'Auswahl', fn () => NewsCategory::query()->orderBy('sort')->orderBy('name')->pluck('name', 'slug')->all())
->help('Gilt bei „Nur die ausgewählten“ bzw. „Alle außer den ausgewählten“.'),
Field::number('count', 'Anzahl', 1, 12, 3),
Field::select('layout', 'Darstellung', ['cards' => 'Kacheln', 'list' => 'Liste'], 'cards'),
Field::select('layout', 'Darstellung', ['large' => 'Große Vorschau (untereinander)', 'featured' => 'Aufmacher und Liste', 'cards' => 'Kacheln nebeneinander', 'list' => 'Liste'], 'cards'),
Field::text('more_label', 'Link unten (leer = kein Link)', 60, 'Alle Beiträge'),
])
->resolveUsing(function (array $data, ?User $user) {
$slugs = $data['filter'] === 'all' ? [] : $data['categories'];
$posts = NewsPost::query()->published()->visibleTo($user)->with('category')
->when($data['category'], fn ($q, $slug) => $q->whereHas('category', fn ($c) => $c->where('slug', $slug)))
->when($slugs !== [] && $data['filter'] === 'include', fn ($q) => $q->whereHas('category', fn ($c) => $c->whereIn('slug', $slugs)))
->when($slugs !== [] && $data['filter'] === 'exclude', fn ($q) => $q->where(fn ($q) => $q->whereNull('category_id')->orWhereHas('category', fn ($c) => $c->whereNotIn('slug', $slugs))))
->latest('published_at')->limit($data['count'])->get();
$query = match (true) {
$slugs === [] => '',
$data['filter'] === 'include' && count($slugs) === 1 => '?kategorie='.rawurlencode($slugs[0]),
$data['filter'] === 'include' => '?kategorien='.rawurlencode(implode(',', $slugs)),
default => '?ohne='.rawurlencode(implode(',', $slugs)),
};
return [
'posts' => NewsPostResource::collection($posts)->resolve(),
'more_url' => '/neuigkeiten'.($data['category'] ? '?kategorie='.$data['category'] : ''),
'more_url' => '/neuigkeiten'.$query,
];
}));
}