Skip to content

Commit

Permalink
feat: add support for Spatie Translatable
Browse files Browse the repository at this point in the history
Signed-off-by: Lukas Frey <[email protected]>
  • Loading branch information
lukas-frey committed Mar 22, 2024
1 parent cb5f9c1 commit cbcee42
Show file tree
Hide file tree
Showing 3 changed files with 245 additions and 2 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
},
"require-dev": {
"orchestra/testbench": "^8.0",
"laravel/pint": "^1.11"
"laravel/pint": "^1.11",
"filament/spatie-laravel-translatable-plugin": "^3.2"
},
"extra": {
"laravel": {
Expand Down
129 changes: 128 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

115 changes: 115 additions & 0 deletions src/Concerns/CreateRelatedRecord/Translatable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php

namespace Guava\FilamentNestedResources\Concerns\CreateRelatedRecord;

use Filament\Facades\Filament;
use Filament\Resources\Concerns\HasActiveLocaleSwitcher;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Arr;
use Illuminate\Validation\ValidationException;
use Livewire\Attributes\Locked;

trait Translatable
{

use HasActiveLocaleSwitcher;

protected ?string $oldActiveLocale = null;

#[Locked]
public $otherLocaleData = [];

public function mountTranslatable(): void
{
$resource = Filament::getModelResource($this->getRelation()->getRelated());

$this->activeLocale = $resource::getDefaultTranslatableLocale();
}

public function getTranslatableLocales(): array
{
$resource = Filament::getModelResource($this->getRelation()->getRelated());

return $resource::getTranslatableLocales();
}

protected function handleRecordCreation(array $data): Model
{
$model = $this->getRelation()->getRelated();
$resource = Filament::getModelResource($model);

$record = app($model::class);

$translatableAttributes = $resource::getTranslatableAttributes();

$record->fill(Arr::except($data, $translatableAttributes));

foreach (Arr::only($data, $translatableAttributes) as $key => $value) {
$record->setTranslation($key, $this->activeLocale, $value);
}

$originalData = $this->data;

foreach ($this->otherLocaleData as $locale => $localeData) {
$this->data = [
...$this->data,
...$localeData,
];

try {
$this->form->validate();
} catch (ValidationException $exception) {
continue;
}

$localeData = $this->mutateFormDataBeforeCreate($localeData);

foreach (Arr::only($localeData, $translatableAttributes) as $key => $value) {
$record->setTranslation($key, $locale, $value);
}
}

$this->data = $originalData;

if ($owner = $this->getOwnerRecord()) {
$record = $this->associateRecordWithParent($record, $owner);
}

if (
$resource::isScopedToTenant() &&
($tenant = Filament::getTenant())
) {
return $this->associateRecordWithTenant($record, $tenant);
}

$record->save();

return $record;
}

public function updatingActiveLocale(): void
{
$this->oldActiveLocale = $this->activeLocale;
}

public function updatedActiveLocale(string $newActiveLocale): void
{
if (blank($this->oldActiveLocale)) {
return;
}

$this->resetValidation();
$resource = Filament::getModelResource($this->getRelation()->getRelated());

$translatableAttributes = $resource::getTranslatableAttributes();

$this->otherLocaleData[$this->oldActiveLocale] = Arr::only($this->data, $translatableAttributes);

$this->data = [
...Arr::except($this->data, $translatableAttributes),
...$this->otherLocaleData[$this->activeLocale] ?? [],
];

unset($this->otherLocaleData[$this->activeLocale]);
}
}

0 comments on commit cbcee42

Please sign in to comment.