outl1ne / nova-translatable

A Laravel Nova package that allows you to make any input field translatable.
MIT License
197 stars 55 forks source link

Package remove images from Trix #83

Open vodnicearv opened 1 year ago

vodnicearv commented 1 year ago

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

vodnicearv commented 1 year ago

any update here?

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

mabdullahsari commented 1 year ago

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,
    ];

    // ...
}