Magento module layout xml load order

11,999

Solution 1

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>

Solution 2

This is what I came up with: http://pastebin.com/tMUnWBWR

Now via layout xml or the addJs method I can use the sort_order parameter to define where I'd like my js included.

Example layout xml would look like:

<action method="addJs">
<script>js/script.js</script>
<params/>
<sort_order>0</sort_order>
</action>

Solution 3

Magento loads config files from app/etc/modules in alphabetically order in file app/code/core/Mage/Core/Model/Config.php in protected method _getDeclaredModuleFiles()

It tells that firstly Mage_All.xml will be loaded, then all the Magento config files (starts with Mage_) and finally custom modules in alphabetically order.

So here is a small but "dirty" trick - name your namespace as AaaPackagename. Name starts with numbers will also do the trick: 1Packagename or 000Packagename

Share:
11,999
ringerce
Author by

ringerce

Updated on June 08, 2022

Comments

  • ringerce
    ringerce almost 2 years

    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.