Loheide-eu-mitarbeiterplatt.../plugins-core/pages/src/Snippets.php
Friederich Loheide 2166a7e11a
Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 5m0s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 5m0s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 4m50s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 4m33s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 6m11s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 6m46s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 6m24s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 5m33s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m17s
Seiten: Bausteine und Vorlagen (0.22.0)
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-27 07:48:02 +00:00

75 lines
2.4 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace Inhaus\Plugins\Pages;
use App\Models\User;
use App\Pagebuilder\Content;
use Inhaus\Plugins\Pages\Models\ContentSnippet;
use Inhaus\Plugins\Pages\Models\Page;
/**
* Bausteine im Pagebuilder: Anzeige (mit Schutz vor Endlosschleifen) und Verwendungsnachweis.
*/
class Snippets
{
public const BLOCK_TYPE = 'pages.snippet';
/** @var list<int> gerade angezeigte Bausteine (verschachtelt) */
private array $stack = [];
/**
* Daten für den Block „Baustein“ – null blendet ihn aus.
*
* @param array<string, mixed> $data
* @return array<string, mixed>|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<int, list<array{id: int, title: string}>> 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;
}
}