Skip to content

Instantly share code, notes, and snippets.

@bezhanSalleh
Last active August 16, 2022 18:06
Show Gist options
  • Save bezhanSalleh/c4fb48724f3ade246bda45a5294cf1be to your computer and use it in GitHub Desktop.
Save bezhanSalleh/c4fb48724f3ade246bda45a5294cf1be to your computer and use it in GitHub Desktop.
A trait to make Filament Simple Resources(modal) translatable
<?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()
]);
}
}
@mohamedsabil83
Copy link

Hi @bezhanSalleh, I'm wondering if you update this to work with the newer filament version in one of your projects?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment