*/ public array $data = []; public static function canAccess(): bool { return (bool) auth()->user()?->hasRole(UserRole::Admin, UserRole::Content); } public function mount(): void { $values = []; foreach (Content::sections() as $section) { foreach (array_keys($section['fields']) as $key) { $values[self::fieldName($key)] = Content::get($key); } } $this->form->fill($values); } public function form(Schema $schema): Schema { return $schema ->statePath('data') ->components([ Tabs::make('sections') ->persistTabInQueryString() ->tabs(collect(Content::sections()) ->map(fn (array $section): Tab => Tab::make($section['label'])->schema( collect($section['fields'])->map(fn (array $field, string $key) => $this->field($key, $field))->values()->all(), )) ->values() ->all()), ]); } public function content(Schema $schema): Schema { return $schema->components([ Form::make([EmbeddedSchema::make('form')]) ->id('form') ->livewireSubmitHandler('save') ->footer([ Actions::make([ Action::make('save')->label('Speichern')->submit('save')->keyBindings(['mod+s']), ])->sticky(), ]), ]); } public function save(): void { $state = $this->form->getState(); $values = []; foreach (Content::sections() as $section) { foreach (array_keys($section['fields']) as $key) { $value = $state[self::fieldName($key)] ?? null; $values[$key] = $value === null ? null : (string) $value; } } Content::save($values); Notification::make()->title('Texte gespeichert')->success()->send(); } /** * @param array{label: string, type: string, default?: ?string} $field */ private function field(string $key, array $field): TextInput|Textarea|RichEditor|Select { $name = self::fieldName($key); return match ($field['type']) { 'textarea' => Textarea::make($name)->label($field['label'])->rows(3), 'richtext' => RichEditor::make($name) ->label($field['label']) ->toolbarButtons([['bold', 'italic', 'link'], ['h2', 'h3'], ['bulletList', 'orderedList'], ['undo', 'redo']]), 'photo' => Select::make($name) ->label($field['label']) ->options(fn (): array => Photo::query()->orderBy('category')->orderBy('sort_order')->pluck('alt_text', 'id')->all()) ->searchable() ->placeholder('Kein Bild gewählt') ->helperText('Bilder verwalten Sie unter „Webseite › Bilder“.'), default => TextInput::make($name)->label($field['label'])->maxLength(255), }; } /** * Dots would be read as nested state, so content keys are flattened for the form. */ private static function fieldName(string $key): string { return str_replace('.', '__', $key); } }