whitecube / nova-flexible-content

Flexible Content & Repeater Fields for Laravel Nova
MIT License
789 stars 229 forks source link

Question: Is it possible to use custom Presets and Resolvers to a MorphTo ? #268

Open crezra opened 3 years ago

crezra commented 3 years ago

Taking a look at the docs and a couple issues here on the repo, I understand that relationships with Flexible Content are different from the usual ones used for nova, and to get a relationship working with this package, I would need to set a custom preset and resolver.

However, what would the resolver look like for a MorphTo relation. What I'm trying to achieve is to have nova's default MorphTo field, with a select for the morphable_type and then a select for the morphable_id, and put it inside a Layout I can reuse.

class CustomPreset extends Preset
{
    /**
     * Execute the preset configuration
     *
     * @param Flexible $field
     * @return void
     * @throws Exception
     */
    public function handle(Flexible $field): void
    {
        $field->button(Str::ucfirst(__('flexible.buttons.add.link')));
        $field->addLayout(MorphableLayout::class)->resolver(CustomResolver:class);
    }
}
class MorphableLayout extends Layout
{
    public function fields(): array
    {
        return [
            MorphTo::make('Relatable')->types([\App\Nova\User::class])
        ];
    }
}

However I am not sure what the CustomeResolver class is supposed to return to get it to work with a MorphTo field.

Thanks for the great package.

Running

Broutard commented 3 years ago

@crezra Have you successfully added a morphTo field to a layout?