Saving a page with a category failed with "El campo categoría no está
en la lista de valores permitidos" — for every category, not only the
one being tried. Filament builds such a list through a query, which
handles `->relationship('category', 'name->es')`, but validates a choice
by reading the title back off the record as a property,
`$record->{'name->es'}`, which does not exist. No label, so every
option was refused.
The same pattern was in seven more places: a call's, a testimonial's,
a talk's and a repository's project, a testimonial's service, and the
page and application filters. TranslatedOptionLabel gives Filament the
record's own translation instead, in the language of whoever uses the
panel; the JSON path stays for search and ordering.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
223 lines
7.6 KiB
PHP
223 lines
7.6 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Filament\Resources\Talks;
|
|
|
|
use App\Filament\RelationManagers\RevisionsRelationManager;
|
|
use App\Filament\Resources\Talks\Pages\CreateTalk;
|
|
use App\Filament\Resources\Talks\Pages\EditTalk;
|
|
use App\Filament\Resources\Talks\Pages\ListTalks;
|
|
use App\Filament\Support\TranslatableTabs;
|
|
use App\Filament\Support\TranslatedOptionLabel;
|
|
use App\Models\Talk;
|
|
use BackedEnum;
|
|
use Filament\Actions\BulkActionGroup;
|
|
use Filament\Actions\DeleteAction;
|
|
use Filament\Actions\DeleteBulkAction;
|
|
use Filament\Actions\EditAction;
|
|
use Filament\Forms\Components\DatePicker;
|
|
use Filament\Forms\Components\Select;
|
|
use Filament\Forms\Components\Textarea;
|
|
use Filament\Forms\Components\TextInput;
|
|
use Filament\Forms\Components\Toggle;
|
|
use Filament\Resources\Resource;
|
|
use Filament\Schemas\Components\Section;
|
|
use Filament\Schemas\Schema;
|
|
use Filament\Support\Icons\Heroicon;
|
|
use Filament\Tables\Columns\IconColumn;
|
|
use Filament\Tables\Columns\TextColumn;
|
|
use Filament\Tables\Filters\SelectFilter;
|
|
use Filament\Tables\Filters\TernaryFilter;
|
|
use Filament\Tables\Table;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use UnitEnum;
|
|
|
|
/**
|
|
* Talks given somewhere that is not this website.
|
|
*
|
|
* The cheapest content there is to keep a site looking alive, and the only
|
|
* kind that shows the collective exists outside its own pages. Written once,
|
|
* it shows both on the landing page and on the profile of whoever gave it.
|
|
*/
|
|
class TalkResource extends Resource
|
|
{
|
|
protected static ?string $model = Talk::class;
|
|
|
|
protected static string|BackedEnum|null $navigationIcon = Heroicon::OutlinedMicrophone;
|
|
|
|
protected static ?int $navigationSort = 7;
|
|
|
|
public static function getModelLabel(): string
|
|
{
|
|
return __('panel.talks.label');
|
|
}
|
|
|
|
public static function getPluralModelLabel(): string
|
|
{
|
|
return __('panel.talks.plural');
|
|
}
|
|
|
|
public static function getNavigationGroup(): string|UnitEnum|null
|
|
{
|
|
return __('panel.groups.collective');
|
|
}
|
|
|
|
public static function getRecordTitle(?Model $record): ?string
|
|
{
|
|
return $record?->title;
|
|
}
|
|
|
|
public static function form(Schema $schema): Schema
|
|
{
|
|
return $schema->components([
|
|
Section::make()
|
|
->columns(2)
|
|
->schema([
|
|
// The name of a talk that was given once, in one language.
|
|
// Translating it would invent a talk that never happened.
|
|
TextInput::make('title')
|
|
->label(__('panel.talks.fields.title'))
|
|
->helperText(__('panel.talks.fields.title_hint'))
|
|
->required()
|
|
->maxLength(255)
|
|
->columnSpanFull(),
|
|
|
|
TextInput::make('event')
|
|
->label(__('panel.talks.fields.event'))
|
|
->required()
|
|
->maxLength(255),
|
|
|
|
TextInput::make('city')
|
|
->label(__('panel.talks.fields.city'))
|
|
->maxLength(120),
|
|
|
|
DatePicker::make('held_at')
|
|
->label(__('panel.talks.fields.held_at')),
|
|
|
|
Select::make('language')
|
|
->label(__('panel.talks.fields.language'))
|
|
->options(fn () => collect(config('locales.supported'))
|
|
->map(fn (array $meta, string $code) => $meta['native'] ?? $code)
|
|
->all())
|
|
->default(config('locales.default'))
|
|
->required(),
|
|
|
|
TextInput::make('video_url')
|
|
->label(__('panel.talks.fields.video_url'))
|
|
->url()
|
|
->maxLength(2048),
|
|
|
|
TextInput::make('slides_url')
|
|
->label(__('panel.talks.fields.slides_url'))
|
|
->url()
|
|
->maxLength(2048),
|
|
]),
|
|
|
|
Section::make()
|
|
->columns(2)
|
|
->schema([
|
|
Select::make('member_id')
|
|
->label(__('panel.talks.fields.member'))
|
|
->helperText(__('panel.talks.fields.member_hint'))
|
|
->relationship('member', 'name')
|
|
->searchable()
|
|
->preload(),
|
|
|
|
Select::make('project_id')
|
|
->label(__('panel.talks.fields.project'))
|
|
->relationship('project', 'title->'.config('locales.default'))
|
|
->getOptionLabelFromRecordUsing(TranslatedOptionLabel::using('title'))
|
|
->searchable()
|
|
->preload(),
|
|
|
|
TranslatableTabs::make(fn (string $locale) => [
|
|
Textarea::make("summary.{$locale}")
|
|
->label(__('panel.talks.fields.summary'))
|
|
->rows(3)
|
|
->maxLength(500)
|
|
->columnSpanFull(),
|
|
])->columnSpanFull(),
|
|
|
|
Toggle::make('is_public')
|
|
->label(__('panel.talks.fields.is_public'))
|
|
->default(true),
|
|
]),
|
|
]);
|
|
}
|
|
|
|
public static function table(Table $table): Table
|
|
{
|
|
return $table
|
|
->defaultSort('held_at', 'desc')
|
|
->columns([
|
|
TextColumn::make('title')
|
|
->label(__('panel.talks.fields.title'))
|
|
->weight('medium')
|
|
->description(fn (Talk $record) => trim($record->event.' · '.($record->city ?? ''), ' ·'))
|
|
->wrap()
|
|
->searchable(['title', 'event']),
|
|
|
|
TextColumn::make('member.name')
|
|
->label(__('panel.talks.fields.member'))
|
|
->placeholder('—')
|
|
->toggleable(),
|
|
|
|
TextColumn::make('held_at')
|
|
->label(__('panel.talks.fields.held_at'))
|
|
->date()
|
|
->placeholder('—')
|
|
->sortable(),
|
|
|
|
IconColumn::make('video_url')
|
|
->label(__('panel.talks.fields.video_url'))
|
|
->boolean()
|
|
->toggleable(),
|
|
|
|
IconColumn::make('is_public')
|
|
->label(__('panel.talks.fields.is_public'))
|
|
->boolean(),
|
|
])
|
|
->filters([
|
|
TernaryFilter::make('is_public')->label(__('panel.talks.fields.is_public')),
|
|
|
|
SelectFilter::make('member')
|
|
->label(__('panel.talks.fields.member'))
|
|
->relationship('member', 'name')
|
|
->searchable()
|
|
->preload(),
|
|
])
|
|
->recordActions([
|
|
EditAction::make(),
|
|
DeleteAction::make(),
|
|
])
|
|
->toolbarActions([
|
|
BulkActionGroup::make([DeleteBulkAction::make()]),
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* @return array<int, string>
|
|
*/
|
|
public static function getGloballySearchableAttributes(): array
|
|
{
|
|
return ['title', 'event'];
|
|
}
|
|
|
|
public static function getRelations(): array
|
|
{
|
|
return [
|
|
RevisionsRelationManager::class,
|
|
];
|
|
}
|
|
|
|
public static function getPages(): array
|
|
{
|
|
return [
|
|
'index' => ListTalks::route('/'),
|
|
'create' => CreateTalk::route('/create'),
|
|
'edit' => EditTalk::route('/{record}/edit'),
|
|
];
|
|
}
|
|
}
|