Please upgrade here. These earlier versions are no longer being updated and have security issues.
HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.
How to create custom event hooks?
pavsid
New
Hi, I wondered if someone could help me. I want to create an event hook/handler and have been reading the docs here and here but am getting nowhere.
I'd like to hook into the Gdn_Email::SendMail event, with the intention of wrapping a template around email content.
So far I have created a class..
class MyHooks extends Gdn_Plugin { public function Base_sendmail_handler($sender, $args) { var_dump($sender); die; } }
But it doesn't seem to be getting called. Any ideas?
0
Answers
I've tried adding...
..to my theme hooks class as well but still getting nothing
You cannot create events via a plugin method name. You can hook into events via a plugin method name.
Check out the Eventi addon. It hooks into every event and spits out some markup detailing the event name and arguments.
Search first
Check out the Documentation! We are always looking for new content and pull requests.
Click on insightful, awesome, and funny reactions to thank community volunteers for their valuable posts.
Hi @hgtonight - i'm not trying to create an event, i want to create an event hook/handler. I've checked out the Eventi addon but it's not really helping me here as the
Gdn_Email::SendMail
event i'm testing this on is triggered by posting a comment on someone's wall, which is done by AJAX, and doesn't work at all if i enable that plugin.Any ideas how I format my function name so that it is recognised as the handler for
Gdn_Email::SendMail
?Your second version is more correct. But as far as I can see, it will fail nevertheless. In class.email.php there is this code:
Looking through the code, the send() method is nearly always called without passing any parameter, so I guess the "SendMail" Event is never fired. If you want to make sure you never miss it, you would have to create a
public function base_sendMail_handler($sender, $args) {
function in your themehooks.But even if your code gets called, it might be quite useless. You can only alter what has been passed as an "EventArgument" and none of the critical information (nearly no information at all!) has been passed here.
So wrapping all emails will not work that way. Maybe you could "override" the class with that factory-something (ask hgtonight since he knows about things like that) but that would be no fun.
You could also make a pull request on GitHub so that this event would be useful, maybe something like
And you could already implement that in your own copy of Vanilla, always needing to remember, that you have to check this after any updates.
This is not required as the PhpMailer is a public member of the Gdn_Email class which is sent as the
$Sender
object.Sorry for misunderstanding you.
This will work with the caveats R_J pointed out above.
Considering nothing changes between
beforeSendMail
andsendMail
except phpMailer configurations, I would use thebeforeSendMail
event.Hope this helps.
Search first
Check out the Documentation! We are always looking for new content and pull requests.
Click on insightful, awesome, and funny reactions to thank community volunteers for their valuable posts.
Dammit, seems that the
$this->fireEvent('BeforeSendMail');
event was only added in v2.2. Guess I could just add this event myself.However, I see that there's reference to
email.master
masterview, and although the setter method for this value is down as a TODO, I wonder if i could just edit theemail.master
itself to my liking? I've tried searching for this file though and can't find it anywhere.