Created
February 27, 2024 16:11
-
-
Save nathangross/fcd60c84a7b312b4b746f2fca8c01c0a to your computer and use it in GitHub Desktop.
This file contains hidden or 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\Blocks; | |
use Filament\Forms\Get; | |
use Illuminate\Support\Str; | |
use Filament\Support\Markdown; | |
use Illuminate\Support\Collection; | |
use Filament\Forms\Components\Select; | |
use Filament\Forms\Components\Toggle; | |
use Filament\Forms\Components\Fieldset; | |
use Filament\Forms\Components\Repeater; | |
use Filament\Forms\Components\TextInput; | |
use Filament\Forms\Components\RichEditor; | |
use Filament\Forms\Components\Builder\Block; | |
use Filament\Forms\Components\MarkdownEditor; | |
use Filament\Forms\Components\SpatieMediaLibraryFileUpload; | |
class ChapterBlock | |
{ | |
public string $chapterId; | |
public static function make(): Block | |
{ | |
$instance = new self(); | |
$instance->chapterId = (string) Str::uuid(); // Assigning a UUID to each instance | |
return Block::make('chapter-block') | |
->label(function (?array $state): string { | |
if ($state === null) { | |
return 'Chapter Block'; | |
} | |
return (($state['title'] . ' (Chapter Block)') ?? 'Chapter Block'); | |
}) | |
->columnSpanFull() | |
->schema([ | |
Fieldset::make('Chapter Title') | |
->columns(2) | |
->schema([ | |
TextInput::make('title') | |
->label('Title') | |
->required() | |
->columnSpanFull(), | |
Toggle::make('show_in_nav') | |
->label('Show in Navigation') | |
->default(true), | |
Toggle::make('custom_nav_title') | |
->label('Custom Title in Navigation') | |
->default(false) | |
->live(), | |
TextInput::make('nav-title') | |
->label('Navigation Title') | |
->helperText('Use this to display a different title in the navigation') | |
->columnSpanFull() | |
->hidden(fn (Get $get) => $get('custom_nav_title') !== true), | |
]), | |
SpatieMediaLibraryFileUpload::make('media') | |
->label('Chapter Image') | |
->collection('chapter-images') | |
->acceptedFileTypes(['image/*']) | |
->customProperties(fn (): array => [ | |
'chapter_id' => $this->chapterId, // Using the unique chapterId for each instance | |
]) | |
->filterMediaUsing(function (Collection $media): Collection { | |
return $media->where('custom_properties.chapter_id', $this->chapterId); | |
}), | |
TextInput::make('image_caption'), | |
MarkdownEditor::make('content') | |
]); | |
} | |
} |
This file contains hidden or 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\Blocks\ChapterBlock; | |
use App\Filament\Blocks\HeadingBlock; | |
use App\Filament\Blocks\StatsBlock; | |
use App\Filament\Blocks\TextBlock; | |
use Filament\Forms; | |
use App\Models\Page; | |
use Filament\Tables; | |
use Filament\Forms\Get; | |
use Filament\Forms\Set; | |
use Filament\Forms\Form; | |
use Filament\Tables\Table; | |
use Illuminate\Support\Str; | |
use Filament\Resources\Resource; | |
use Filament\Forms\Components\Toggle; | |
use Filament\Forms\Components\Builder; | |
use Filament\Forms\Components\Section; | |
use Filament\Forms\Components\Fieldset; | |
use Filament\Forms\Components\Textarea; | |
use Filament\Tables\Actions\EditAction; | |
use Filament\Tables\Columns\IconColumn; | |
use Filament\Tables\Columns\TextColumn; | |
use Filament\Forms\Components\TextInput; | |
use Filament\Forms\Components\RichEditor; | |
use Filament\Forms\Components\Builder\Block; | |
use Filament\Tables\Actions\BulkActionGroup; | |
use Filament\Tables\Actions\DeleteBulkAction; | |
use App\Filament\Resources\PageResource\Pages; | |
use Illuminate\Database\Eloquent\SoftDeletingScope; | |
use App\Filament\Resources\PageResource\Pages\EditPage; | |
use App\Filament\Resources\PageResource\Pages\ListPages; | |
use App\Filament\Resources\PageResource\Pages\CreatePage; | |
use App\Filament\Resources\PageResource\RelationManagers; | |
use Filament\Forms\Components\Repeater; | |
use Filament\Forms\Components\SpatieMediaLibraryFileUpload; | |
class PageResource extends Resource | |
{ | |
protected static ?string $model = Page::class; | |
protected static ?string $navigationIcon = 'heroicon-o-rectangle-stack'; | |
protected static ?string $navigationGroup = 'Pages'; | |
public static function form(Form $form): Form | |
{ | |
return $form | |
->schema([ | |
Section::make('Page Details') | |
->collapsible() | |
->persistCollapsed() | |
->columns(2) | |
->schema([ | |
TextInput::make('title') | |
->label('Page Title') | |
->required() | |
->maxLength(255) | |
->columnSpanFull() | |
->live() | |
->afterStateUpdated(function (Set $set, $state) { | |
$set('slug', Str::slug($state)); | |
}), | |
TextInput::make('slug') | |
->label('Slug (Permalink)') | |
->required() | |
->helperText('This cannot be changed once the page is created. It is used to create the URL for the page.') | |
->maxLength(255) | |
->disabledOn('edit'), | |
TextInput::make('sort_order') | |
->label('Sort Order') | |
->helperText('The order in which the page will appear in the navigation') | |
->numeric(), | |
Toggle::make('show_in_navigation') | |
->label('Show in Main Navigation') | |
->default(false) | |
->columnSpanFull(), | |
]), | |
Section::make('Hero') | |
->collapsible() | |
->persistCollapsed() | |
->schema([ | |
Toggle::make('has_hero') | |
->default(true) | |
->label('Enable Hero Section') | |
->live(), | |
Fieldset::make('Hero') | |
->hidden(fn (Get $get) => $get('has_hero') == false) | |
->schema([ | |
Toggle::make('use_page_title') | |
->default(true) | |
->label('Use Page Title') | |
->helperText('Use the page title as the hero title') | |
->columnSpanFull() | |
->live(), | |
TextInput::make('hero_title') | |
->label('Custom Hero Title') | |
->maxLength(255) | |
->columnSpanFull() | |
->hidden(fn (Get $get) => $get('use_page_title') == true), | |
TextInput::make('subtitle') | |
->columnSpanFull() | |
->maxLength(255), | |
SpatieMediaLibraryFileUpload::make('image') | |
->label('Hero Image') | |
->collection('page-heros') | |
->name('Image'), | |
]) | |
]), | |
Section::make('Content') | |
->collapsible() | |
->schema([ | |
Builder::make('content_json') | |
->label('Page Content') | |
->columnSpanFull() | |
->blocks([ | |
StatsBlock::make(), | |
HeadingBlock::make(), | |
ChapterBlock::make(), | |
TextBlock::make(), | |
]) | |
->reorderableWithButtons() | |
->collapsible() | |
->collapsed() | |
->blockNumbers(false) | |
->cloneable(), | |
]), | |
]); | |
} | |
public static function table(Table $table): Table | |
{ | |
return $table | |
->columns([ | |
TextColumn::make('title') | |
->searchable(), | |
TextColumn::make('slug') | |
->searchable() | |
->toggleable(isToggledHiddenByDefault: true), | |
IconColumn::make('show_in_navigation') | |
->boolean(), | |
TextColumn::make('sort_order') | |
->numeric() | |
->sortable() | |
->toggleable(isToggledHiddenByDefault: false), | |
TextColumn::make('created_at') | |
->dateTime() | |
->sortable() | |
->toggleable(isToggledHiddenByDefault: true), | |
TextColumn::make('updated_at') | |
->dateTime() | |
->sortable() | |
->toggleable(isToggledHiddenByDefault: true), | |
]) | |
->reorderable('sort_order') | |
->defaultSort('sort_order', 'asc') | |
->filters([ | |
// | |
]) | |
->actions([ | |
EditAction::make(), | |
]) | |
->bulkActions([ | |
BulkActionGroup::make([ | |
DeleteBulkAction::make(), | |
]), | |
]); | |
} | |
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'), | |
]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment