Skip to content

Commit

Permalink
Use setData
Browse files Browse the repository at this point in the history
  • Loading branch information
justinbeaty committed Feb 25, 2025
1 parent fbc40d4 commit 0ea28d8
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 35 deletions.
35 changes: 33 additions & 2 deletions app/code/core/Mage/Core/Model/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -1360,6 +1360,38 @@ public function addEventArea($area)
}

/**
* Dispatch event to observers
*
* Default arguments can be defined in `config.xml` using the `<args>` node. These are merged with the $args parameter.
*
* For example, this defines `is_ajax=1` for the `controller_action_predispatch` event:
* ```xml
* <global>
* <events>
* <controller_action_predispatch>
* <observers>
* <my_event_observer>
* <class>Company_Name_Model_Observer</class>
* <method>process</method>
* <args>
* <is_ajax>1</is_ajax>
* </args>
* </my_event_observer>
* </observers>
* </controller_action_predispatch>
* </events>
* </global>
* ```
*
* In the observer method, `Company_Name_Model_Observer->process()`, access the args with:
* ```php
* public function process(Varien_Event_Observer $observer): void
* {
* $isAjax = (bool) $observer->getIsAjax();
* // ...
* }
* ```
*
* @param string $eventName
* @param array $args
* @return $this
Expand Down Expand Up @@ -1400,8 +1432,7 @@ public function dispatchEvent($eventName, $args)
}

foreach ($events[$eventName]['observers'] as $obsName => $obs) {
$event->args = $obs['args'];
$observer->setData(['event' => $event]);
$observer->setData([...$obs['args'], 'event' => $event]);
Varien_Profiler::start('OBSERVER: ' . $obsName);
switch ($obs['type']) {
case 'disabled':
Expand Down
33 changes: 0 additions & 33 deletions lib/Varien/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,39 +24,6 @@ class Varien_Event extends Varien_Object
*/
protected $_observers;

/**
* Array containing arguments from `config.xml`, or an empty array if the `<args>` node is missing or empty.
*
* This example defines an arg `is_ajax=1` for the `controller_action_predispatch` event:
* ```xml
* <global>
* <events>
* <controller_action_predispatch>
* <observers>
* <my_event_observer>
* <class>Company_Name_Model_Observer</class>
* <method>process</method>
* <args>
* <is_ajax>1</is_ajax>
* </args>
* </my_event_observer>
* </observers>
* </controller_action_predispatch>
* </events>
* </global>
* ```
*
* In the observer method, `Company_Name_Model_Observer->process()`, access the args with:
* ```php
* public function process(Varien_Event_Observer $observer): void
* {
* $isAjax = (bool) ($observer->getEvent()->args['is_ajax'] ?? null);
* // ...
* }
* ```
*/
public array $args;

/**
* Constructor
*
Expand Down

0 comments on commit 0ea28d8

Please sign in to comment.