Leafiny Documentation
Events
Events are dispatched by modules when certain actions are triggered. In addition to its own events, Leafiny allows you to create your own events that can be dispatched in your code. When an event is dispatched, it can pass data to any observers configured to watch that event.
Dispatch a new event
Dispatch a new event with App::dispatchEvent
method:
App::dispatchEvent(
'my_event_name',
[
'custom_data_1' => 'azerty',
'custom_data_2' => ['Hello', 'World']
]
);
Observe an event
Observers are executed whenever the event they are configured to watch is dispatched by the event manager.
In a custom module config file, add an observer as follow:
/** modules/Vendor_Example/etc/config.php **/
'events' => [
'my_event_name' => [ // Dispatched event name to observe
'custom_and_unique_observer_name' => 100, // Custom and unique observer name with sort order as value
],
],
'observer' => [
'custom_and_unique_observer_name' => [ // The observer name
'class' => Example_Observer_DoSomething::class, // The observer class name
'custom_var' => 'Hello world!' // An optional custom variable to use in the observer
],
],
Observer class with execute
required method:
<?php
/** modules/Vendor_Example/app/Example/Observer/DoSomething.php **/
declare(strict_types=1);
/**
* Class Example_Observer_DoSomething
*/
class Example_Observer_DoSomething extends Core_Observer implements Core_Interface_Observer
{
/**
* Execute
*
* @param Core_Page|Leafiny_Object $object
*
* @return void
* @throws Exception
*/
public function execute(Leafiny_Object $object): void
{
$customVar = $this->getCustom('custom_var'); // Hello world!
$customData1 = $object->getData('custom_data_1'); // azerty
$customData2 = $object->getData('custom_data_2'); // ['Hello', 'World']
// Custom observer logic
}
}
Disable an observer
Disable a core module observer by setting null for the observer value in the Frontend or custom module configuration file:
'events' => [
'backend_object_save_after' => [
'log_object_save_after' => null,
],
],
In this exemple we disable admin object save log. The observer is declared in the Leafiny_Log
module.
Example
/** modules/Vendor_Example/etc/config.php **/
'events' => [
'page_render_before' => [
'observer_example' => 100,
],
],
'observer' => [
'observer_example' => [
'class' => Example_Observer_Christmas::class,
'is_enabled' => 1,
],
],
Observer class
<?php
/** modules/Vendor_Example/app/Example/Observer/Christmas.php **/
declare(strict_types=1);
/**
* Class Example_Observer_Christmas
*/
class Example_Observer_Christmas extends Core_Observer implements Core_Interface_Observer
{
/**
* Execute
*
* @param Leafiny_Object $object
*
* @return void
* @throws Exception
*/
public function execute(Leafiny_Object $object): void
{
if (!$this->getCustom('is_enabled')) {
return;
}
if (date('md') !== '1225') {
return;
}
/** @var Core_Page $page */
$page = $object->getData('object');
/* modules/Vendor_Example/template/christmas.twig */
$page->setCustom('template', 'Vendor_Example::christmas.twig');
}
}