Overriding Magento Event and Observer

Working with Magento Events and Observer are one of the best ways to extend the core functionality of the Magento Platform. Both Magento 1 and 2 provides the same events and observer customizations.

In this blog, we read on

Overriding Magento Event and Observer

Working with Magento Events and Observer are one of the best ways to extend the core functionality of the Magento Platform. Both Magento 1 and 2 provides the same events and observer customizations.

Overriding Magento Event and Observer

What is Magento Event?

Events are dispatched by Magento Modules whenever specific actions are triggered. As a result of its own events, Magento allows to create your own events that can be dispatched in your code.
When an event gets dispatched, it can pass data to any observer configured to which events it relates to. If an event is to be dispatched you have to call the dispatch function of the event manager class and provide it with the name you want to dispatch along with an array of data you wish to provide.

Dispatching Method

Dispatch Magento Events using the Magento\Framework\Event\Manager Class

What is Magento Observer?

Observers are some type of classes that includes general behaviour, performance or change business logic. The Observers gets executed whenever the event they are configured to watch is dispatched by the event manager. For creating an observer file, you must clas file under your module /Observer directory. Your Observer class should implement Magento\Framework\Event\ObserverInterface and define its execute function.

How to Override Magento Event and Observer?

As you know Magento comes with many functionalities which helps Magento developers to implement its core files bit easier.
Here we will have to override the Magento observers, when dealing with the extension’s conflicts or may have to disable or modify a part of some extension’s functionality.
Basically, there is no major difference between the observer’s overriding and overriding of any other regular model. In some cases, it's that not so easy to override.
There might be some sort of issues faced while working with a simple override.
Now Let's look into the steps which today's top Magento Development Company use for overriding Magento event browser.
First-of-all pay attention to the naming. Assume that we have some third-party extension which is named as Xyz_Extension, and this extension has the following observer:

class Xyz_Extension_Model_Observer
{
    public function xyzMethod( $observer )
    {
    // Code for an Event written here
        return $observer;
    }
}

This observer is catching some event using xyzMethod(), and it is described in the config.xml file as shown below :

    
        
            1.0.0
        
    
    
        
            
                Xyz_Extension_Model
            
        
        
            
                
                    
                        singleton
                        xyz_extension/observer
                        xyzMethod
                    
                
            
        
    

Now we should override observer’s method – xyzMethod().
Let’s prepare our extension for this stage:
We should make our extension dependent on the other extension (which is being overridden) in the Egits_Extension.xml. So first register our module in app/etc/modules as below:

&ltconfig&gt
    &ltmodules&gt
        &ltegits_extension&gt
            &ltactive&gttrue&lt/active&gt
            &ltcodepool&gtlocal&lt/codepool&gt
            &ltdepends&gt
                &ltxyz_extension&gt
            &lt/xyz_extension&gt&lt/depends&gt
        &lt/egits_extension&gt
    &lt/modules&gt
&lt/config&gt

Then create config.xml file within your module's etc directory. In this case, we can make a conventional model rewrite. We have to add the rewrite to the model’s node in config.xml:

&ltconfig&gt
    &ltmodules&gt
        &ltegits_extension&gt
            &ltversion&gt1.0.0&lt/version&gt
        &lt/egits_extension&gt
    &lt/modules&gt
    &ltglobal&gt
        &ltmodels&gt
            &ltegits_extension&gt
                &ltclass&gtEgits_Extension_Model&lt/class&gt
            &lt/egits_extension&gt
     &ltxyz_extension&gt
                &ltrewrite&gt
                    &ltobserver&gtEgits_Extension_Model_Rewrite_Observer&lt/observer&gt
                     &lt/rewrite&gt
           &lt/xyz_extension&gt
        &lt/models&gt
    &lt/global&gt
&lt/config&gt

Here is the rewritten observer file:

class Egits_Extension_Model_Rewrite_Observer extends Xyz_Extension_Model_Observer
{
    public function xyzMethod( $observer )
    {
        // Event's code rewritten here for our purpose
        return $observer;
    }
}

Now we should adjust the model call to use pseudonyms in the original observer’s identifier by rewriting it. The whole config.xml of our extension will look like this:

&ltconfig&gt
    &ltmodules&gt
        &ltegits_extension&gt
            &ltversion&gt0.1.0&lt/version&gt
        &lt/egits_extension&gt
    &lt/modules&gt
    &ltglobal&gt
        &ltmodels&gt
            &ltegits_extension&gt
                &ltclass&gtEgits_Extension_Model&lt/class&gt
            &lt/egits_extension&gt
            &ltxyz_extension&gt
                &ltrewrite&gt
                    &ltobserver&gtEgits_Extension_Model_Rewrite_Observer&lt/observer&gt
                &lt/rewrite&gt
            &lt/xyz_extension&gt
        &lt/models&gt
        &ltevents&gt
            &ltcore_block_abstract_prepare_layout_before&gt
                &ltobservers&gt
                    &ltxyz_extension_observer&gt
                        &lttype&gtsingleton&lt/type&gt
                        &ltclass&gtXyz_Extension_Model_Observer&lt/class&gt
                        &ltmethod&gtxyzMethod&lt/method&gt
                    &lt/xyz_extension_observer&gt
                &lt/observers&gt
            &lt/core_block_abstract_prepare_layout_before&gt
        &lt/events&gt
    &lt/global&gt
&lt/config&gt

This approach allows us to make the conventional rewrite using a safe way. Finally we would prefer to mention that, there was no interference into the Xyz_Extension code.
We hope that this article will help improve your skills in the programming work.
Thank you for reading our blog!

Leave a Reply

Your email address will not be published. Required fields are marked *