Fire custom events - good idea?

Permalink
Hey there,

I just read some resources about the concrete5 Events System. The in-code documentation suggests that this is an internal system which helps devs hook into system events.

Is it possible to fire custom events to let other devs hook into these events?

E.g. if I want to offer a package which should be extendable in certain ways?

Thanks in advance for suggestions!

Best regards,
Matthias

programmieraffe
 
mesuva replied on at Permalink Reply
mesuva
Yes, you can create your own events and let others use them. The eCommerce add-on for example has a handful of additional events it adds, so you definitely add new events in your packages.

I believe all you need to do is fire your event where it's appropriate in your code:
Events::fire('my_event_name', $val1, $val2);


Then anything that has extended that event will get fired. I don't believe you have to register/install an event beforehand or anything.

Have a read of this:
https://www.concrete5.org/documentation/how-tos/developers/how-concr...

I've not created any events myself, but I use concrete5's and eCommerce's events a huge amount and I'm REALLY glad they exist. So I'd encourage their use personally, as long as you pick appropriate event names that aren't going to clash or be confusing.
programmieraffe replied on at Permalink Reply
programmieraffe
Hey mesuva,

thanks very much pointing out the ecommerce package as an use-case example! I read about how system events work, but the manual did not really "encourage" me to fire custom events in my package ;)

Best regards,
Matthias
JohntheFish replied on at Permalink Reply
JohntheFish
Several of my addons fire custom events at critical points to facilitate third party integration.

Firing an event does carry an overhead, so you wouldn't, for example, want to fire one for each keypress in an editor (after sending it to the server in an ajax post) - that would be silly design in so many ways.

If there is no-one listening, the event overhead is fairly small.

Be wary that others listening to the events do not do so in any controllable order. So if an event depends on data returned by a listener, that could be hobbled by further listeners also returning data.

The event tester addon by mnkras is an essential tool if you are developing custom events.
programmieraffe replied on at Permalink Reply
programmieraffe
Thanks John for the additions! :-)