Overriding Magento observers

As you may know, we need to override the Magento observers when we deal with the extension’s conflicts or want to disable (or modify) a part of some extension’s functionality. Usually, there is no difference between the observer’s overriding and overriding of any other regular model. But sometimes we can face with the issues working with the simple override. How should we act then to keep it conventionally? Let’s check the following step by step instruction for Magento observers overriding. First of all, we should pay attention to the naming. Let’s assume that we have some 3rd party extension. It is named Some_Extension, and this extension has the following observer:
<?php
/**
 * file path:
 * magento_root/app/code/local/Some/Extension/Model/Observer.php
 */
 
class Some_Extension_Model_Observer
{
   public function someMethod( $observer )
   {
      /** 
       *Do some stuff here 
       */
         
      return $observer;
   }
}
?>

This observer is catching some event using someMethod(), and it is described in the extension’s config.xml file (note that we are using only the required parameters for this example):
<?xml version="1.0"?>


    
        
            1.0.0
        
    
    
        
            
                Some_Extension_Model
            
        
        
            
                
                    
                        singleton
                        some_extension/observer
                        someMethod
                    
                
            
        
    

When we already have a starting point, we need to override the observer’s method – someMethod(). Let’s prepare our extension for this stage:
<?xml version="1.0"?>


    
        
            true
            local
        
    

<?xml version="1.0"?>


    
        
            1.0.0
        
    
    
        
            
                Our_Extension_Model
            
        
    


In this case, we can make a conventional model rewrite. We need to add the rewrite to the model’s node in config.xml:

    
        Our_Extension_Model
    
    
        
            Our_Extension_Model_Rewrite_Observer
        
    


Here is the rewritten observer file itself:
<?php
/**
 * file path:
 * magento_root/app/code/local/Our/Extension/Model/Rewrite/Observer.php
 */
 
class Our_Extension_Model_Rewrite_Observer extends Some_Extension_Model_Observer
{
    public function someMethod( $observer )
    {
        /**
         * Our stuff here
         */
 
        return $observer;
    }
}
?>
By the way, recently we have faced with the observer declaration that can not be overridden that way:

    
        
            
                singleton
                Some_Extension_Model_Observer
                someMethod
            
        
    

Actually, there are two ways to reach our goal here. And in both ways we need to make our extension dependent on the other extension (which is being overridden) in the Our_Extension.xml:
<?xml version="1.0"?>


    
        
            true
            local
            
                
            
        
    

The first way is to disable the second extension’s observer and bind our observer to the same event instead. Add the events node to our extension’s config.xml with this content (the events node is included):

    
        
            
                disabled
            
            
                singleton
                Our_Extension_Model_Rewrite_Observer
                someMethod
            
        
    

Furthermore, the second way is to 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:
<?xml version="1.0"?>


    
        
            0.1.0
        
    
    
        
            
                Our_Extension_Model
            
            
                
                    Our_Extension_Model_Rewrite_Observer
                
            
        
        
            
                
                    
                        singleton
                        Some_Extension_Model_Observer
                        someMethod
                    
                
            
        
    

This approach allows us to make the conventional rewrite using a safe way. Moreover, the last thing that we would like to mention is that there was no interference into the Some_Extension code.

No comments:

Post a Comment