Skip to content

Commit f5411a0

Browse files
committed
Support Filament translatable content driver
Add support for a translatable content driver when creating and updating records in the Translatable trait. The action callbacks now try to obtain a driver from Livewire (makeFilamentTranslatableContentDriver) or from the container (Filament\\SpatieLaravelTranslatableContentDriver) and delegate record creation/update to it. If no driver is available, the previous behavior is preserved: translatable attributes are merged/fill()ed and the record is saved. Also ensure mutateFormDataBeforeSave is evaluated for update actions before filling/saving to avoid Filament actions not mutating data prior to save.
1 parent 3ccbadb commit f5411a0

1 file changed

Lines changed: 46 additions & 17 deletions

File tree

src/Concern/TreeRecords/Translatable.php

Lines changed: 46 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -47,17 +47,32 @@ protected function afterConfiguredCreateAction(CreateAction $action): CreateActi
4747

4848
if (method_exists($action, 'using')) {
4949
$model = $action->getModel();
50-
$action->using(function (array $data) use ($model) {
51-
if (method_exists($model, 'getTranslatableAttributes')) {
52-
foreach (app($model)->getTranslatableAttributes() as $attr) {
53-
$data[$attr] = array_merge(
54-
[$this->getActiveLocale() => $data[$attr]],
55-
$this->getActiveLocale() !== app()->getFallbackLocale() ? [app()->getFallbackLocale() => $data[$attr]] : [],
56-
);
50+
$action->using(function (array $data, $livewire) use ($model) {
51+
$translatableContentDriver = null;
52+
if (method_exists($livewire, 'makeFilamentTranslatableContentDriver')) {
53+
$translatableContentDriver = $livewire->makeFilamentTranslatableContentDriver();
54+
} elseif (class_exists('Filament\\SpatieLaravelTranslatableContentDriver')) {
55+
$translatableContentDriver = app('Filament\\SpatieLaravelTranslatableContentDriver');
56+
}
57+
58+
if ($translatableContentDriver) {
59+
$record = $translatableContentDriver->makeRecord($model, $data);
60+
} else {
61+
if (method_exists($model, 'getTranslatableAttributes')) {
62+
foreach (app($model)->getTranslatableAttributes() as $attr) {
63+
$data[$attr] = array_merge(
64+
[$this->getActiveLocale() => $data[$attr]],
65+
$this->getActiveLocale() !== app()->getFallbackLocale() ? [app()->getFallbackLocale() => $data[$attr]] : [],
66+
);
67+
}
5768
}
69+
$record = new $model;
70+
$record->fill($data);
5871
}
5972

60-
return $model::create($data);
73+
$record->save();
74+
75+
return $record;
6176
});
6277
}
6378

@@ -74,19 +89,33 @@ protected function afterConfiguredEditAction(EditAction $action): EditAction
7489
});
7590

7691
if (method_exists($action, 'using')) {
77-
$action->using(function (array $data, Model $record) use ($action) {
92+
$action->using(function (array $data, Model $record, $action, $livewire) {
7893

79-
$data = $action->evaluate($action->getMutateFormDataBeforeSave(), ['data' => $data]);
94+
$translatableContentDriver = null;
95+
if (method_exists($livewire, 'makeFilamentTranslatableContentDriver')) {
96+
$translatableContentDriver = $livewire->makeFilamentTranslatableContentDriver();
97+
} elseif (class_exists('Filament\\SpatieLaravelTranslatableContentDriver')) {
98+
$translatableContentDriver = app('Filament\\SpatieLaravelTranslatableContentDriver');
99+
}
100+
101+
if ($translatableContentDriver) {
102+
$translatableContentDriver->updateRecord($record, $data);
103+
} else {
104+
// Preventing filmanet action not mutating the data before save
105+
if ($action && method_exists($action, 'getMutateFormDataBeforeSave')) {
106+
$data = $action->evaluate($action->getMutateFormDataBeforeSave(), ['data' => $data]);
107+
}
80108

81-
$record->fill($data);
82-
if (method_exists($record, 'setTranslation') &&
83-
method_exists($record, 'getTranslatableAttributes')
84-
) {
85-
foreach ($record->getTranslatableAttributes() as $attr) {
86-
$record->setTranslation($attr, $this->getActiveLocale(), $data[$attr]);
109+
$record->fill($data);
110+
if (method_exists($record, 'setTranslation') &&
111+
method_exists($record, 'getTranslatableAttributes')
112+
) {
113+
foreach ($record->getTranslatableAttributes() as $attr) {
114+
$record->setTranslation($attr, $this->getActiveLocale(), $data[$attr]);
115+
}
87116
}
117+
$record->save();
88118
}
89-
$record->save();
90119
});
91120
}
92121

0 commit comments

Comments
 (0)