-
-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Expose config.xml//events/args
node to observers
#113
Conversation
Although I'm dumb because the example above could have just been this without the need for observer args at all. if ($controller->getRequest()->isAjax()) {
$controller->getResponse()->setBodyJson(['error' => true, 'message' => $errorMessage]);
return;
} But I think it's still a useful feature, so let's keep it open? |
If it's a useful feature, here's two things to discuss: 1. The $observer->getEvent()->args; // Varien_Event
$observer->args; // Varien_Event_Observer 2. Should the $observer->args->getMyProperty(); It might feel more "Magento" to access them on a Varien Object...? You can still use array access with Varien Objects, so |
I think it may be useful so I'd merge it as it is :-) |
@justinbeaty if you give me your green light I'll merge |
But first, which one looks best? public function verify(Varien_Event_Observer $observer): void
{
// A
$myVal = $observer->args['my_val'];
// B
$myVal = $observer->getEvent()->args['my_val'];
// C
$myVal = $observer->args->getMyVal();
// D
$myVal = $observer->getEvent()->args->getMyVal();
} |
😅 I would say D? isn't it possible to get observer params both via $observer->getXXX() and also $observer->getEvent()->getXXX() (which is usually what's used lately)? |
0ea28d8
to
61423c2
Compare
Varien_Profiler::start('OBSERVER: ' . $obsName); | ||
switch ($obs['type']) { | ||
case 'disabled': | ||
break; | ||
case 'object': | ||
case 'model': | ||
$method = $obs['method']; | ||
$observer->addData($args); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not sure if I'd remove this, wouldn't it break anybody's code that does $observer->getXXX() instead of $observer->getEvent()->getXXX()?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's added here:
maho/app/code/core/Mage/Core/Model/App.php
Line 1438 in 977a78f
...$args, // Mage::dispatchEvent() $args |
You are right. I had originally avoided that to not conflict with the args provided in Bonus that the PR now only affects one file, maho/app/code/core/Mage/Core/Model/App.php Lines 1431 to 1439 in 977a78f
Note it's possible to define an arg named I'm pretty sure this all works fine with the various ways developers pass Mage::dispatchEvent('...', ['obj' => new Varien_Object]);
// In observer:
$observer->getObj()->setFoo('123');
$observer->getEvent()->getObj()->setBar('456'); |
mmmm I've a ton of code that I've wrote without the getEvent() (I never understood why the data was duplicated so I chose the shortest notation to get the data) and it would also be very tricky for people to understand the change. is it worth it? |
I don't understand why the data is duplicate either, other than |
ah ok ok it you can still access it both way then :-) |
Yeah, unless there's some caveat on object reference that breaks when using the splat |
tested with a couple of modules that use events and couldn't find any problem, let's merge :-) |
There was at sometime support in
config.xml
for an<args>
node when defining event observers. Currently, this node is read from the XML file and stored in a property, but not available to the event observers in any way.People have come up with ways to get around this, such as using xpath to read the values in the observer, and patching
Mage_Core_Model_App
. Magento 2 removed the feature entirely.However, I think it's useful and can be enabled without any breaking changes. My first thought was since it's a Varien Object, we could just set whatever we decided to name the property:
But the
$event
object was already initialized with the$args
fromMage::dispatchEvent('event_name', [ ...$args ])
, so no matter what we named the data key, there is possibility to conflict.Instead I just used a
public array $args
, so there shouldn't be any conflicts.Example
In #109, we could do this:
And then in the Observer, consolidate both
verifyAjax()
andfailedVerification()
into a singleverify()
method: