Creating a subscriber
Create your subscriber
In Launchpad there are two ways to create a subscriber.
This is due to the fact the classic way provides more security preventing the developer from making certain mistakes but the counterpart is on performance as the reflection API is used in that method.
The classic way
To create a subscriber the classic way there is nothing easier.
Just create a class.
Then for each callback create first a public method and then add the docblock @hook
with the name of the event to register on:
It is possible to add a priority by adding a number after the event the following way:
Concerning the number of parameters this is done automatically by the framework and there is no need to indicate it.
The optimized way
In Launchpad every optimized subscriber needs to implements the interface Launchpad\Dependencies\LaunchpadCore\EventManagement\SubscriberInterface
.
Due to that your subscriber should look like that:
Once you created the base from your subscriber you will then have to callbacks to the subscriber.
For that inside the get_subscribed_events
method we will have to return an array containing the mapping between callbacks from the subscriber and events.
To create a callback you just need to create a public function with the parameters you need:
Then if it is a filter you will have to return a value:
Finally you will have to register your callback to the event for that there is multiple syntaxes:
The easiest way is to just register the callback:
'hook_name' => 'method_callback'
.If you need to register the callack with a priority or more than one param, you can then use the array syntax:
'hook_name_3' => [ 'method_callback_3', 10, 2 ],
If you have multiple callbacks then you will have to use the array syntax wrapped into an array:
'hook_name_4' => [ [ 'method_callback_4' ], [ 'method_callback_5' ], ],
At the end you should have this structure for your subscriber:
Register your subscriber
Once we created your subscriber we need to then attach it to our plugin as an actual subscriber because for the moment is a simple class for the framework.
For that we will need go register it on a ServiceProvider as a subscriber.
With Launchpad default behavior we have 4 subscriber types:
Common subscribers: Subscribers that load on any context.
Administrative subscribers: Subscribers that load only when the admin dashboard is loaded.
Front-end subscribers: Subscribers that load only on pages visible by regular users.
Initialisation subscribers: Subscribers loading before other to modify the loading logic.
To define the type from we need to register the subscriber in the method matching the right type:
Last updated