0

How can a developer prioritize a plugin's execution, if possible?

A. The developer can use sortOrder property by specifying a higher value than the target plugin.

B. This cannot be achieved as the plugins are always executed by their module's load order in app/etc/config.php file

C. The developer can use sortOrder property by specifying a lower value than the target plugin.

As per my understanding (Plugin executed in Asc Order, so the lower one priority over the targeted one), the answer should be option C but in one of the untrusted resource it's showing option A as answer.

Please confirm the correct answer.

2 Answers 2

1

Prioritizing rules are as follows:

  1. Before executing the original method, Magento will run before plugins from the smallest to the biggest value in sortOrder.
  2. The part of the plugin around code will also be executed from the smallest to the biggest value before calling the original method (callable).
  3. The after plugin is called from the biggest to the smallest after calling the original method. Bottom line answer is c
2
  • Correct Abu, the Answer should be C. Thanks! Commented Dec 17, 2022 at 9:40
  • Thanks @WakarAhamad , can you please accepted it. Commented Dec 17, 2022 at 10:09
1

The correct answer is C.

That means the plugin with lower sortOrder will execute first, and the plugin with higher sortOrder will execute after that. So if you want to override other plugins, you should make sure sortOrder in your plugin is highest.

The sortOrder property from the plugin node declared in di.xml determines the plugin's prioritization when more than one plugin is observing the same method.

The Magento\Framework\Interception\PluginListInterface which is implemented by Magento\Framework\Interception\PluginList\PluginList is responsible to define when to call the before, around, or after methods respecting this prioritization.

If two or more plugins have the same sortOrder value or do not specify it, the component load order declared in the sequence node from module.xml and area will define the merge sequence. Check the component load order in app/etc/config.php file.

The application executes plugins using these rules during each plugin execution in two main flows:

  • Before the execution of the observed method, starting from lowest to highest sortOrder.
    • The application executes the current plugin’s before method.
    • Then the current plugin's around method is called.
      • The first part of the plugin's around method is executed.
      • The around method executes the callable.
        • If there is another plugin in the chain, all subsequent plugins are wrapped in an independent sequence loop and the execution starts another flow.
        • If the current plugin is the last in the chain, the observed method is executed.
      • The second part of the around method is executed.
    • The application moves on to the next plugin.
  • Following the execution flow, starting from lowest to highest sortOrder in the current sequence plugins loop.
    • The current plugin's after method is executed.
    • The application moves on to the next plugin.

As a result of these rules, the execution flow of an observed method is affected not only by the prioritization of the plugins, but also by their implemented methods.

You can read more about Prioritizing plugins and many scenarios from the official document: https://developer.adobe.com/commerce/php/development/components/plugins/#prioritizing-plugins

You also can write code and test it in your Magento project.

3
  • I appreciate you for very deep explanation but still I am not sure how the Option A could be the Answer. For example suppose a Plugin already executing with SortOrder=20 and if we want to prioritize other plugin 'XYZ' over this then XYZ SortOrder should be less than 20 not greater than 20. what do you think ? Commented Dec 17, 2022 at 9:40
  • Yes, assume you have 2 plugins A and B that modify a method. Plugin A has sortOrder = 20 and B has sortOrder = 19. Magento will execute plugin B first, then execute plugin A.
    – Tu Van
    Commented Dec 17, 2022 at 9:46
  • @WakarAhamad I was misunderstood a little about the question, I've updated the answer for your question is C with more details in the latest updated answer.
    – Tu Van
    Commented Dec 17, 2022 at 9:47

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.