> */ public function all(): array { return collect(config('themes.sections')) ->mapWithKeys(fn (string $class): array => [$class::key() => $class]) ->all(); } public function get(string $key): ?Section { $class = $this->all()[$key] ?? null; return $class ? app($class) : null; } /** * @return list */ public function blocks(): array { return array_values(array_map( fn (string $class): Block => Block::make($class::key()) ->label(fn (?array $state): string => $this->blockLabel($class, $state)) ->icon($class::icon()) ->schema([ Hidden::make('id'), ...$class::schema(), Select::make('appearances') ->label('Nur anzeigen im Erscheinungsbild') ->helperText('Leer = immer anzeigen. Z. B. ein Weihnachts-Baustein nur im Erscheinungsbild „Weihnachten“.') ->options(fn (): array => Appearance::query()->orderByDesc('is_default')->orderBy('name')->pluck('name', 'id')->all()) ->multiple(), ]), $this->all(), )); } /** * Whether a placed section is shown with the active appearance. * * @param array $data */ public function isVisible(Theme $theme, array $data): bool { $appearances = array_map('intval', array_filter((array) ($data['appearances'] ?? []))); return $appearances === [] || ($theme->appearance !== null && in_array($theme->appearance->id, $appearances, true)); } /** * Renders the stored sections of a page; unknown or failing types are skipped (and reported), * so a page never breaks because of one section. * * @param list}>|null $sections * @return list */ public function render(Theme $theme, ?array $sections): array { $views = []; foreach ($sections ?? [] as $index => $stored) { $section = $this->get($stored['type'] ?? ''); if ($section === null || ! ViewFactory::exists($section->view()) || ! $this->isVisible($theme, $stored['data'] ?? [])) { continue; } $views[] = ViewFactory::make($section->view(), [ ...$section->data($theme, $stored['data'] ?? []), 'theme' => $theme, 'sectionId' => $section::key().'-'.$index, ]); } return $views; } /** * "Text mit Bild · Ihr Zuhause an der Ostsee · nur: Winter" for the collapsed builder item. * * @param class-string
$class * @param array|null $state */ private function blockLabel(string $class, ?array $state): string { if ($state === null) { return $class::label(); } $appearances = array_filter((array) ($state['appearances'] ?? [])); $only = $appearances === [] ? null : 'nur: '.Appearance::query()->whereKey($appearances)->pluck('name')->implode(', '); return collect([$class::label(), $class::summary($state), $only])->filter()->implode(' · '); } }