From 7981fcec9ea5f17872fa41f866bef32026c10a5e Mon Sep 17 00:00:00 2001 From: Albert Date: Thu, 18 Jul 2024 17:14:12 +0200 Subject: [PATCH] Fix 404 when adding attachments to translated Trix fields --- src/Translatable.php | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) 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; + } }