Magento 2 – Handling Multiple Modules Overriding the Same Template

layoutmagento2modulesequence

I am using Magento Commerce 2.2.6 running in developer mode.

I have multiple modules that are overriding the same template via the setTemplate action in layout xml. I am using sequence in the modules' module.xml files to control processing order with the intent of controlling which module will ultimately have it's template applied.

UPDATE: It seems Magento is not respecting the sequence specified in these modules. If I manually edit config.php and change the load order (B loads before A) then the modules load correctly/as expected. However, if I then disable and re-enable the modules via cli, Magento rewrites config.php and the order of the modules is switched back (A loads before B).

Below I have copied the module.xml and layout files from each of the conflicting modules:

Vendor_A module.xml:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
    <module name="Vendor_A" setup_version="1.0.0"/>
    <sequence>
        <module name="Vendor_B" />
        <module name="MageWorx_Downloads" />
    </sequence>
</config>

Vendor_A catalog_product_view_type_grouped.xml:

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <referenceBlock name="product.info.grouped.options">
        <action method="setTemplate">
            <argument name="template" xsi:type="string">Vendor_A::product/view/type/grouped.phtml</argument>
        </action>
    </referenceBlock>
</page>

Vendor_B module.xml:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
    <module name="Vendor_B" setup_version="1.0.0"/>
    <sequence>
        <module name="Aitoc_GroupedProductsOptions" />
    </sequence>
</config>

Vendor_B catalog_product_view_type_grouped.xml:

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <referenceBlock name="product.info.grouped.options">
        <action method="setTemplate">
            <argument name="template" xsi:type="string">Vendor_B::product/view/type/grouped.phtml</argument>
        </action>
    </referenceBlock>
</page>

It is my expectation that Vendor_A would have it's template applied because the sequencing specifies for it to be loaded after Vendor_B. However, the opposite is true.

If I comment out the setTemplate action in Vendor_B, then Vendor_A has it's template applied so it seems everything works except sequencing.

Does sequencing not work in the way I think it does or am I making some other erroneous error?

Best Answer

I had a typo in my module.xml files. The first module node in:

<module name="Vendor_A" setup_version="1.0.0"/>
<sequence>
    <module name="MageWorx_Downloads" />
    <module name="Vendor_B" />
</sequence>

should not be self-closing and instead should be written as

<module name="Vendor_A" setup_version="1.0.0">
    <sequence>
        <module name="MageWorx_Downloads" />
        <module name="Vendor_B" />
    </sequence>
</module>

Sequencing now works as expected.