Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

Translate File Field #18

Closed
pierreberchtold opened this issue Dec 11, 2018 · 12 comments
Closed

Translate File Field #18

pierreberchtold opened this issue Dec 11, 2018 · 12 comments
Labels
bug Something isn't working help wanted Extra attention is needed

Comments

@pierreberchtold
Copy link

When I try to translate a Nova File Field I get error when saving. The File Field is trying to save the translations_FIELDNAME_LANG attribute and not the FIELDNAME attribute.

I don't really see through how nova-translatable and the File Field are working so I have no idea what the best approach would be to fix this. If you have a hint I'd be happy to do a PR.

@NSpehler
Copy link

Same issue here, I'm getting an error Call to a member function store() on null when using Translatable on a File field.

@freekmurze freekmurze added bug Something isn't working help wanted Extra attention is needed labels Jan 12, 2019
@danyelkeddah
Copy link

Hey everyone, Any updates ?
well I have the same issue with Trix field too, after dropping an image inside the field it is trying to upload it and store its data into the table on this end point
nova-api/{resource}/trix-attachment/translations_FIELDNAME_LANG
for some reason it's dumping 404 (Not Found) to the browser console

@n0n0n0n0
Copy link

n0n0n0n0 commented Mar 6, 2019

same error, looking for solution!

@tonci
Copy link

tonci commented Apr 14, 2019

As a workaround, just add:

Trix::make('translations_FIELDNAME_LANG')->withFiles('public')->hideWhenCreating()->hideWhenUpdating()->hideFromDetail()

so your fields function will look like:

public function fields(Request $request)
    {
        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 needs to be done for each language you want to support (yeah... ugly).
Tested this with amazon s3 and seem to also work when you delete the files from the trix field.

@jrmadsen67
Copy link

when trying @tonci method I get:

image

Trix images work fine without the wrapper.
If I have Trix::make('body')->withFiles('public'), shouldn't my workaround field be:
Trix::make('translations_body_de-AT')->withFiles('public')->hideWhenCreating()->hideWhenUpdating()->hideFromDetail(),?

@mkhmylife
Copy link

@jrmadsen67 You have to use Trix::make('translations_body_de-AT', 'translations_body_de-AT')->withFiles('public')->hideWhenCreating()->hideWhenUpdating()->hideFromDetail() to make it work

@ilikourou
Copy link

Is there any update here?

Both Trix and Froala fields don't work with spatie/nova-translatable when it comes to file uploads/attachments.

@Grunkhead
Copy link

Same issue here. Seems like the call is of the wrong type.

image

@Grunkhead
Copy link

As a workaround, just add:

Trix::make('translations_FIELDNAME_LANG')->withFiles('public')->hideWhenCreating()->hideWhenUpdating()->hideFromDetail()

so your fields function will look like:

public function fields(Request $request)
    {
        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 needs to be done for each language you want to support (yeah... ugly).
Tested this with amazon s3 and seem to also work when you delete the files from the trix field.

Do not forget to place it in the main array within the fields method of het Resource. It acts like a hidden field.

Just to be sure. Thanks!

@ilikourou
Copy link

Keep in mind that this workaround seems to be working only with Trix field type.

If you are using a different type of editor (eg Froala), this is not enough to make it work, unfortunately.

@Naoray
Copy link

Naoray commented Dec 14, 2020

Had the same problem with file uploads using the Trix field. Fixed it by creating my own Translatable field by extending Spatie's and overriding the createTranslationFields() method.

// User Resource
public function fields(Request $request)
    {
        return [
              //...

              Translatable::make([
                  Trix::make('About')
                       ->withFiles('public')
                       ->alwaysShow(),
                  ])
        ];
    }

// Translatable field
namespace App\Nova\Fields;

use Laravel\Nova\Fields\Trix;
use Laravel\Nova\Fields\Field;
use Spatie\NovaTranslatable\Translatable as BaseTranslatable;

class Translatable extends BaseTranslatable
{
    protected function createTranslatableFields()
    {
        parent::createTranslatableFields();

        if ($this->onIndexPage()) {
            return;
        }

        collect($this->locales)
            ->crossJoin($this->originalFields)
            ->eachSpread(function (string $locale, Field $field) {
                if ($field instanceof Trix && $field->withFiles) {
                    $this->data[] = get_class($field)::make('translations_' . $field->attribute . '_' . $locale)
                        ->onlyOnIndex()
                        ->showOnIndex(false)
                        ->withFiles($field->disk, $field->storagePath);
                }
            });
    }
}

@freekmurze I don't feel that this solution is clean in any way, but maybe it sparks an idea on your side on how to solve this in a more elegant way? Otherwise I am also open to create a PR with my fix if you want.

@JoJuhasz
Copy link

What about File fields ?
Is this not the subject of this issue ? This is not about Trix

@spatie spatie locked and limited conversation to collaborators Apr 21, 2021

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
bug Something isn't working help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests