record = AppearanceModel::firstOrNew([ 'company_id' => auth()->user()->currentCompany->id, ]); abort_unless(static::canView($this->record), 404); $this->fillForm(); } public function fillForm(): void { $data = $this->record->attributesToArray(); $this->form->fill($data); } public function save(): void { try { $data = $this->form->getState(); $this->handleRecordUpdate($this->record, $data); } catch (Halt $exception) { return; } $this->getSavedNotification()->send(); } protected function getSavedNotification(): Notification { return Notification::make() ->success() ->title(__('filament-panels::resources/pages/edit-record.notifications.saved.title')); } public function form(Form $form): Form { return $form ->schema([ $this->getGeneralSection(), $this->getDataPresentationSection(), ]) ->model($this->record) ->statePath('data') ->operation('edit'); } protected function getGeneralSection(): Component { return Section::make('General') ->schema([ Select::make('primary_color') ->allowHtml() ->softRequired() ->localizeLabel() ->options( collect(PrimaryColor::cases()) ->sort(static fn ($a, $b) => $a->value <=> $b->value) ->mapWithKeys(static fn ($case) => [ $case->value => " " . $case->getLabel() . ' ', ]), ), Select::make('font') ->allowHtml() ->softRequired() ->localizeLabel() ->options( collect(Font::cases()) ->mapWithKeys(static fn ($case) => [ $case->value => "{$case->getLabel()}", ]), ), ])->columns(); } protected function getDataPresentationSection(): Component { return Section::make('Data Presentation') ->schema([ Select::make('table_sort_direction') ->softRequired() ->localizeLabel() ->options(TableSortDirection::class), Select::make('records_per_page') ->softRequired() ->localizeLabel() ->options(RecordsPerPage::class), ])->columns(); } protected function handleRecordUpdate(AppearanceModel $record, array $data): AppearanceModel { $record->fill($data); $keysToWatch = [ 'primary_color', 'font', ]; $cachedKeysToWatch = [ 'primary_color', 'font', 'table_sort_direction', 'records_per_page', ]; if ($record->isDirty($keysToWatch)) { $this->dispatch('appearanceUpdated'); } if ($record->isDirty($cachedKeysToWatch)) { CompanySettingsService::invalidateSettings($record->company_id); } $record->save(); return $record; } /** * @return array */ protected function getFormActions(): array { return [ $this->getSaveFormAction(), ]; } protected function getSaveFormAction(): Action { return Action::make('save') ->label(__('filament-panels::resources/pages/edit-record.form.actions.save.label')) ->submit('save') ->keyBindings(['mod+s']); } public static function canView(Model $record): bool { try { return authorize('update', $record)->allowed(); } catch (AuthorizationException $exception) { return $exception->toResponse()->allowed(); } } }