Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Package remove images from Trix #83

Open
vodnicearv opened this issue Sep 22, 2022 · 2 comments
Open

Package remove images from Trix #83

vodnicearv opened this issue Sep 22, 2022 · 2 comments

Comments

@vodnicearv
Copy link

vodnicearv commented Sep 22, 2022

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
@vodnicearv vodnicearv changed the title this package remove images from trim Package remove images from Trix Sep 22, 2022
@vodnicearv
Copy link
Author

any update here?

spatie/nova-translatable#18

@mabdullahsari
Copy link

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,
    ];
    
    // ...
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants