TL;DR: Can I register hook functions in a custom panel field, or do I have to use a plugin?
I wrote a (relatively) complex panel field to manage participants of events.
There are a few cases when I have to send out emails (money has been paid, forms have been handed in, …). The current code stays very close to the existing controller methods of the structure field:
When the user confirms the form, the structure entry gets edited and saved. When this has happened, I need to send my email. Instead of pasting the same email code everywhere, I thought about using the hook system and trigger a custom event. This works perfectly fine.
The only problem is the registration of the listener: as far as I know, this has to be done in a plugin. However, I would like to keep it in the field definition, as it will be needed anywhere else. Is there a way this can be achieved?
I think you don’t need a hook but simply a public static method that can be called from your field methods. You can then call this method from anywhere and pass the email parameters to it.
Of course that works; I guess I was just excited to use an event driven system. If there is no easy way to register the hooks, this exactly what I’ll end up using.
But I think that events aren’t that useful here. You only have one function that sends emails, so it’s not a many-to-many relationship. But if you’d rather use hooks anyway or if I forgot something, go ahead.
And where would you put that code so that the event listener is actually defined before the event might be triggered? The only thing that I could come up with the constructor of my field controller, but that doesn’t seem to be the cleanest solution either.
The rationale behind going for events/hooks was that in general there will be more things happening than just sending emails. Imagine the event running out of places, spots freeing up, … They trigger different things obviously, and I like modeling such stuff with events when I can.
Do you have any further information for me here to this solution @lukasbestle ? I want to create a select field (duplicated from the standard select field) which triggers whenever a certain value of the select field is saved.