Does Module Sequence Impact Layout Handle XML Loading Order in Magento 2?

configurationlayoutmagento2

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.

Best Answer

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.

Related Topic