Control module load order in Magento
Free Preview
Learn how to control Magento 2 module load order using the sequence tag in module.xml for compatibility and customization.
Lesson Content
Sometimes you need your module to load before or after another one. This is crucial when working with Magento's extensible architecture to ensure your code properly extends or modifies existing functionality.
To make certain modules load before your module, use the sequence tag in your module's etc/module.xml file:
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Vendor_Module">
<sequence>
<module name="Magento_Catalog"/>
<module name="Foo_Bar"/>
</sequence>
</module>
</config>
This sequence affects several important aspects of module behavior:
- Class inheritance behavior
- Layout file processing order
- Plugin execution sequence
For example, if modules A and B both have plugins on the same method with identical sort orders, and B depends on A in its sequence, then B's plugin will run after A's plugin.
This load order becomes particularly important when you're customizing third-party extensions or overriding core Magento functionality. Getting the sequence wrong can lead to unexpected behaviors that are difficult to debug.
In cases where you’d like your code loaded before a third-party module’s code, you’ll need to use another approach. This could be adjusting plugin sort orders, creating class preferences, or applying patches with Composer.
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Vendor_Module">
<sequence>
<module name="Magento_Catalog"/>
<module name="Foo_Bar"/>
</sequence>
</module>
</config>
Join the discussion!
Comments
Want to comment on this lesson?
Enroll in this course to leave comments.