Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magento module layout xml load order

I have a custom extension that includes jQuery through layout XML like so:

<reference name="head">
    <action method="addJs"><script>jquery/jquery-1.8.1.min.js</script></action>
</reference>

Other extensions that use jQuery need to be loaded after my module so jQuery remains on top.

By default Magento loads everything alphabetically. Is there any way to specify a sort order for extensions?

One way is to override page.xml in my theme and manually include jQuery into the head or I can set every custom module to depend on the module I want on top for example:

<depends>
    <Package_JQueryLib />
</depends>

Any other recommendations?

Edit

I was thinking I could also override Mage_Page_Block_Html_Head and modify the addItem() method to include sorting or prepending files to the head.

like image 276
ringerce Avatar asked Sep 26 '12 21:09

ringerce


Video Answer


1 Answers

The <depends /> node is a valid approach - it is intended to provide control over colliding module xpath values, but it also can be used to affect the order in which child nodes appear.

The other option, rewriting the page/html_head class, is something that has been needed for awhile and, I imagine, actually done by others.

Another option to try would be to use an <update> directive in your custom module layout XML, as the directives in an <update /> are processed before the other directives. This means that jQuery will load before all other files.

<?xml version="1.0" encoding="UTF-8"?>
<layout>
    <default>
        <update handle="add_jquery_first" />
    </default>

    <add_jquery_first>
        <action method="addJs" block="head">
            <file>jquery/jquery-1.8.1.min.js</file>
        </action>
        <!-- or
        <reference name="head">
            <action method="addJs">
                <file>jquery/jquery-1.8.1.min.js</file>
            </action>
        </reference>
        -->
    </add_jquery_first>
</layout>
like image 103
benmarks Avatar answered Sep 23 '22 03:09

benmarks