Skip to content

Commit

Permalink
Hydrate and dehydrate the tabs using the field instead of a trait (#21)
Browse files Browse the repository at this point in the history
* Hydrate and dehydrate the tabs using the field instead of a trait

---------

Co-authored-by: AngryMoustache <[email protected]>
Co-authored-by: Jyrki De Neve <[email protected]>
  • Loading branch information
3 people authored Aug 29, 2024
1 parent a1d2967 commit 0831ed0
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 46 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
"spatie/laravel-package-tools": "^1.12"
},
"require-dev": {
"larastan/larastan": "^2.0",
"laravel/pint": "^1.0",
"nunomaduro/collision": "^7.0|^8.0",
"larastan/larastan": "^2.0",
"orchestra/testbench": "^8.0|^9.0",
"pestphp/pest": "^2.0",
"pestphp/pest-plugin-laravel": "^2.0",
Expand Down
12 changes: 12 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,15 @@ parameters:
message: "#^Access to an undefined property Livewire\\\\Component\\:\\:\\$data\\.$#"
count: 2
path: src/Actions/CopyTranslationAction.php
-
message: "#^Call to an undefined method Livewire\\\\Component\\:\\:getRecord\\(\\)\\.$#"
count: 1
path: src/Forms/TranslatableTabs.php
-
message: "#^Cannot call method getTranslatedLocales\\(\\)\\ on class-string\\|object\\.$#"
count: 1
path: src/Forms/TranslatableTabs.php
-
message: "#^Cannot call method getTranslation\\(\\)\\ on class-string\\|object\\.$#"
count: 1
path: src/Forms/TranslatableTabs.php
57 changes: 54 additions & 3 deletions src/Forms/TranslatableTabs.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
use Filament\Support\Concerns\CanBeContained;
use Filament\Support\Concerns\CanPersistTab;
use Filament\Support\Concerns\HasExtraAlpineAttributes;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Support\Arr;
use Livewire\Component as Livewire;

class TranslatableTabs extends Component
Expand Down Expand Up @@ -38,6 +40,34 @@ final public function __construct(string $label)
$this->label($label);

$this->columnSpan(['lg' => 2]);

$this->afterStateHydrated(static function (TranslatableTabs $component, string|array|null $state, Livewire $livewire): void {
if (blank($state)) {
$component->state([]);

return;
}

$record = $livewire->getRecord();

if (! $record || ! method_exists($record, 'getTranslatableAttributes')) {
return;
}

foreach ($record->getTranslatableAttributes() as $field) {
foreach ($record->getTranslatedLocales($field) as $locale) {
$value = $record->getTranslation($field, $locale);

if ($value instanceof Arrayable) {
$value = $value->toArray();
}

$state[$locale][$field] = $value;
}
}

$component->state($state);
});
}

public static function make(): static
Expand All @@ -48,6 +78,23 @@ public static function make(): static
return $static;
}

public function dehydrateState(array &$state, bool $isDehydrated = true): void
{
parent::dehydrateState($state, $isDehydrated);

$model = app($this->getModel());

foreach (Arr::except($state['data'], $model->getFillable()) as $locale => $values) {
if (! is_array($values)) {
continue;
}

foreach (Arr::only($values, $model->getTranslatableAttributes()) as $key => $value) {
$state['data'][$key][$locale] = $value;
}
}
}

public function getTabQueryStringKey(): ?string
{
return 'locale';
Expand Down Expand Up @@ -98,6 +145,11 @@ public function locales(array|Closure $locales): static
return $this;
}

public function getLocales(): array
{
return $this->evaluate($this->locales);
}

public function icon(null|string|Closure $icon): static
{
$this->icon = $icon;
Expand Down Expand Up @@ -127,15 +179,14 @@ public function isTabPersistedInQueryString(): bool
public function getChildComponents(): array
{
$tabs = [
Tab::make('Default')
->schema($this->evaluate($this->defaultFields)),
Tab::make('Default')->schema($this->evaluate($this->defaultFields)),
];

if (! is_null($this->extraTabs)) {
$tabs = array_merge($tabs, $this->evaluate($this->extraTabs));
}

foreach ($this->evaluate($this->locales) as $locale) {
foreach ($this->getLocales() as $locale) {
$tabs[] = Tab::make($locale)
->schema($this->evaluate($this->translatableFields, [
'locale' => $locale,
Expand Down
42 changes: 0 additions & 42 deletions src/Resources/Traits/HasTranslations.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,52 +3,10 @@
namespace Codedor\TranslatableTabs\Resources\Traits;

use Filament\Actions\Action;
use Illuminate\Support\Arr;
use Illuminate\Support\Js;

trait HasTranslations
{
protected function mutateFormDataBeforeCreate(array $data): array
{
return $this->mutateData($data);
}

protected function mutateFormDataBeforeSave(array $data): array
{
return $this->mutateData($data);
}

protected function mutateFormDataBeforeFill(array $data): array
{
$record = $this->getRecord();
foreach ($record->getTranslatableAttributes() as $field) {
$locales = array_keys($record->getTranslations($field, config('translatable.allowed-locales', null)));
foreach ($locales as $locale) {
$data[$locale][$field] = $record->getTranslation($field, $locale);
}
}

return $data;
}

protected function mutateData(array $data): array
{
$model = app($this->getModel());
foreach (Arr::except($data, $model->getFillable()) as $locale => $values) {
if (! is_array($values)) {
continue;
}

foreach (Arr::only($values, $model->getTranslatableAttributes()) as $key => $value) {
$data[$key][$locale] = $value;
}

// unset($data[$locale]);
}

return $data;
}

protected function getCancelFormAction(): Action
{
return Action::make('cancel')
Expand Down

0 comments on commit 0831ed0

Please sign in to comment.