I was working on adding the ability to set a default product list sorting option for just search results, when I noticed that \app\code\core\Mage\CatalogSearch\Block\Result.php has the method setListOrders(). I couldn't find any PHP code that called this method, but did find this:


<action method="setListOrders"/>

This suggests to me that the method is simply called, without passing any data, at some point before the layout is rendered. Can someone tell me more about how this works, and where this happens in the process of building the page?


It's a dependency injection system via XML.

When generating/instantiating the layout blocks (usually with $this->loadLayout() in a controller), Mage will parse the computed layout XML, calling Mage_Core_Model_Layout::generateBlocks for every block node, in a recursive manner.

Then, among other things, it searches for action nodes in the current block node. When it finds one, it passes the action node to Mage_Core_Model_Layout::_generateAction, which calls call_user_func_array for you, with the method you specified in the method node attribute (and optional arguments).

It looks like this:
call_user_func_array(array([Mage_CatalogSearch_Block_Result object], 'setListOrders'), array());


You can visit this document to check how it works. quick summary: when you loadLayout it will look for all tag in xml, e.g.: ,,. so when Magento see it would call public PHP methods of the block classes. and output to html when you call renderLayout.

you can dump layout data to see what blocks you load by using getLayout().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.