generated from spatie/package-skeleton-laravel
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathTranslatableWithMedia.php
94 lines (71 loc) · 3.16 KB
/
TranslatableWithMedia.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?php
namespace Statikbe\FilamentFlexibleContentBlocks\Filament\Pages\CreateRecord\Concerns;
use Filament\Resources\Pages\CreateRecord\Concerns\Translatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Arr;
use Illuminate\Validation\ValidationException;
trait TranslatableWithMedia
{
use Translatable;
protected function handleRecordCreation(array $data): Model
{
$record = app(static::getModel());
$translatableAttributes = $this->getTranslatedAttributesWithTranslatableMedia();
$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->form->fill([
...$this->data,
...$localeData,
]);
try {
$this->form->validate();
} catch (ValidationException $exception) {
continue;
}
$localeData = Arr::only($this->form->getState(), array_keys($localeData));
$localeData = $this->mutateFormDataBeforeCreate($localeData);
foreach (Arr::only($localeData, $translatableAttributes) as $key => $value) {
$record->setTranslation($key, $locale, $value);
}
}
$this->data = $originalData;
$record->save();
return $record;
}
/**
* This function is overridden to be able to update the state of translatable media when the form locale switches.
* It makes use of the getTranslatableMediaCollections() function to include these media collections as translatable
* and thus change the state when the locale switches.
*
* @see HasTranslatableMedia
*/
public function updatedActiveLocale(string $newActiveLocale): void
{
if (blank($this->oldActiveLocale)) {
return;
}
$this->resetValidation();
$translatableAttributes = $this->getTranslatedAttributesWithTranslatableMedia();
$this->otherLocaleData[$this->oldActiveLocale] = Arr::only($this->data, $translatableAttributes);
$this->form->fill([
...Arr::except($this->data, $translatableAttributes),
...$this->otherLocaleData[$this->activeLocale] ?? [],
]);
unset($this->otherLocaleData[$this->activeLocale]);
}
private function getTranslatedAttributesWithTranslatableMedia(): array
{
$record = app(static::getModel());
$translatableAttributes = $record->getTranslatableAttributes();
if (method_exists($record, 'getTranslatableMediaCollections')) {
$translatableAttributes = array_merge($translatableAttributes, $record->getTranslatableMediaCollections());
} else {
throw new \BadMethodCallException('The model does not implement the HasTranslatableMedia interface. If this is not implemented translated images will not change when the language switch changes.');
}
return $translatableAttributes;
}
}