6

According to magento devdocs

declares the list of components that must be loaded before the current component is loaded. It’s used for loading different kind of files: configuration files, view files (including CSS, Less, and template files), or setup classes. Note that does not affect the loading of regular classes (non-setup classes).

So if we define any same plugin(suppose before) for same function and with same order in different module(A and B). If module A depends on module B then should plugin A loads after B?

2

Yes, The sortOrder property for plugins determine when their before, after, or around methods get called when more than one plugins are observing the same method.

Let's say following plugins observing the same method with the following sort orders.

  • PluginA sortOrder 10
  • PluginB sortOrder 20

The execution flow will be as follows:

  • PluginA::beforeDispatch()
    • PluginA::aroundDispatch() (Magento calls the first half until callable)
  • PluginB::beforeDispatch()
    • PluginB::aroundDispatch() (Magento calls the first half until callable)
  • Action::dispatch()
    • PluginB::aroundDispatch() (Magento calls the Second half until callable)
  • PluginB::afterDispatch()
    • PluginA::aroundDispatch() (Magento calls the Second half until callable)
  • PluginA::afterDispatch()

If there's same sortorder in different module, then it will be load by module sequence, In your case A is dependent on B.

<?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_ComponentA" setup_version="0.0.1">
        <sequence>
        <!-- Vendor_ComponentA is dependent on Vendor_ComponentB: -->
            <module name="Vendor_ComponentB" />
        </sequence>
    </module>
</config>

UPDATED

More simpler way by Raphael at Digital Pianism Magento 2 cookbook

If there are multiple plugins that extend the same original function, they are executed in the following sequence:

  • the before plugin with the lowest sortOrder
  • the around plugin with the lowest sortOrder
  • other before plugins (from the lowest to highest sortOrder)
  • other around plugins (from the lowest to highest sortOrder)
  • the after plugin with the highest sortOrder
  • other after plugins (from the highest to the lowest sortOrder)
| improve this answer | |
1

When declaring a plugin you have the option to put a sortOrder element. Sort Order allows you to put a sequence in which a plugin should be called. I haven't tried using the same plugin with the same sort order from a different module. But I think it should work the way how your module depends on each other. In your case, plugin A loads after B.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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