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?

fris
  • 894
  • 6
  • 19
  • 2
    Check http://stackoverflow.com/questions/15934553/how-do-you-set-the-sort-order-for-event-observers-in-magento – mbalparda Jul 08 '15 at 02:48

1 Answers1

2

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/)

Rajeev K Tomy
  • 17,234
  • 6
  • 61
  • 103
  • 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 09 '15 at 07: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 09 '15 at 07:53