". Register new sections in config/themes.php. */ abstract class Section { /** One photo (field holds a photo id). */ public const SLOT_SINGLE = 'single'; /** Several chosen photos (field holds a list of ids). */ public const SLOT_MULTIPLE = 'multiple'; /** Photos picked automatically (e.g. by category); an appearance may replace or precede them. */ public const SLOT_COLLECTION = 'collection'; abstract public static function key(): string; abstract public static function label(): string; /** * Heroicon name shown in the page builder. */ public static function icon(): string { return 'heroicon-o-square-3-stack-3d'; } /** * Admin fields of the section (builder block schema). Field names are keys of $data in data(). * * @return list */ public static function schema(): array { return []; } /** * Photo places an appearance can fill with seasonal photos ("Bilder je Baustein"). * * @return array */ public static function photoSlots(): array { return []; } /** * Short text identifying a placed section in admin lists, e.g. its heading. * * @param array $data */ public static function summary(array $data): ?string { $text = $data['heading'] ?? $data['title'] ?? null; return filled($text) ? str(strip_tags((string) $text))->replace('*', '')->limit(50)->toString() : null; } /** * Variables for the view. * * @param array $data what the admin entered * @return array */ abstract public function data(Theme $theme, array $data): array; public function view(): string { return 'theme::sections.'.static::key(); } /** * Sanitised HTML of a rich text field. */ protected function html(?string $value): ?HtmlString { return filled($value) ? new HtmlString(RichContentRenderer::make($value)->toHtml()) : null; } /** * @param list|null $ids * @return Collection */ protected function photos(?array $ids): Collection { if (blank($ids)) { return new Collection; } $photos = Photo::query()->published()->whereKey($ids)->with('media')->get()->keyBy('id'); return new Collection(array_values(array_filter(array_map(fn ($id) => $photos->get((int) $id), $ids)))); } /** * The seasonal photo the active appearance sets for a single slot of this placed section. * * @param array $data */ protected function seasonalPhoto(Theme $theme, array $data, string $slot): ?Photo { return $this->seasonalPhotos($theme, $data, $slot)?->first(); } /** * The seasonal photos the active appearance sets for a slot of this placed section, null if none. * * @param array $data * @return Collection|null */ protected function seasonalPhotos(Theme $theme, array $data, string $slot): ?Collection { $ids = $theme->sectionPhotoIds($data['id'] ?? null, $slot); if ($ids === []) { return null; } $photos = $this->photos($ids); return $photos->isEmpty() ? null : $photos; } /** * Photos of a collection slot: the automatic photos, replaced or preceded by the seasonal ones. * * @param array $data * @param Collection $automatic * @return Collection */ protected function withSeasonalPhotos(Theme $theme, array $data, string $slot, Collection $automatic): Collection { $seasonal = $this->seasonalPhotos($theme, $data, $slot); if ($seasonal === null) { return $automatic; } if ($theme->sectionPhotoMode($data['id'] ?? null) === 'prepend') { return $seasonal->merge($automatic)->unique('id')->values(); } return $seasonal; } protected function pageUrl(?string $slug): ?string { if (blank($slug)) { return null; } return $slug === Page::HOME ? route('home') : route('page', ['slug' => $slug]); } /** * Select field listing the website photos with thumbnails. */ protected static function photoSelect(string $name, string $label, bool $multiple = false): Select { return PhotoSelect::make($name) ->label($label) ->multiple($multiple); } /** * Select field listing the pages of the site, for links. */ protected static function pageSelect(string $name, string $label): Select { return Select::make($name) ->label($label) ->options(fn (): array => Page::query()->orderBy('sort_order')->pluck('title', 'slug')->all()) ->searchable(); } }