1

I'm having problems setting up an event. It gets triggered and I can access the event's params array, but I can't override the value like I can with Craft's default events.

MyPlugin_myServiceService.php

public function setTest()
{
    $test = true;

    $event = new Event($this, array(
        'test' => $test
    ));

    $this->onAfterSetTest($event);

    return $test;
}

public function onAfterSetTest(Event $event)
{
    $this->raiseEvent('onAfterSetTest', $event);
}

MyOtherPluginPlugin.php

craft()->on('myPlugin_myService.afterSetTest', function(Event $event)
{  
    $event->params['test'] = false;
});

Not sure what I'm doing wrong but craft()->myPlugin_myService->setTest() returns true.

2

It looks like it doesn't updated the variables passed to the event directly (referenced) and you have to access the value from the $event object.

public function setTest()
{
    // ...

    $test = $event->params['test']

    return $test;
}
| improve this answer | |
  • Sounds like you may need a custom hook, instead of an event. Generally speaking, events aren't supposed to modify the data which triggered it. – Lindsey D Jul 30 '15 at 19:15
  • Totally right, a hook is really the better solution to do this. Thank you! – carlcs Jul 31 '15 at 8:02
1

Thanks to Lindsey D's comment to my first answer, I had a closer look at custom hooks and found out that this is indeed the better solution to do this.

MyPlugin_myServiceService.php

public function setTest()
{
    $test = true;

    craft()->plugins->call('modifyTest', array(&$test));  

    return $test;
}

MyOtherPluginPlugin.php

public function modifyTest(&$test)
{
    $test = false;
}
| improve this answer | |

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.