? Pending

User tests: Successful: Unsuccessful:

avatar framontb
framontb
26 Nov 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->setViewModels(Object $view),
you can easily assign the models you want to any of your views.

Everything works exactly the same. The method are 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();
    }


    public function setViewModels(Object $view)
    {
        parent::setViewModels($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->DisplayController() method to assign the models.
Nor call to Factories. All you need is in the DisplayController with minimum code.
You only overwrite the DisplayController->setViewModels(), 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 4.2 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->DisplayController().

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, comming 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, comming 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:

  • [ X ] No documentation changes for docs.joomla.org needed

  • Pull Request link for manual.joomla.org:

  • [ X ] No documentation changes for manual.joomla.org needed

avatar framontb framontb - open - 26 Nov 2022
avatar framontb framontb - change - 26 Nov 2022
Status New Pending
avatar joomla-cms-bot joomla-cms-bot - change - 26 Nov 2022
Category Libraries
avatar framontb framontb - change - 26 Nov 2022
The description was changed
avatar framontb framontb - edited - 26 Nov 2022
avatar framontb framontb - change - 26 Nov 2022
The description was changed
avatar framontb framontb - edited - 26 Nov 2022
avatar richard67
richard67 - comment - 26 Nov 2022

As it’s not a bug fix but an enhancement or new feature, this PR should be made for the 4.3-dev branch. @framontb Could you rebase your branch to 4.3-dev? Thanks in advance.

avatar richard67
richard67 - comment - 26 Nov 2022

P.S.: In addition, you should fix the code style errors reported here: https://ci.joomla.org/joomla/joomla-cms/59762/1/6 . You will see the details when clicking on the red “PHPCS”. Thanks in advance.

avatar laoneo
laoneo - comment - 28 Nov 2022

Nice change, but it definitely needs some documentation updates.

avatar framontb
framontb - comment - 28 Nov 2022

Thanks for the comments.
I will try to fix the issues.

PS: @laoneo , about the documentation any suggestions ?

avatar framontb framontb - change - 28 Nov 2022
Labels Added: ?
avatar framontb
framontb - comment - 28 Nov 2022

I had updated my feature branch with changes in 4.2-dev, so I couldn't do a rebase to 4.3

I made a new PR: Ease the use of multiple models in one view , so I close this one.

Sorry for the inconvenience.

avatar framontb framontb - change - 28 Nov 2022
Status Pending Closed
Closed_Date 0000-00-00 00:00:00 2022-11-28 19:53:51
Closed_By framontb
avatar framontb framontb - close - 28 Nov 2022

Add a Comment

Login with GitHub to post a comment