Forked from bezhanSalleh/CanMakeSimpleResourcesTranslatable.php
Created
March 30, 2022 14:04
-
-
Save mohamedsabil83/eee3622b28825970c716b232e27092f8 to your computer and use it in GitHub Desktop.
A trait to make Filament Simple Resources(modal) translatable
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\Traits; | |
use Filament\Tables; | |
use Filament\Pages\Actions\Modal; | |
use Illuminate\Database\Eloquent\Model; | |
use Filament\Pages\Actions\ButtonAction; | |
use Filament\Resources\Pages\Concerns\HasActiveFormLocaleSelect; | |
trait CanMakeSimpleResourcesTranslatable | |
{ | |
use HasActiveFormLocaleSelect; | |
public $editMode = true; | |
protected function beforeCreateFill(): void | |
{ | |
$this->editMode = false; | |
$this->activeFormLocale = static::getResource()::getDefaultTranslatableLocale(); | |
} | |
protected function handleRecordCreation(array $data): Model | |
{ | |
$record = static::getModel()::usingLocale( | |
$this->activeFormLocale, | |
)->fill($data); | |
$record->save(); | |
return $record; | |
} | |
protected function getCreateAction(): ButtonAction | |
{ | |
return parent::getCreateAction() | |
->modalActions([ | |
Modal\Actions\ButtonAction::make('create') | |
->label(__('filament::resources/pages/list-records.actions.create.modal.actions.create.label')) | |
->submit('callMountedAction') | |
->color('primary'), | |
Modal\Actions\ButtonAction::make('createAndCreateAnother') | |
->label(__('filament::resources/pages/list-records.actions.create.modal.actions.create_and_create_another.label')) | |
->action('createAndCreateAnother') | |
->color('secondary'), | |
Modal\Actions\ButtonAction::make('cancel') | |
->label(__('tables::table.actions.modal.buttons.cancel.label')) | |
->cancel() | |
->color('secondary'), | |
$this->getActiveFormLocaleSelectAction() | |
]); | |
} | |
protected function fillEditForm(): void | |
{ | |
if ($this->activeFormLocale === null) { | |
$this->setActiveFormLocale(); | |
} | |
$data = $this->getMountedTableActionRecord()->toArray(); | |
foreach (static::getResource()::getTranslatableAttributes() as $attribute) { | |
$data[$attribute] = $this->getMountedTableActionRecord()->getTranslation($attribute, $this->activeFormLocale); | |
} | |
$this->getMountedTableActionForm()->fill($data); | |
} | |
protected function setActiveFormLocale(): void | |
{ | |
$resource = static::getResource(); | |
$availableLocales = array_keys($this->getMountedTableActionRecord()->getTranslations($resource::getTranslatableAttributes()[0])); | |
$resourceLocales = $resource::getTranslatableLocales(); | |
$defaultLocale = $resource::getDefaultTranslatableLocale(); | |
$this->activeFormLocale = in_array($defaultLocale, $availableLocales) ? $defaultLocale : array_intersect($availableLocales, $resourceLocales)[0] ?? $defaultLocale; | |
$this->getMountedTableActionRecord()->setLocale($this->activeFormLocale); | |
} | |
protected function handleRecordUpdate(Model $record, array $data): Model | |
{ | |
$record->setLocale($this->activeFormLocale)->fill($data)->save(); | |
return $record; | |
} | |
public function updatedActiveFormLocale(): void | |
{ | |
if ($this->editMode) { | |
$this->fillEditForm(); | |
} | |
} | |
public function updatingActiveFormLocale(): void | |
{ | |
if ($this->editMode) { | |
$this->save(); | |
} | |
} | |
protected function getEditAction(): Tables\Actions\Action | |
{ | |
return parent::getEditAction() | |
->modalActions([ | |
Modal\Actions\ButtonAction::make('save') | |
->label(__('filament::resources/pages/list-records.table.actions.edit.modal.actions.save.label')) | |
->action('save') | |
->color('primary'), | |
Modal\Actions\ButtonAction::make('cancel') | |
->label(__('tables::table.actions.modal.buttons.cancel.label')) | |
->cancel() | |
->color('secondary'), | |
$this->getActiveFormLocaleSelectAction() | |
]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment