Skip to content

Commit

Permalink
Fix 404 when adding attachments to translated Trix fields
Browse files Browse the repository at this point in the history
  • Loading branch information
albertcat committed Jul 18, 2024
1 parent ab229fd commit 7981fce
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/Translatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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;
}
}

0 comments on commit 7981fce

Please sign in to comment.