30

In Magento 2, what are the pros and cons of using a plugin vs an observer to achieve something?

I understand that observers are subscribed to events whereas plugins can jump in before and/or after a public method being called on a Magento class, but surely they're coming very close to crossing paths now?

scrowler
  • 2,014
  • 1
  • 18
  • 42

3 Answers3

46

Plugins are omnipresent since it is possible to modify/replace the behavior of any public method in the system. Customizations should be done using plugins for public methods/classes marked with @api annotation (stable public API) whenever possible. Such approach guarantees that customization will stay functional after new Magento releases. In addition to before/after plugins mentioned in the question, it is possible to create around plugins to substitute original behavior.

On the other hand, observers are legacy extension mechanism inherited from Magento 1, it is pretty limited and should be avoided if possible. However, unlike plugins, they may provide extension points inside protected/private methods.

Rafael Corrêa Gomes
  • 13,309
  • 14
  • 84
  • 171
Alex Paliarush
  • 13,751
  • 5
  • 51
  • 55
  • Also take a look at answer talking about preferences vs plugins/observers: http://magento.stackexchange.com/a/94035/697 , may be useful. – Alex Paliarush Dec 16 '15 at 17:44
  • @alex:- how to write plugin for protected function, most of case we need to override protected function in that situation how to do it ? http://magento.stackexchange.com/questions/91353/how-to-override-or-extend-protected-or-parent-function-in-magneto2-using-plugin – Pradeep Kumar Dec 17 '15 at 03:43
  • @PradeepKumar plugins can be added to public methods only. The question you mentioned has an answer, but proposed solution is blocked but known issue (plugins cannot be applied to virtual types). As a temporary workaround you may declare plugin for framework URL class and there add conditional logic based on arguments (so that plugin does something on your case only) – Alex Paliarush Dec 17 '15 at 07:30
  • i jut gave one example , there is lot of function in protected in that case how to override, any way i there to override protected function – Pradeep Kumar Dec 17 '15 at 09:23
  • @PradeepKumar if you need to override a protected method you may need to extend the class and use preference/rewriting. Anyway, suggest you ask a question for it instead of in these comments – scrowler Dec 17 '15 at 18:02
  • Can I use observer to log or clean some data(cache, for example) or I should create a plugin? – Max Souza Mar 30 '21 at 14:45
6

According to Magento technical guide (https://devdocs.magento.com/guides/v2.1/coding-standards/technical-guidelines.html#14-events): All values (including objects) passed to an event MUST NOT be modified in the event observer. Instead, plugins SHOULD BE used for modifying the input or output of a function.

For me the main difference between plugins and observers is:

  1. Plugins can modify only public methods while observers can modify private, protected as well.
  2. There is sort order for plugins but there is no sort order for observers.
  3. You can add observer only to the events that are already dispatched in Magento. Plugins are more flexible here.
transversus
  • 416
  • 4
  • 4
  • I can also update an order with an observer though right? – scrowler May 01 '19 at 19:04
  • @RobbieAverill yes, you can create an observer for checkout_submit_all_after event. Your observer will be triggered after order is successfully placed. – transversus May 02 '19 at 23:31
  • Does that mean that “they do not modify data” is not true in that case? – scrowler May 03 '19 at 01:35
  • 1
    Yes you are correct @RobbieAverill Both plugins and observers can both modify data. For me the main difference between plugins and observers is:
    1. Plugins can modify only public methods while observers can modify private, protected as well.
    2. There is sort order for plugins but there is no sort order for observers.
    3. You can add observer only to the events that are already dispatched in Magento. Plugins are more flexible here.
    – transversus May 06 '19 at 19:12
  • @transversus, Simple and clear. Thank you. – Ravi Soni Feb 03 '20 at 04:14
  • Nice very well explained bout the difference between plugin and observer Thank you! – MazeStricks Jun 03 '20 at 04:37
-2

The question here is which approach to be used Plugins or events & Observers pattern.

When to use ?

  • plugins and observers both can be used to run our custom script after certain Magento 2 - events or public methods are executed.

Which one to use ?

  • When we need to modify Magento core functionality (Ex: adding additional data to order collection object) then we have to use plugins. In this case we can't use Events & Observer.

  • When we want to run our customization after certain event is dispatched without disturbing Magento Core functionality then we have to choose Event & Observer pattern.

user3512810
  • 111
  • 4