Last active
August 1, 2024 14:49
-
-
Save pxlrbt/15342387355aeae0c1b043ab385be8a8 to your computer and use it in GitHub Desktop.
Filament Template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Filament\PageTemplates; | |
use Filament\Forms\Components\Repeater; | |
use Filament\Forms\Components\RichEditor; | |
use Filament\Forms\Components\TextInput; | |
final class Faq | |
{ | |
public static function title() | |
{ | |
return 'FAQ'; | |
} | |
public static function schema() | |
{ | |
return [ | |
TextInput::make('title'), | |
Repeater::make('faq')->label('FAQ')->schema([ | |
TextInput::make('title'), | |
Repeater::make('items')->schema([ | |
TextInput::make( 'title'), | |
RichEditor::make('content') | |
]) | |
]) | |
]; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Filament\Resources; | |
use App\Filament\Resources\PageResource\Pages; | |
use App\Models\Page; | |
use Filament\Forms; | |
use Filament\Resources\Form; | |
use Filament\Resources\Resource; | |
use Filament\Resources\Table; | |
use Filament\Tables; | |
use Illuminate\Filesystem\Filesystem; | |
use Illuminate\Support\Collection; | |
use Illuminate\Support\Str; | |
use SplFileInfo; | |
class PageResource extends Resource | |
{ | |
protected static ?string $model = Page::class; | |
protected static ?string $navigationIcon = 'heroicon-o-document-text'; | |
protected static ?string $navigationGroup = 'Inhalte'; | |
protected static ?string $label = 'Seite'; | |
protected static ?string $pluralLabel = 'Seiten'; | |
public static function form(Form $form): Form | |
{ | |
return $form | |
->schema([ | |
Forms\Components\Select::make('template') | |
->reactive() | |
->options(static::getTemplates()), | |
...static::getTemplateSchemas(), | |
]); | |
} | |
public static function getTemplates(): Collection | |
{ | |
return static::getTemplateClasses()->mapWithKeys(fn ($class) => [$class => $class::title()]); | |
} | |
public static function getTemplateClasses(): Collection | |
{ | |
$filesystem = app(Filesystem::class); | |
return collect($filesystem->allFiles(app_path('Filament/PageTemplates'))) | |
->map(function (SplFileInfo $file): string { | |
return (string) Str::of('App\\Filament\\PageTemplates') | |
->append('\\', $file->getRelativePathname()) | |
->replace(['/', '.php'], ['\\', '']); | |
}); | |
} | |
public static function getTemplateSchemas(): array | |
{ | |
return static::getTemplateClasses() | |
->map(fn ($class) => | |
Forms\Components\Group::make($class::schema()) | |
->columnSpan(2) | |
->afterStateHydrated(fn ($component, $state) => $component->getChildComponentContainer()->fill($state)) | |
->statePath('temp_content.' . static::getTemplateName($class)) | |
->visible(fn ($get) => $get('template') === $class) | |
) | |
->toArray(); | |
} | |
public static function getTemplateName($class) | |
{ | |
return Str::of($class)->afterLast('\\')->snake()->toString(); | |
} | |
public static function table(Table $table): Table | |
{ | |
return $table | |
->columns([ | |
Tables\Columns\TextColumn::make('slug')->sortable(), | |
Tables\Columns\TextColumn::make('title')->sortable(), | |
Tables\Columns\TextColumn::make('updated_at')->dateTime('d.m.Y, H:i')->sortable(), | |
]) | |
->filters([ | |
// | |
]); | |
} | |
public static function getRelations(): array | |
{ | |
return [ | |
// | |
]; | |
} | |
public static function getPages(): array | |
{ | |
return [ | |
'index' => Pages\ListPages::route('/'), | |
'create' => Pages\CreatePage::route('/create'), | |
'edit' => Pages\EditPage::route('/{record}/edit'), | |
]; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Filament\Resources\PageResource\Pages; | |
use App\Filament\Resources\PageResource; | |
use Filament\Resources\Pages\EditRecord; | |
use Illuminate\Support\Str; | |
class EditPage extends EditRecord | |
{ | |
protected static string $resource = PageResource::class; | |
protected function mutateFormDataBeforeFill(array $data): array | |
{ | |
$data['temp_content'][static::getTemplateName($data['template'])] = $data['content']; | |
unset($data['content']); | |
return $data; | |
} | |
protected function mutateFormDataBeforeSave(array $data): array | |
{ | |
$data['content'] = $data['temp_content'][static::getTemplateName($data['template'])]; | |
unset($data['temp_content']); | |
return $data; | |
} | |
public static function getTemplateName($class): string | |
{ | |
return Str::of($class)->afterLast('\\')->snake()->toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Amazing. Thanks.
For multiple options:
->visible(fn ($get) => in_array($class, $get('template')))