Declaring Observers in Magento 2.0 (Updated)

Declaring Observers in Magento 2.0 (Updated)

Registration and creation of observers in Magento 2.x is a bit different from the version 1.x.

  • a separate events.xml file is used for registering observers;
  • the scope of the observer is defined not by the xml node, but by the location of the events.xml file;
  • XML syntax is slightly different from the version 1.x.

Let’s take a closer look at these differences using our module Age Verification Page as an example.

Unlike in Magento 1.x where all events are registered in the common config module, in Magento 2.x all module observers are stored in a separate xml file under the following path:

app/code/BelVG/Verpage/etc/events.xml(app/code/BelVG/Verpage/etc/adminhtml/events.xml

or

app/code/BelVG/Verpage/etc/frontpage/events.xml

Let’s take a look at the xml file:

The attribute ‘name’ in the ‘event’ node defines the event when the observer should be called.

The “observer” node defines the observer itself and its attributes:

  • name – the observer registration name (it is important that the names do not coincide);
  • instance – the class, which method will be executed when a specific even occurs;
  • method – the method being executed.

Let’s take a look at the observer (app/code/BelVG/Verpage/Model/Observer.php):

As you see, there have been very few changes compared to Magento 1.x except for the class names. In our case the function ‘verificate’ receives the current observer object as an argument.

In the beginning we also mentioned one more important difference – the definition of the scope of the observer. Unlike in Magento 1.x:

The file config.xml:

In such case Magento 1.x defines the observer only on the frontend.

In Magento 2.x the scope of the observer is defined by the location of the events.xml file in the specific folder.

Frontend location: app/code/Namespace/Modulename/etc/frontend/
Admin location: app/code/Namespace/Modulename/etc/adminhtml/

If the observer is required both on the frontend and in the admin panel: app/code/Namespace/Modulename/etc/

 

Update

In the final version of Magento Community Edition (CE), several changes have been included:

app/code/BelVG/Infscroll/etc/frontpage/events.xml (for example, Infinite Scroll extension)

The “method” parameter has been deleted and now observers represent something like a controller (that has an additional “execute” function).
Accordingly, the following changes in the observer have been made:

  1. All observers have been moved to the root catalog of the extension
  2. The observers are now made similarly to controllers.

Example: app/code/BelVG/Infscroll/Observer/AjaxRenderBefore.php

namespace BelVG\Infscroll\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\DataObject as Object;



7 comments

  1. Thank you for such useful post.
    Here, I have one pretty question I have.

    In magento 2 where I can get list of events/observers?

    Thanks,
    Praful

  2. There is a change in observers.
    Now you should not declare the attribute method and they automatically dispatch the method execute.

    Regards

  3. Hi andrei,
    I tried this observer in magento2 with putting logs in the observer method, but it seems observer cant firing in my method my events and observer function are

    _registry = $registry;
    $this->_logger = $logger;

    }

    public function message(\Magento\Framework\Event\Observer $observer)
    {
    $this->_logger->addInfo(‘some text or variable’);
    //$logger->info(‘Informational message’);
    }

    }

    please check it and let me know whats the problem here. I cant able to see any logs also

  4. Hi Karthick, make sure you are doing something like this to use logger in your observer:

    protected $_logger;
    public function __construct(
    \Psr\Log\LoggerInterface $logger
    ) {
    $this->_logger = $logger;
    }

    and then call it: $this->_logger->info('some message here',true);

  5. Firstly to let you know that the namespace and use statements in the update are not in the ‘code’ formatting :)

    Also thank you for posting as this helped me fix a 3rd party extension which had not been updated.

    Nice one!

Post a new comment

top
BelVG Newsletter
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.
Email *