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

Fix nested flexible #250

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Fix nested flexible #250

wants to merge 1 commit into from

Conversation

Gil-1
Copy link

@Gil-1 Gil-1 commented Jan 14, 2021

It allows to get the right model when there is nested flexible.

It allows to get the right model when there is nested flexible.
Copy link
Member

@toonvandenbos toonvandenbos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does $this->model contain in your case ? Is it a resource ?

@Gil-1
Copy link
Author

Gil-1 commented Jan 18, 2021

Hello Toon, this is the model that needs to be associated with the media using spatie laravel media library.
When using a nested flexible $this->model doesn't return the right model. The code proposed fix this.

@harmenjanssen
Copy link
Contributor

harmenjanssen commented Feb 17, 2021

I see what you're doing here. I also have some trouble getting the right model back in case of nested layouts supporting media.

In my case I can fix it with a custom accessor method:

    public function getSlidingCardsAttribute()
    {
        return $this->flexible('sliding_cards', [
            'sticky_story_sliding_card' => StickyStorySlidingCard::class,
        ])->tap(fn(Collection $cards) => $cards->each->setModel($this->model));
    }

What's happening here is that sliding_cards is a Flexible field, inside another Flexible field. It has to support media via the HasMedia class (otherwise I get an error that the original model cannot be found), but that also blocks child layouts from retrieving the media correctly (because the model they get back is the parent Layout and not the outermost model, which is what is stored in the media table under model_type).

The tap call above ensures child layouts get the model of the parent layout, thus matching the model_type in the table again.

(I do wonder whether model->model is enough here; what if the Flexible field is nested twice?)

@harmenjanssen
Copy link
Contributor

Hmm, sorry, this will fail in CMS context because $this->flexible() will return an array.

This implementation works inside and outside of Nova:

        $out = $this->flexible('sliding_cards', [
            'sticky_story_sliding_card' => StickyStorySlidingCard::class,
        ]);
        if ($out instanceof Collection) {
            $out->each->setModel($this->model);
        }
        return $out;

@trippo
Copy link

trippo commented Mar 16, 2021

Please use this pr #267
because there was an error on import Layout class and getMediaModel method access (there was protected but must be public). This fixes the nested layout problem.

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

Successfully merging this pull request may close these issues.

4 participants