". Register new sections in config/themes.php. */ abstract class Section { /** How photos follow the seasons – shown below every photo choice. */ public const SEASON_HINT = 'Fotos mit Saison (Webseite › Fotos › „Zeigen in“) erscheinen nur in ihrem Erscheinungsbild, alle anderen ganzjährig.'; 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 []; } /** * 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)))); } /** * Chosen photos in the editor's order: published and fitting the season in effect. * * @param list|null $ids * @return Collection */ protected function chosenPhotos(Theme $theme, ?array $ids): Collection { return $this->photos($ids)->filter(fn (Photo $photo): bool => $photo->fitsSeason($theme->appearance))->values(); } /** * Photos picked automatically for the section's apartment and the season (Photo::automatic). * * @return Collection */ protected function automaticPhotos(Theme $theme, ?PhotoCategory $category = null, ?int $limit = null): Collection { $photos = Photo::automatic($theme->property(), $theme->appearance, $category); return $limit !== null ? $photos->take($limit)->values() : $photos; } /** * Photos of a section that offers "automatisch" or "eigene Auswahl" (fields from photoSourceFields()). * * @param array $data * @return Collection */ protected function sourcedPhotos(Theme $theme, array $data, ?PhotoCategory $category = null, ?int $limit = null): Collection { if (($data['photo_source'] ?? 'auto') === 'selection') { $photos = $this->chosenPhotos($theme, $data['photo_ids'] ?? []); return $limit !== null ? $photos->take($limit)->values() : $photos; } return $this->automaticPhotos($theme, $category, $limit); } /** * "Automatisch" or "Eigene Auswahl" plus the selection field, shown only for a selection. * * @return list */ protected static function photoSourceFields(string $automaticLabel): array { return [ Radio::make('photo_source') ->label('Bilder') ->options(['auto' => $automaticLabel, 'selection' => 'Eigene Auswahl']) ->default('auto') ->inline() ->live(), static::photoSelect('photo_ids', 'Ausgewählte Bilder', multiple: true) ->helperText(self::SEASON_HINT) ->visible(fn (Get $get): bool => $get('photo_source') === 'selection'), ]; } 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(); } }