4

I've got an event listener hooked up like this:

craft()->on('elements.populateElement', array($this, 'onPopulateElements'));

Is it possible to detach/deregister the listener? E.g. something like this (this doesn't work, but to illustrate):

craft()->off('elements.populateElement', array($this, 'onPopulateElements'));

2 Answers 2

2

Not possible currently, but there's a feature request.

0

Completely untested, and not sure if this will work with events registered with on, instead of the traditional Yii method, but try either:

craft()->detachEventHandler('elements.onPopulateElements', array($this, 'elements.onPopulateElements'));

or:

craft()->detachEventHandler('onPopulateElements', array($this, 'onPopulateElements'));

If neither of those work, then we'd need to add a custom off that takes into account the componentName.event event syntax that on users.

2
  • Thanks @Brad – unfortunately I wasn't able to make either of those work. The use case is that I only want/need the elements.onPopulateElement event handler to trigger one time (the event fires several times for most requests), so I figured the clean thing would be to detach the listener when the event fires the first time. I can put a flag in the event handler so no biggie, but an off method would be nice – I'll put up a feature request :) Commented Nov 29, 2015 at 19:29
  • Awesome... I was going to suggest a feature request, then. :)
    – Brad Bell
    Commented Nov 29, 2015 at 21:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.