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>
138 lines
4.6 KiB
PHP
138 lines
4.6 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Filament\Resources\Repositories;
|
|
|
|
use App\Enums\GitProvider;
|
|
use App\Filament\Resources\Repositories\Pages\CreateRepository;
|
|
use App\Filament\Resources\Repositories\Pages\EditRepository;
|
|
use App\Filament\Resources\Repositories\Pages\ListRepositories;
|
|
use App\Filament\Resources\Repositories\Tables\RepositoriesTable;
|
|
use App\Filament\Support\TranslatedOptionLabel;
|
|
use App\Models\Repository;
|
|
use BackedEnum;
|
|
use Filament\Forms\Components\Select;
|
|
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\Table;
|
|
use UnitEnum;
|
|
|
|
/**
|
|
* Git repositories connected to the collective's projects.
|
|
*
|
|
* The form is intentionally tiny: a repository is identified by its provider
|
|
* and "owner/repo", and everything else — stars, languages, contributors,
|
|
* newcomer-friendly issues — arrives from the provider on each sync. Nothing
|
|
* synced is editable, because a hand-typed star count is a lie waiting to
|
|
* happen.
|
|
*/
|
|
class RepositoryResource extends Resource
|
|
{
|
|
protected static ?string $model = Repository::class;
|
|
|
|
protected static string|BackedEnum|null $navigationIcon = Heroicon::OutlinedCodeBracketSquare;
|
|
|
|
protected static ?int $navigationSort = 5;
|
|
|
|
protected static ?string $recordTitleAttribute = 'full_name';
|
|
|
|
public static function getModelLabel(): string
|
|
{
|
|
return __('panel.repositories.label');
|
|
}
|
|
|
|
public static function getPluralModelLabel(): string
|
|
{
|
|
return __('panel.repositories.plural');
|
|
}
|
|
|
|
public static function getNavigationGroup(): string|UnitEnum|null
|
|
{
|
|
return __('panel.groups.collective');
|
|
}
|
|
|
|
public static function form(Schema $schema): Schema
|
|
{
|
|
return $schema->components([
|
|
Section::make()
|
|
->columns(2)
|
|
->schema([
|
|
Select::make('provider')
|
|
->label(__('panel.repositories.fields.provider'))
|
|
->options(GitProvider::class)
|
|
->default(GitProvider::default())
|
|
->required()
|
|
// Only providers with a driver registered in
|
|
// config/git.php can actually be synced.
|
|
->disableOptionWhen(fn (string $value) => ! GitProvider::from($value)->isSupported()),
|
|
|
|
TextInput::make('full_name')
|
|
->label(__('panel.repositories.fields.full_name'))
|
|
->placeholder('colectivo/mi-proyecto')
|
|
->helperText(__('panel.repositories.fields.full_name_hint'))
|
|
->required()
|
|
->maxLength(255)
|
|
->rules(['regex:/^[\w.-]+\/[\w.-]+$|^[\w.-]+$/']),
|
|
|
|
Select::make('project_id')
|
|
->label(__('panel.repositories.fields.project'))
|
|
->relationship('project', 'title->'.config('locales.default'))
|
|
->getOptionLabelFromRecordUsing(TranslatedOptionLabel::using('title'))
|
|
->searchable()
|
|
->preload(),
|
|
|
|
Toggle::make('is_primary')
|
|
->label(__('panel.repositories.fields.is_primary'))
|
|
->helperText(__('panel.repositories.fields.is_primary_hint')),
|
|
|
|
Toggle::make('show_publicly')
|
|
->label(__('panel.repositories.fields.show_publicly'))
|
|
->default(true),
|
|
]),
|
|
]);
|
|
}
|
|
|
|
public static function table(Table $table): Table
|
|
{
|
|
return RepositoriesTable::configure($table);
|
|
}
|
|
|
|
/**
|
|
* @return array<int, string>
|
|
*/
|
|
public static function getGloballySearchableAttributes(): array
|
|
{
|
|
return ['full_name'];
|
|
}
|
|
|
|
public static function getPages(): array
|
|
{
|
|
return [
|
|
'index' => ListRepositories::route('/'),
|
|
'create' => CreateRepository::route('/create'),
|
|
'edit' => EditRepository::route('/{record}/edit'),
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Flags repositories whose sync keeps failing — usually renamed or deleted
|
|
* upstream — so stale numbers do not sit on the public site unnoticed.
|
|
*/
|
|
public static function getNavigationBadge(): ?string
|
|
{
|
|
$failing = static::getModel()::query()->failing()->count();
|
|
|
|
return $failing > 0 ? (string) $failing : null;
|
|
}
|
|
|
|
public static function getNavigationBadgeColor(): ?string
|
|
{
|
|
return 'danger';
|
|
}
|
|
}
|