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

Compatibility issue with spatie/nova-translatable #40

Open
f-liva opened this issue Jan 15, 2019 · 10 comments
Open

Compatibility issue with spatie/nova-translatable #40

f-liva opened this issue Jan 15, 2019 · 10 comments
Labels
more info required More information is required

Comments

@f-liva
Copy link

f-liva commented Jan 15, 2019

It doesn't work with https://github.com/spatie/nova-translatable, could someone help me identify and solve the problem?

@wize-wiz
Copy link
Contributor

@fede91it could you supply an example?

@wize-wiz wize-wiz added the more info required More information is required label Sep 26, 2019
@ThisIsJustARandomGuy
Copy link

ThisIsJustARandomGuy commented Oct 1, 2019

@wize-wiz Not OP, but I ran into the same problem today. I'm trying to have a Textfield that's translatable and which depends on the value of a select. The code I'm trying to get to work is quite simple:

NovaDependencyContainer::make([
    Translatable::make([
        Text::make('Service', 'service')
    ]),
])->dependsOn('type', 'some_value')

But it fails in NovaDependencyContainer::resolveAttribute() when it tries to call $field->resolveForDisplay($resource) which does not exist on the Spatie Translatable Field.

Here's the code of the method that throws (NovaDependencyContainer.php on line 164):

protected function resolveAttribute($resource, $attribute)
{
    foreach ($this->meta['fields'] as $field) {
        // changed to resolveForDisplay(), resolve() will be called when displayCallback is empty.
        $field->resolveForDisplay($resource);
    }

    return [];
}

The error message is:

"Call to undefined method Spatie\\NovaTranslatable\\Translatable::resolveForDisplay()

Translatable extends MergeValue instead of Field, so there is no resolveForDisplay(...). Internally Translatable creates multiple fields (with their names being suffixed by _$locale), so I tried to modify resolveAttribute(...) so that it calls resolveForDisplay(...) on the nested fields (the ones created by Translatable). It no longer throws, but the translated fields do not show up in the backend. Let me know, if you need anything else.

Edit: Fixed missing code in my example

@wize-wiz
Copy link
Contributor

wize-wiz commented Oct 1, 2019

@ThisIsJustARandomGuy Thanks for the input. Just out of curiosity, what does ])->('type', VoucherType::SERVICE) exactly do?

I'll look into it.

@wize-wiz
Copy link
Contributor

wize-wiz commented Oct 1, 2019

@ThisIsJustARandomGuy

I haven't tested it yet, but this should fix the problem.

Translatable calls $this->createTranslatableFields(); in __construct

    public function __construct(array $fields = []) {
        ... 
        $this->createTranslatableFields();
    }

which calls $this->data[] = $this->createTranslatedField($field, $locale); in the eachSpread callback at the bottom. Which sets a resolve callback and returns the cloned $originalField.

$translatedField = clone $originalField;

$translatedField
    ->resolveUsing(function (...) {
        ...
    });

return $translatedField;

So the $translatedField lands in the public $data property of MergeValue. Iterating over each translatedField in public $data and call resolveForDisplay should fix the problem. You could replace this method and give some feedback till I have the time to implement a clean compatibility fix for all these Nova packages.

protected function resolveAttribute($resource, $attribute)
    foreach ($this->meta['fields'] as $field) {
        // patch for nova-translatable 
        // https://github.com/epartment/nova-dependency-container/issues/40
        if(is_a($field, \Illuminate\Http\Resources\MergeValue::class)) {
            foreach($field->data as $translated_field) {
                $translated_field->resolveForDisplay($resource);
            }
            continue;
        }
        // changed to resolveForDisplay(), resolve() will be called when displayCallback is empty.
        $field->resolveForDisplay($resource);
    }
    return [];
}

@wize-wiz
Copy link
Contributor

wize-wiz commented Oct 1, 2019

@ThisIsJustARandomGuy sorry, I updated the comment a view times. Try the fix if you like and if it works, let me know, I'll pull it in the next release.

@ThisIsJustARandomGuy
Copy link

@wize-wiz Sorry for the late reply. I missed some code in my original comment. It should say ->dependsOn('type', Voucher::SERVICE). I'll edit my comment. Anyways, thank you for the quick and thorough reply! I'll test this tomorrow morning and will give feedback.

@ThisIsJustARandomGuy
Copy link

@wize-wiz I got it to work correctly by overriding the NovaDependencyContainer constructor with a slightly altered version of your patch:

public function __construct($fields, $attribute = null, $resolveCallback = null)
{
    parent::__construct('', $attribute, $resolveCallback);

    $metaFields = [];
    foreach ($fields as $field) {
        // patch for nova-translatable
        // https://github.com/epartment/nova-dependency-container/issues/40
        if ( is_a($field, MergeValue::class) ) {
            foreach ($field->data as $translated_field) {
                $metaFields[] = $translated_field;
            }
            continue;
        }
        $metaFields[] = $field;
    }

    $this->withMeta([ 'fields' => $metaFields ]);
}

@wize-wiz
Copy link
Contributor

wize-wiz commented Oct 7, 2019

That doesn't seem right even if it might fix the problem. The translatable container should manage resolving its own fields, I'll look into this problem when I have some more time to test around.

@ghost
Copy link

ghost commented Feb 16, 2020

Still... spatie/nova-translatable#34

@aliwesome
Copy link

I'm facing same issue. does anybody have a solution for this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
more info required More information is required
Projects
None yet
Development

No branches or pull requests

4 participants