3

Each extension can add observers, so one can end up with multiple <observers> for the same <event>.

Sometimes I need to be sure that all essential data is initialised when my observer function kicks in. So I want my observer to be called last of the bunch.

Or sometimes I want it to be first, or at least before core's observer, so that all the data important to me is there before the core observer saves it.

Can I influence the order in which observer functions for the same event are executed?

1

The only possibility to achieve this by declaring Module Dependency1 in your extension. Below I will show you the two cases which will help you to ORDER your observer in effective way.

Scenario

Let us call our module Namespace_Module. Suppose you are observing to an event some_example_event_to_observe. Now suppose below modules are also observing the same event.

  • Mage_Cms
  • Mage_Customer
  • Namespace_Module
  • SomeNamespace_SomeModule
  • SomeOtherComapny_SomeOtherModule

Please note, without any module dependency, Magento is going observe the event in the same listed order

Case - I

You want your module observer should trigger only after observer of SomeNamespace_SomeModule. For that case, your activation file will look like this.

File: app\etc\modules\Namespace_Module.xml
<config>
    <modules>
        <Namespace_Module>
            <active>true</active>
            <codePool>community</codePool>
            <depends>
                <SomeNamespace_SomeModule />
            </depends>
        </Namespace_Module>
    </modules>
</config>

See the <depend /> tag. We just specified there that, our module depends on SomeNamespace_SomeModule. Therefore Magento will process observer SomeNamespace_SomeModule first and then consider our observer.

Case - II

We want our observer to listen at last. In this case, our module's activation file will look like

File: app\etc\modules\Namespace_Module.xml
<config>
    <modules>
        <Namespace_Module>
            <active>true</active>
            <codePool>community</codePool>
            <depends>
                <SomeNamespace_SomeModule />
                <SomeOtherComapny_SomeOtherModule />
            </depends>
        </Namespace_Module>
    </modules>
</config>

Since we specified our module now depends on SomeNamespace_SomeModule and SomeOtherComapny_SomeOtherModule, our module is going to load last.

Case - III

We need to trigger our event very first. In that case

File: app\etc\modules\Namespace_Module.xml
<config>
    <modules>
        <Namespace_Module>
            <active>true</active>
            <codePool>community</codePool>
        </Namespace_Module>
        <Mage_Cms>
            <depends>Namespace_Module</depends>
        </Mage_Cms>
        <Mage_Customer>
            <depends>Namespace_Module</depends>
        </Mage_Customer>
    </modules>
</config>

Here we make our module as a dependency for the core modules Mage_Cms and Mage_Customer. This will force our module's observer to listen very first.

I hope this will make sense now. Let me know if you have any further doubts.


1 : Module dependency is a technique which is used in Magento for ordering loading of modules based on the dependency specified in the activation files (XML files which we can find inside app/etc/modules/)

  • 1
    Modules are loaded alphabetically, so you can also influence the load and execution order by renaming a module so that it is loaded before another. – paj Jul 9 '15 at 7:49
  • 1
    @paj true. But in almost all cases renaming an extension will be a bitter experience. So I strongly oppose that "trick". I feel it is bad. – Rajeev K Tomy Jul 9 '15 at 7:53

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.