12

Do the <sequence/> tags in module.xml files impact the order Magento will process layout handle XML files in? My preliminary research says no, but that seems wrong so I'm looking for confirmation/proof that they don't (proof == source code points where layout handle XML file paths are generated and loaded)

If the sequence tags don't impact layout handle XML file loading order -- is there a way to change the module order these files are loaded in?

Specific problem I'm trying to solve is if I have the following loaded before the container is actually aded to the page

    <referenceContainer name="product.info.media">    
        <action method="unsetChild">
            <argument name="block" xsi:type="string">product.info.media.image</argument>
        </action>
    </referenceContainer>

Magento barfs.

1 Answer 1

17

The sequence in module.xml has an impact on app/etc/config.php. This file gets updated when you run bin/magento module:enable Vendor_ModuleName so if you have added/changed sequences I'd suggest disabling your module and then re-enabling it. Updating your module.xml file and clearing your cache isn't enough here, you'll need to do a full disable re-enable cycle to get Magento to see sequence changes during development.

The sort order of modules in the config.php file is then used for all other configuration file loading as per Anton's comment here.

The code locations in that comment are a bit out of date. This is the code for the sequence sorting https://github.com/magento/magento2/blob/2.0.2/lib/internal/Magento/Framework/Module/ModuleList/Loader.php#L131

Update 2:

app/etc/di.xml

<type name="Magento\Framework\View\Model\Layout\Merge">
    <arguments>
        <argument name="fileSource" xsi:type="object">Magento\Framework\View\Layout\File\Collector\Aggregated\Proxy</argument>
        <argument name="pageLayoutFileSource" xsi:type="object">pageLayoutFileCollectorAggregated</argument>
        <argument name="cache" xsi:type="object">Magento\Framework\App\Cache\Type\Layout</argument>
    </arguments>
</type>

which references a page layout file collector in the same di.xml

<virtualType name="pageLayoutFileCollectorAggregated" type="Magento\Framework\View\Layout\File\Collector\Aggregated">
    <arguments>
        <argument name="baseFiles" xsi:type="object">pageLayoutFileSourceBaseSorted</argument>
        <argument name="themeFiles" xsi:type="object">pageLayoutFileSourceThemeSorted</argument>
        <argument name="overrideBaseFiles" xsi:type="object">pageLayoutFileSourceOverrideBaseSorted</argument>
        <argument name="overrideThemeFiles" xsi:type="object">pageLayoutFileSourceOverrideThemeSorted</argument>
    </arguments>
</virtualType>

the one that looks of interest to us is pageLayoutFileSourceBaseSorted still in the same di.xml

<virtualType name="pageLayoutFileSourceBaseSorted" type="Magento\Framework\View\File\Collector\Decorator\ModuleDependency">
    <arguments>
        <argument name="subject" xsi:type="object">pageLayoutFileSourceBaseFiltered</argument>
    </arguments>
</virtualType>

Magento\Framework\View\File\Collector\Decorator\ModuleDependency does the following sorting

protected function getModulePriority($moduleName)
{
    if ($this->orderedModules === null) {
        $this->orderedModules = $this->moduleList->getNames();
    }
    $result = array_search($moduleName, $this->orderedModules);
    // Assume unknown modules have the same priority, distinctive from known modules
    if ($result === false) {
        return -1;
    }
    return $result;
}

where moduleList is based on Magento\Framework\Module\ModuleList which in turn uses the Loader mentioned way above.

4
  • 2
    +1 for good information, but that seems to leave an open question -- are layout handle XML files considered configuration files, or are they something else. Commented Feb 19, 2016 at 6:33
  • 3
    Commenting for google posterity -- the configuration loading does obey the sequence settings, but the sequence isn't set on a cache clear, its set on a module enable/disable. Commented Feb 19, 2016 at 23:42
  • 1
    Life saver! I took me quite a while before I noticed that this was happening.
    – user2450
    Commented Apr 22, 2016 at 11:48
  • And what about the layout-xml in the theme (app/design/AwesomeTheme/default/..? Are all these layout-xml's merged after de modules in the vendor and app/code locations, in the same order as explained hereabove? Commented Nov 28, 2018 at 15:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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