Skip to content

Commit

Permalink
Merge pull request #106 from albertcat/attachments
Browse files Browse the repository at this point in the history
Fix 404 when adding attachments to translated Trix fields
  • Loading branch information
freekmurze authored Jul 19, 2024
2 parents ab229fd + 7981fce commit c51f2ca
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 c51f2ca

Please sign in to comment.