Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Pass arguments to observer method using <args> node in Magento's observer design pattern
<?php
class Namespace_Module_Helper_Data extends Mage_Core_Helper_Abstract {
/**
* Retrieve args node of observer config
*
* @param Varien_Event_Observer $observer
* @return array $observerArgs
*/
public function getObserverArgs(Varien_Event_Observer $observer, $callingClass, $callingMethod) {
/**
* Define vars
*/
$usedObservers = array();
$observerArgs = array();
$eventObservers = array();
/**
* Load Magento config
*/
$config = Mage::getConfig();
/**
* Retrieve all observers attached to the current observer's event
*/
$eventObservers = (array) $config->getXpath('//events/' . $observer->getEvent()->getName() . '/observers/*');
/**
* Retrieve all XML nodes of the current observer (including <args>!)
* and populate $usedObservers with observers that:
* - call the same class and method than the $observer passed as arguments for this function
* - have an <args> node declared in config
*/
foreach($eventObservers as $eventObserver) {
$className = $config->getModelClassName($eventObserver->class);
$method = $eventObserver->method;
$args = (bool) $eventObserver->args;
if($className == $callingClass && $method == $callingMethod && $args) {
$usedObservers[] = $eventObserver;
}
}
/**
* Create array of args
*/
foreach($usedObservers as $usedObserver) {
$args = (array) $usedObserver->args;
foreach($args as $name => $value) {
$observerArgs[$name] = $value;
}
}
$args = new Varien_Object;
$args->setData($observerArgs);
return $args;
}
}
@herveguetin

This comment has been minimized.

Copy link
Owner Author

@herveguetin herveguetin commented Aug 10, 2012

The observer is declared as shown below:

<events>
    <event_to_observe>
        <observers>
            <observer_name>
                <type>singleton</type>
                <class>Namespace_Module_Model_ObserverClass</class>
                <method>observerMethod</method>

                <args>
                    <arg_name>arg_value</arg_name>
                </args>

            </observer_name>
        </observers>
    </event_to_observe>
</events>

The helper method in the Gist is called as shown below from the observer method:

<?php
/**
 * Observer class
 */

class Namespace_Module_Model_ObserverClass {

    /**
     * Observer method
     * 
     * @param Varien_Event_Observer $observer
     * @return void
     */
    public function observerMethod(Varien_Event_Observer $observer) {

        $args = Mage::helper('ecmessenger')->getObserverArgs($observer, get_class($this), __FUNCTION__);
        //Mage::log($args, null, 'observer.log');
    }
}
@herveguetin

This comment has been minimized.

Copy link
Owner Author

@herveguetin herveguetin commented Aug 10, 2012

Quick correction on the above comment:
$args = Mage::helper('ecmessenger')->getObserverArgs($observer, get_class($this), FUNCTION);

is

$args = Mage::helper('module')->getObserverArgs($observer, get_class($this), FUNCTION);

@herveguetin

This comment has been minimized.

Copy link
Owner Author

@herveguetin herveguetin commented Aug 10, 2012

Grrr... Sorry...

$args = Mage::helper('module')->getObserverArgs($observer, get_class($this), __FUNCTION__);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment