diff --git a/src/Translatable.php b/src/Translatable.php index c4f6946..ed451ab 100644 --- a/src/Translatable.php +++ b/src/Translatable.php @@ -4,10 +4,12 @@ use Closure; use Illuminate\Database\Eloquent\Model; +use Illuminate\Http\Request; use Illuminate\Http\Resources\MergeValue; use Illuminate\Support\Str; use Laravel\Nova\Fields\Field; use Laravel\Nova\Http\Controllers\ResourceIndexController; +use Laravel\Nova\Http\Controllers\FieldAttachmentController; use Spatie\NovaTranslatable\Exceptions\InvalidConfiguration; class Translatable extends MergeValue @@ -165,6 +167,20 @@ protected function createTranslatableFields() return; } + if ($this->isFieldAttachmentRequest(request())) { + collect($this->locales) + ->crossJoin($this->originalFields) + ->eachSpread(function (string $locale, Field $field) { + $translatedField = clone $field; + $translatedField->attribute = 'translations_'.$translatedField->attribute.'_'.$locale; + + $this->data[] = $translatedField; + $this->translatedFieldsByLocale[$locale][] = $translatedField; + }); + + return; + } + $this->data = []; collect($this->locales) @@ -237,4 +253,15 @@ protected function onIndexPage(): bool return $currentController === ResourceIndexController::class; } + + protected function isFieldAttachmentRequest(Request $request): bool + { + if (! $request->route()) { + return false; + } + + $currentController = Str::before(request()->route()->getAction()['controller'] ?? '', '@'); + + return $currentController === FieldAttachmentController::class; + } }