gerade angezeigte Bausteine (verschachtelt) */ private array $stack = []; /** * Daten für den Block „Baustein“ – null blendet ihn aus. * * @param array $data * @return array|null */ public function resolve(array $data, ?User $user, array $context): ?array { $id = (int) ($data['snippet'] ?? 0); $snippet = $id ? ContentSnippet::query()->where('kind', ContentSnippet::BLOCK)->find($id) : null; if (! $snippet || in_array($id, $this->stack, true) || count($this->stack) >= 3) { // Im Editor einen Hinweis zeigen, auf der Seite nichts return ($context['editing'] ?? false) ? ['missing' => true] : null; } $this->stack[] = $id; try { $blocks = app(Content::class)->render($snippet->content, $user, [...$context, 'snippet_id' => $id])['blocks']; } finally { array_pop($this->stack); } return [ 'name' => $snippet->name, 'blocks' => $blocks, 'edit_url' => $user?->can('pages.manage') ? route('snippets.edit', $snippet) : null, ]; } /** * Seiten, die einen Baustein einbinden (veröffentlicht oder im Entwurf). * * @return array> Baustein => Seiten */ public function usages(): array { $content = app(Content::class); $usages = []; Page::query()->get(['id', 'title', 'content', 'draft'])->each(function (Page $page) use ($content, &$usages) { $ids = []; foreach ([$page->content, $page->draft] as $tree) { array_push($ids, ...$content->collect($tree, fn (array $block) => ($block['type'] ?? null) === self::BLOCK_TYPE ? [(int) ($block['data']['snippet'] ?? 0)] : [])); } foreach (array_unique(array_filter($ids)) as $id) { $usages[$id][] = ['id' => $page->id, 'title' => $page->title]; } }); return $usages; } }