Question

I'm trying to use phpstorm with xdebug to step through magento code. In particular, I'm trying to understand how the layout object's xml is formed. There is code that is called from within generateXML, and then that code calls simplexml_load_string( $layout updates). The $layout updates is just an array of short xml strings, that are then imploded to form a concatenated string.

But then simplexml_load_string is called, and I can't debug it since its a php built-in, and after stepping through that line, the $xml is automatically generated.

Also the simplexml_load_string is passed a layout element object that extends Varien_Simplexml that extends SimpleXMLElement. I'm confused as to whats going on under that hood. Can anyone explain it to me? Thanks.

Was it helpful?

Solution

It's sort of hard to explain in a single Stack Overflow question — if you want the full details in a less compressed way, No Frills Magento Layout walks through the functioning of the layout system in detail. (self link, book I wrote and sell, but I honestly don't know of a better resource)

High level though is sounds like you have two questions: How is the updates array populated, and why are the Simple XML objects a Varien_Simplexml object.

Answering the second question first, when Magento instantiates its SimpleXML objects it uses the little known $class_name parameter of simplexml_load_string and simplexml_load_file. This is a PHP feature which allows a user to specify what sort of objects should represent the SimpleXML nodes. Magento passes in the string Varien_Simplexml, which means PHP tries to create each object returned by these functions as a Varien_Simplexml object. Varien_Simplexml extends the base SimpleXML object, which means these XML objects behave exactly the same as a normal PHP SimpleXML object except they also have the additional methods defined by Varien_Simplexml.

As for loading the updates in "the right order", here's how that update array is populated. First, Magento loads all the layout XML files in a theme (page.xml, catalog.xml, etc; saving the local.xml for last) into a single giant XML object called the package layout. Then, Magento goes through each layout "handle" (default, cms_index_index, etc) that's been specified by other parts of Magento, and looks for package XML layout nodes that match each handle name. As it finds each of these nodes, they're added to the update array.

So, to your titular question, order is influenced by two things. First, the order of the handles — all the default handle nodes are loaded into the update array first, followed by STORE_default, followed by the full action handle update (cms_index_index), etc. Second, order is included why the way PHP returns child XML nodes. This is currently (and likely to remain) the order the nodes appear/were-added-to the package XML document.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top