nova-translatable icon indicating copy to clipboard operation
nova-translatable copied to clipboard

Package remove images from Trix

Open valeriu-dev opened this issue 3 years ago • 2 comments

when save resource, in trix field with ->translatable() all images are deleted

  • optimistdigital/nova-translatable 2.1
  • Laravel 9.25.1
  • nova 4.12.14

valeriu-dev avatar Sep 22 '22 16:09 valeriu-dev

any update here?

https://github.com/spatie/nova-translatable/issues/18

valeriu-dev avatar Jun 05 '23 13:06 valeriu-dev

That's because Nova is unable to save the image as it errors out with a 404. I just ran into the same issue.

The Controller responsible for saving the pending attachment tries to get the field by its attribute, but cannot find it so throws a 404. I moved forward by overriding the FieldAttachmentController in the IoC container and normalizing incoming requests first:

use Illuminate\Http\JsonResponse;
use Illuminate\Http\Response;
use Laravel\Nova\Http\Controllers\FieldAttachmentController as Controller;
use Laravel\Nova\Http\Requests\NovaRequest;

final class FieldAttachmentController extends Controller
{
    private const TRANSLATABLE_SEPARATOR = '.';

    public function destroyAttachment(NovaRequest $request): Response
    {
        return parent::destroyAttachment($this->normalizeFieldAttributeName($request));
    }

    public function destroyPending(NovaRequest $request): Response
    {
        return parent::destroyPending($this->normalizeFieldAttributeName($request));
    }

    public function store(NovaRequest $request): JsonResponse
    {
        return parent::store($this->normalizeFieldAttributeName($request));
    }

    private function normalizeFieldAttributeName(NovaRequest $request): NovaRequest
    {
        return $request->merge([
            'field' => current(explode(self::TRANSLATABLE_SEPARATOR, $request->route('field', ''), 2))
        ]);
    }
}

Register it as a singleton in your NovaServiceProvider:

final class NovaServiceProvider extends NovaApplicationServiceProvider
{
    public array $singletons = [
        \Laravel\Nova\Http\Controllers\FieldAttachmentController::class => \App\Nova\Controllers\FieldAttachmentController::class,
    ];
    
    // ...
}

mabdullahsari avatar Jun 22 '23 15:06 mabdullahsari