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

[5.0]New method BaseController->prepareViewModel() to set the View Models, #39447

Merged
merged 5 commits into from
Jan 13, 2023

Conversation

framontb
Copy link
Contributor

@framontb framontb commented Dec 18, 2022

Summary of Changes

Changes to ease the use of multiple models in one view for custom (or core) components in Joomla 4.

By moving just three lines of code from BaseController->display()
to a new method BaseController->setViewModel($view),
you can easily assign the models you want to any of your views.

Everything works exactly the same. The method is called at the same point the lines were before.
But the diference is:

  • now you can overwrite the new method in the DisplayController of your custom component like this:
    public function display($cachable = false, $urlparams = [])
    {
         return parent::display();
    }


    protected function prepareViewModel($view)
    {
        parent::prepareViewModel($view);

        $viewName = $view->getName();

        // Push the Second model into the Foos view
        if ($viewName == strtolower("Foos") && ($model = $this->getModel('Second'))) {
            $view->setModel($model);
        }

        // Push the Third model into the Foos view
        if ($viewName == strtolower("Foos") && ($model = $this->getModel('Third'))) {
            $view->setModel($model);
        }
    }
    }

Here you see that you don't need to touch your DisplayController->display() method to assign the models.
Nor call to Factories. All you need is already in the DisplayController with minimum code.
You only overwrite the DisplayController->setViewModel(), call the parent (that assigns the default model as usual),
and assigns a couple of new models to the "Foos" view.

Now you only have to call it properly and easily in your view, like this:

	public function display($tpl = null)
	{
            $this->msg  = $this->get('Msg');
            $this->msg2 = $this->get('Msg', 'Second');
            $this->msg3 = $this->get('Msg', 'Third');
        
	    return parent::display($tpl);
	}

Here you see how to call the methods of the Second and Third Model.

Just like it is described here: Using multiple models in an MVC component
Only now, you don't need to care about custom display() methods in your views.
The default display() works perfectly fine.

Testing Instructions

I developed this little Component com_foos to test the changes.
You only need to apply the changes to your Joomla 5 installation and install the component as usual.

Actual result BEFORE applying this Pull Request

Break MVC model to call several models for one view, using factories called in helpers,
or need to rewrite your own DisplayController->display().

See How can I use two models in my custom component view in Joomla4?

Expected result AFTER applying this Pull Request

If you pick in the Backend > Component > COM_FOOS menu link, you will see:

Hello Foo from the model: DEFAULT
Hello FOOS from the ADMINISTRATOR model: SECOND
Hello FOOS from the ADMINISTRATOR model: THIRD

Each one of this messages, coming from a different model attached to the Foos View.

If you create a menu link in the FronEnd of the testing site, for the com_foos component as usual, you will see:

Hello FOO from the SITE model: DEFAULT
Hello FOO from the SITE model: SECOND
Hello FOOS from the ADMINISTRATOR model: THIRD

Each one of this messages, coming from a different model attached to the Foo View.

Note that the THIRD model is a backend model. You can assign it exactly the same way.

Link to documentations

Please select:

  • Documentation link for docs.joomla.org: Not specified yet

  • No documentation changes for docs.joomla.org needed

  • Pull Request link for manual.joomla.org:

  • No documentation changes for manual.joomla.org needed

@SharkyKZ

This comment was marked as off-topic.

@laoneo
Copy link
Member

laoneo commented Jan 11, 2023

@framontb sorry for the delay answering here. Can you add the comments and then we are ready to go.

…ew param

2. Check setModel() method exists for $view
@framontb
Copy link
Contributor Author

@laoneo I think the requested changes are done.

@HLeithner
Copy link
Member

Wouldn't make sense to check the view if it has the interface before calling the prepare function?

@framontb
Copy link
Contributor Author

Wouldn't make sense to check the view if it has the interface before calling the prepare function?

Don't know, but the $view = $this->getView() call returns a ViewInterface (says the DocBlocK):

     * @return  ViewInterface  Reference to the view or an error.
     *
...
     */
    public function getView($name = '', $type = '', $prefix = '', $config = array())

@HLeithner
Copy link
Member

OK then it's not needed

@laoneo laoneo merged commit 4b1c7b0 into joomla:5.0-dev Jan 13, 2023
@laoneo
Copy link
Member

laoneo commented Jan 13, 2023

Thank you very much for your contribution!

@laoneo laoneo added this to the Joomla! 5.0 milestone Jan 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants