Skip to content
This repository has been archived by the owner on Nov 16, 2021. It is now read-only.

Attachments not working when Froala field is translatable #26

Closed
ilikourou opened this issue Jul 25, 2019 · 7 comments
Closed

Attachments not working when Froala field is translatable #26

ilikourou opened this issue Jul 25, 2019 · 7 comments

Comments

@ilikourou
Copy link

Hello,

I am using spatie/laravel-translatable to have my models translatable, and also spatie/nova-translatable to manage the translatable fields through Nova.

When using Froala field alone, like this:

Froala::make('Body')
    ->withFiles('images')
    ->showOnIndex()

everything works fine.

However, when I combine it with Translatable field:

Translatable::make([
    Froala::make('Body')
        ->withFiles('images')
        ->showOnIndex()
]),

attachments don't work anymore.

Dev tools inspector shows the following error:
POST http://site.local/nova-vendor/froala-field/pages/attachments/translations_body_en 404 (Not Found)
I guess that field here should be just "body", but due to translatable package it becomes "translations_body_en"?

Is it, by any chance, possible to fix the compatibility between the two packages?

Thanks in advance,
Ilias

@slavarazum
Copy link
Collaborator

Hi!

Haven't experience with spatie/laravel-translatable.
Is it works with Trix editor?

@ilikourou
Copy link
Author

It does not work with Trix editor by default, however there is a workaround (as posted here) for Trix:

return [
    ID::make()->sortable(),
    \Spatie\NovaTranslatable\Translatable::make([
        Trix::make('content')->withFiles('s3'),
    ]),
    Trix::make('translations_content_en')->withFiles('s3')->hideWhenCreating()->hideWhenUpdating()->hideFromDetail(),
    Trix::make('translations_content_dk')->withFiles('s3')->hideWhenCreating()->hideWhenUpdating()->hideFromDetail(),
];

This does the job.

When I try exactly the same with Froala, although the file is temporarily uploaded/stored (both in the filesystem and nova_pending_froala_attachments db table), when I click the "update/save" button in my nova resource, the file is deleted from filesystem and also nova_pending_froala_attachments table (without creating a new entry to nova_froala_attachments).

As it seems, the problem lies on spatie/laravel-translatable and spatie/nova-translatable packages end. Half the job is done here, since the file is saved in the filesystem and also in the temporary db table, something happens in the final steps...

@slavarazum
Copy link
Collaborator

Feel free to make a PR with spatie/nova-translatable compatibility. I think you close to find a solution!

@ilikourou
Copy link
Author

This is what I ended up doing for a temporary workaround:

In Froala.php I modified fillAttribute() method as following:

protected function fillAttribute(NovaRequest $request, $requestAttribute, $model, $attribute)
    {
        
        if ($request->{$this->attribute.'DraftId'} && $this->withFiles) {
            $pendingAttachmentClass =
                config('nova.froala-field.attachments_driver', self::DRIVER_NAME) === self::DRIVER_NAME
                    ? FroalaPendingAttachment::class
                    : TrixPendingAttachment::class;

                $pendingAttachmentClass::persistDraft(
                    $request->{$this->attribute.'DraftId'},
                    $this,
                    $model
                );
        }

        if (isset($this->fillCallback)) {
            return call_user_func(
                $this->fillCallback,
                $request,
                $model,
                $attribute,
                $requestAttribute
            );
        }

        $this->fillAttributeFromRequest(
            $request,
            $requestAttribute,
            $model,
            $attribute
        );
    }

Actually, I moved attachment handling to the beginning of fillAttribute() method, so that attachments are saved/handled before any other modifications are done to the field (for example by Translatable class).

Of course this could not be a permanent solution, but seems like the only way to make it work at the moment.

@martin-ro
Copy link

Same issue with Nova Flexible Content.

@socramjunio2
Copy link

Vou fazer uma PR, é fácil de resolver.
O problema é que o translate do spatial altera o nome do atributo, colocando as linguagens na frente, o froala deveria colocar o originalAttribute, tendo em consideração que vários outros fields alteram os atributos e isso vai acontecer para todos.

Ou pode até mesmo, identificar o translatabe no método META[] e se existir remover os caracteres terminandos com pontos no final antes de adicionar na busca e do filtro.

@gerasart
Copy link

  \Spatie\NovaTranslatable\Translatable::make([
                Froala::make('Text')->rules('required')
            ]),

works for me

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

No branches or pull requests

5 participants