0

I have a doubt regarding magento event observers. Suppose I have multiple modules that observe to same event. Suppose I made a particular module's event to load first. Then what will happen to event observers of other modules ? Magento simply neglect observers of other modules or every observers will execute in the loaded order ?

Kindly lighten me with your ideas.

Thanks in advance.

2

Well the order of execution of your observer methods will be same as the order of loading of modules in Magento.

Check this piece of code,

#File: app/code/core/Mage/Core/Model/Config.php
protected function _getDeclaredModuleFiles()
{
    $etcDir = $this->getOptions()->getEtcDir();
    $moduleFiles = glob($etcDir . DS . 'modules' . DS . '*.xml');

    if (!$moduleFiles) {
        return false;
    }

    $collectModuleFiles = array(
        'base'   => array(),
        'mage'   => array(),
        'custom' => array()
    );

    foreach ($moduleFiles as $v) {
        $name = explode(DIRECTORY_SEPARATOR, $v);
        $name = substr($name[count($name) - 1], 0, -4);

        if ($name == 'Mage_All') {
            $collectModuleFiles['base'][] = $v;
        } else if (substr($name, 0, 5) == 'Mage_') {
            $collectModuleFiles['mage'][] = $v;
        } else {
            $collectModuleFiles['custom'][] = $v;
        }
    }

    return array_merge(
        $collectModuleFiles['base'],
        $collectModuleFiles['mage'],
        $collectModuleFiles['custom']
    );
}

This , $moduleFiles = glob($etcDir . DS . 'modules' . DS . '*.xml'); creates a glob of all the module definition files in etc/modules.

From this,

            if ($name == 'Mage_All') {
                $collectModuleFiles['base'][] = $v;
            } else if (substr($name, 0, 5) == 'Mage_') {
                $collectModuleFiles['mage'][] = $v;
            } else {
                $collectModuleFiles['custom'][] = $v;
            }

We know that Mage_All is loaded first, then the Mage_ named files and last would be sequentially all files of custom modules in alphabetic order.

Once all the xml files are merged they are passed to this function

#File: app/code/core/Mage/Core/Model/Config.php
$moduleDepends = $this->_sortModuleDepends($moduleDepends);

Which will sort the load of modules as per dependency i.e the depends tag in the module definition file.

To cut the long story short, we can print the sequence in which the modules are loaded in our application,

 echo "<pre>";
 print_r(Mage::app()->getConfig()->getNode('modules')->children());

to check which module definition is being called first, and others subsequently.

Yes , so answering your question, magento will call all the observer methods associated with a particular event, and the sequence will be decided as per the call of particular module in which the observer class method is defined.

Hope this helps you in debugging in future.

  • Thanks for detailed answer. But my question didnt answer. I want to know whether magento neglect all other observer call except the first one which is called ? – Rajeev K Tomy Jun 9 '14 at 12:01
  • edited answer.. – huzefam Jun 9 '14 at 12:30
  • cheers @huzefam. Thanks for this wonderful answer – Rajeev K Tomy Jun 9 '14 at 12:55
1

Short answer is that every observer will execute in the loaded order. This is actually the charm of observers as you don't need to chain them, unless you want them to be aware of each other existence.

  • So every observers will observe by magento right ? – Rajeev K Tomy Jun 9 '14 at 12:02
  • When Magento dispatches an event, all observers associated with it get loaded and executed, one by one. – Pronto Jun 9 '14 at 12:07
  • Thanks for your answer. It cleared my doubt. but @huzfam gave a detailed answer. It will be more helpful for otherz. So I would like to accept his answer rather than yours. Pardon me. But I will surely UPVOTE you. Thanks again. – Rajeev K Tomy Jun 9 '14 at 12:11
  • 1
    No worries ... I would do the same. – Pronto Jun 9 '14 at 12:14
  • Good thumbs up.. friend :) – Rajeev K Tomy Jun 9 '14 at 12:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.