spatie / nova-translatable

Making Nova fields translatable
https://murze.be/making-nova-fields-translatable
MIT License
223 stars 28 forks source link

Adding Translatable to new Panel doesn't work #87

Closed Joorren closed 1 year ago

Joorren commented 1 year ago

Issue

When adding Translatable fields to a new Panel, the Translatable fields just get added to the main Panel.

How To Reproduce

  1. Add a new Panel to the resource fields
  2. Add a Translatable field to the new Panel

Versions

Example

I created an example that also includes a new Panel with a regular Nova field, to show that the issue isn't related to the Panel itself. The new Panel with a Nova field does get added. The new Panel with a Translatable field does not get added, the Translatable field gets added to the main Panel instead.

Code

...
    public function fields(NovaRequest $request): array
    {
        return [
            ID::make()->sortable(),

            // Working new Panel with Nova fields
            new Panel('External Fields', [

                Text::make('External Description')
                    ->sortable()
                    ->readonly(),

                Text::make('External Type')
                    ->sortable()
                    ->readonly(),
            ]),

            // Broken new Panel with Translatable fields
            new Panel('Candidate Translations', [
                Translatable::make([
                    Text::make('Candidate Description')
                        ->sortable()
                        ->hideFromIndex(),
                ]),

                Translatable::make([
                    Text::make('Candidate Type')
                        ->sortable()
                        ->hideFromIndex(),
                ]),
            ]),
        ];
    }
...

Result

image
Joorren commented 1 year ago

Changes made in https://github.com/spatie/nova-translatable/pull/77 don't seem to do the trick anymore.

Joorren commented 1 year ago

Combining a Nova field and a Translatable field into the same new Panel results in the Nova field showing in the new Panel and the Translatable field showing in the main Panel.

xPhantomNL commented 1 year ago

Running into this issue as well, any update or a workaround?

Downgrading Nova to 4.21.0 does seem to work on my end, so whatever was done in 4.22.0 breaks this package.

After some further inspection, in Nova 4.22 they're using the ConditionallyLoadsAttributes trait, which has a filter method (vendor/laravel/nova/src/Panel.php:125). That eventually leads to breaking the creation of new panels per locale.

freekmurze commented 1 year ago

I'd accept a PR that fixes this.

ozanhazer commented 1 year ago

I think the filter @xPhantomNL mentioned not only filters but also merges the field data to the original fields wrapped by Translatable and thus the panel information is copied over to the original field and removed from Translatable field.

Removing lines 192 & 193, as in below, fixes the issue. I'd create a PR but I'm not sure how to patch this without breaking backward compatibility.

class Translatable extends MergeValue
{
    // ....
    protected function createTranslatedField(Field $originalField, string $locale): Field
    {
        // ....
        $translatedField
            ->resolveUsing(function ($value, Model $model) use ($translatedField, $locale, $originalAttribute) {
                $translatedField->attribute = 'translations_'.$originalAttribute.'_'.$locale;
//                $translatedField->panel = $this->panel;
//                $translatedField->assignedPanel = $this->assignedPanel;

                return $model->translations[$originalAttribute][$locale] ?? '';
            });
spatie-bot commented 1 year ago

Dear contributor,

because this issue seems to be inactive for quite some time now, I've automatically closed it. If you feel this issue deserves some attention from my human colleagues feel free to reopen it.

marijoo commented 11 months ago

This is still a problem, I opened a PR #99