laravel-filamentlaravel-translatable

How to make editor with filament/spatie-laravel-translatable-plugin


Looking at https://filamentphp.com/docs/2.x/spatie-laravel-translatable-plugin/getting-started docs I added filament/spatie-laravel-translatable-plugin ^2.17.17 into laravel 9 app with filament/filament 2.17.17

"filament/tables": "^2.17.17" and added language switcher my resource editor and listing with getTranslatableLocales method. But I did not find how I can

  1. create editor with all locales in my system(I need decision with changeble list of locales ) to show current values(in edit mode) and save it in db in json field.

  2. In table of resource to show labels in default locale.

Please example how can I do it ?

Additive Info :

I try to implerment this task, but I did not find how :

  1. I need get current record in “edit” mode, like
   public static function form(Form $form): Form
    {
        \Log::info(varDump(self::$record, ' -1 self::$record::')); // it has no such $record
        ...

as I need to create several inputs with values from this model.

  1. I can use method mutateFormDataBeforeCreate in app/Filament/Resources/BannerResource/Pages/CreateBanner.php file, which can be usefull when I add new model, but I did find similar method in app/Filament/Resources/BannerResource/Pages/EditBanner.php file. Has it ?

Additive Info # 2: Searching for decision I found this https://github.com/filamentphp/filament/issues/3772

branch with inline locale switcher : https://prnt.sc/ZfH_IA54tlFC

looks like only filament/spatie-laravel-translatable-plugin package is used in this branch and I do not see any other packages used here, but how can I make such inline locale switcher in my app ?

I need locale switcher opnly for bd/table/column values, not for layout of admin area itself, so I do not need actions like :

Actions\LocaleSwitcher::make()

Additive Info # 3: Looking at valid answer of the branch Laravel Filament Table Actions: url() not working

I try to make as next to recieve current record in “edit” mode :

public static function form(Form $form): Form
{
    $localeInputs = with(new BannerResource)->getLocaleInputs();
    \Log::info(varDump($localeInputs, ' -1 FOUND $localeInputs::'));
    ...
    
    
protected function getLocaleInputs()
{
    return function (Banner $record) {
        return $record->record;
    };

}

But in log file I see :

 (Object of Closure) : -1 FOUND $localeInputs:: : Array
(
    [0] => Closure Object
        (
            [this] => App\Filament\Resources\BannerResource Object
                (
                )

            [parameter] => Array
                (
                    [$record] => <required>
                )
        )
)

But the valid Banner $record as I expected...


Solution

  • Method afterStateHydrated helped me :

    $localeFields = [];
    foreach ($locales as $locale) {
        $localeFields[] = TextInput::make('text_' . $locale)
            ->afterStateHydrated(function (TextInput $component, $state) use ($locale) {
                $bannerModel = $component->getModelInstance();
                $component->state($bannerModel->getTranslation('text', $locale));
            })
            ->label('Text in ' . AppLocale::getAppLocaleLabel($locale))
            ->required()
            ->minLength(2)
            ->maxLength(255);
        $localeFields[] = RichEditor::make('description_' . $locale)
            ->afterStateHydrated(function (RichEditor $component, $state) use ($locale) {
                $bannerModel = $component->getModelInstance();
                $component->state($bannerModel->getTranslation('description', $locale));
            })
            ->label('Description in ' . AppLocale::getAppLocaleLabel($locale))
            ->required()
            ->columnSpan('full');
    
    }
    ...
    
    Forms\Components\Section::make('Locales')
        ->schema($localeFields)
        ->columns(2),
    

    That works for me!