Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update event_listeners.rst #220

Open
wants to merge 19 commits into
base: 5.x
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
156 changes: 155 additions & 1 deletion docs/plugins/event_listeners.rst
Original file line number Diff line number Diff line change
@@ -1,2 +1,156 @@
Event listeners
===============
###############

Mautic leverages Symfony's EventDispatcher to execute and communicate various actions through Mautic. Plugins can hook into these to extend the capability of Mautic. Refer to the Extending Mautic section of the documentation for some of the ways to do this.

.. code-block:: php

<?php
//plugins\HelloWorldBundle\EventListener\LeadSubscriber

namespace MauticPlugin\HelloWorldBundle\EventListener;

use Mautic\LeadBundle\Event as Events;
use Mautic\LeadBundle\LeadEvents;

/**
* Class LeadSubscriber
*
* @package Mautic\LeadBundle\EventListener
*/
class LeadSubscriber extends CommonSubscriber
{

/**
* @return array
*/
static public function getSubscribedEvents()
{
return array(
LeadEvents::LEAD_POST_SAVE => array('onLeadPostSave', 0),
LeadEvents::LEAD_POST_DELETE => array('onLeadDelete', 0)
);
}

public function onLeadPostSave(LeadEvent $event)
{
$lead = $event->getLead();

// do something
}

public function onLeadDelete(LeadEvent $event)
{
$lead = $event->getLead();

$deletedId = $lead->deletedId;

// do something
}
}
// ...
Comment on lines +11 to +51
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A little bit more modern PHP:

Suggested change
namespace MauticPlugin\HelloWorldBundle\EventListener;
use Mautic\LeadBundle\Event as Events;
use Mautic\LeadBundle\LeadEvents;
/**
* Class LeadSubscriber
*
* @package Mautic\LeadBundle\EventListener
*/
class LeadSubscriber extends CommonSubscriber
{
/**
* @return array
*/
static public function getSubscribedEvents()
{
return array(
LeadEvents::LEAD_POST_SAVE => array('onLeadPostSave', 0),
LeadEvents::LEAD_POST_DELETE => array('onLeadDelete', 0)
);
}
public function onLeadPostSave(LeadEvent $event)
{
$lead = $event->getLead();
// do something
}
public function onLeadDelete(LeadEvent $event)
{
$lead = $event->getLead();
$deletedId = $lead->deletedId;
// do something
}
}
// ...
namespace MauticPlugin\HelloWorldBundle\EventListener;
use Mautic\LeadBundle\LeadEvent;
use Mautic\LeadBundle\LeadEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
final class LeadSubscriber extends EventSubscriberInterface
{
static public function getSubscribedEvents(): array
{
return [
LeadEvents::LEAD_POST_SAVE => ['onLeadPostSave', 0],
LeadEvents::LEAD_POST_DELETE => ['onLeadDelete', 0],
];
}
public function onLeadPostSave(LeadEvent $event): void
{
$lead = $event->getLead();
// do something
}
public function onLeadDelete(LeadEvent $event): void
{
$lead = $event->getLead();
$deletedId = $lead->deletedId;
// do something
}
}
// ...


Event subscribers
*****************
The easiest way to listen to various events is to use an event subscriber. Read more about subscribers in :xref:`symfony-event-subscribers`.

Check failure on line 55 in docs/plugins/event_listeners.rst

View workflow job for this annotation

GitHub Actions / prose

[vale] reported by reviewdog 🐶 [Vale.Terms] Use 'Symfony' instead of 'symfony'. Raw Output: {"message": "[Vale.Terms] Use 'Symfony' instead of 'symfony'.", "location": {"path": "docs/plugins/event_listeners.rst", "range": {"start": {"line": 55, "column": 114}}}, "severity": "ERROR"}

Plugin event subscribers can extend ``\Mautic\CoreBundle\EventListener\CommonSubscriber`` which gives access to commonly used dependencies and also allows registering the subscriber service through the config file for the bundle. See :ref:`plugins/config:Service config items` for more information on registering event services.

Check warning on line 57 in docs/plugins/event_listeners.rst

View workflow job for this annotation

GitHub Actions / prose

[vale] reported by reviewdog 🐶 [Mautic.FeatureList] Is this referring to a Mautic feature? If so, use 'Plugins' instead of 'plugins'. Raw Output: {"message": "[Mautic.FeatureList] Is this referring to a Mautic feature? If so, use 'Plugins' instead of 'plugins'.", "location": {"path": "docs/plugins/event_listeners.rst", "range": {"start": {"line": 57, "column": 241}}}, "severity": "INFO"}

Check failure on line 57 in docs/plugins/event_listeners.rst

View workflow job for this annotation

GitHub Actions / prose

[vale] reported by reviewdog 🐶 [Vale.Terms] Use 'Plugins' instead of 'plugins'. Raw Output: {"message": "[Vale.Terms] Use 'Plugins' instead of 'plugins'.", "location": {"path": "docs/plugins/event_listeners.rst", "range": {"start": {"line": 57, "column": 241}}}, "severity": "ERROR"}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Plugin event subscribers can extend ``\Mautic\CoreBundle\EventListener\CommonSubscriber`` which gives access to commonly used dependencies and also allows registering the subscriber service through the config file for the bundle. See :ref:`plugins/config:Service config items` for more information on registering event services.
Plugin event subscribers can extend ``Symfony\Component\EventDispatcher\EventSubscriberInterface`` which gives access to commonly used dependencies and also allows registering the subscriber service through autowiring.


Available events
****************



There are many events available throughout Mautic. Depending on what you're trying to implement, look at the ``*Event.php`` for the core bundle, located in the root of the bundle. For example, the ``app\bundles\LeadBundle\LeadEvents.php`` file defines and describes events relating to Contacts. The final classes provide the names of the events to listen to. Always use the event constants to ensure future changes to event names won't break the Plugin.

Custom events
*************
A Plugin can create and dispatch its own events.

Custom events require the following:

1) The class defining the available events for the Plugin using a ``final class`` with constants.

.. code-block:: php

<?php
// plugins\HelloWorldBundle\HelloWorldEvents.php

namespace MauticPlugin\HelloWorldBundle;

/**
* Class HelloWorldEvents
*/
Comment on lines +81 to +83
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/**
* Class HelloWorldEvents
*/

The comment says the same thing as the class itself so it's not necessary

final class HelloWorldEvents
{
/**
* The helloworld.armageddon event is dispatched when a world is doomed by a giant meteor
*
* The event listener receives a MauticPlugin\HelloWorldBundle\Event\ArmageddonEvent instance.
*
* @var string
*/
const ARMAGEDDON = 'helloworld.armageddon';
}
// ...


2) The Event class that is received by the listeners. This class should extend ``Symfony\Component\EventDispatcher\Event``. It's created when the event is dispatched and should have any information listeners need to act on it.

Check warning on line 98 in docs/plugins/event_listeners.rst

View workflow job for this annotation

GitHub Actions / prose

[vale] reported by reviewdog 🐶 [Google.Contractions] Feel free to use 'that's' instead of 'that is'. Raw Output: {"message": "[Google.Contractions] Feel free to use 'that's' instead of 'that is'.", "location": {"path": "docs/plugins/event_listeners.rst", "range": {"start": {"line": 98, "column": 20}}}, "severity": "INFO"}

Check warning on line 98 in docs/plugins/event_listeners.rst

View workflow job for this annotation

GitHub Actions / prose

[vale] reported by reviewdog 🐶 [Google.Passive] In general, use active voice instead of passive voice ('is received'). Raw Output: {"message": "[Google.Passive] In general, use active voice instead of passive voice ('is received').", "location": {"path": "docs/plugins/event_listeners.rst", "range": {"start": {"line": 98, "column": 25}}}, "severity": "INFO"}

Check warning on line 98 in docs/plugins/event_listeners.rst

View workflow job for this annotation

GitHub Actions / prose

[vale] reported by reviewdog 🐶 [Google.Passive] In general, use active voice instead of passive voice ('is dispatched'). Raw Output: {"message": "[Google.Passive] In general, use active voice instead of passive voice ('is dispatched').", "location": {"path": "docs/plugins/event_listeners.rst", "range": {"start": {"line": 98, "column": 153}}}, "severity": "INFO"}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
2) The Event class that is received by the listeners. This class should extend ``Symfony\Component\EventDispatcher\Event``. It's created when the event is dispatched and should have any information listeners need to act on it.
2) The Event class received by the listeners. This class should extend ``Symfony\Component\EventDispatcher\Event``. It's created when dispatching the event and should have any information listeners need to act on it.

Vale recommendations

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
2) The Event class that is received by the listeners. This class should extend ``Symfony\Component\EventDispatcher\Event``. It's created when the event is dispatched and should have any information listeners need to act on it.
2) The Event class that is received by the listeners. This class should extend ``Symfony\Contracts\EventDispatcher\Event``. It's created when the event is dispatched and should have any information listeners need to act on it.


.. code-block:: php

<?php
// plugins\HelloWorldBundle\Event\ArmageddonEvent.php

namespace MauticPlugin\HelloWorldBundle\Event;

use Symfony\Component\EventDispatcher\Event;
use MauticPlugin\HelloWorldBundle\Entity\World;

class ArmageddonEvent extends Event
{
/** @var World */
protected $world;

/** @var bool */
protected $falseAlarm = false;

public function __construct(World $world)
{
$this->world = $world;
}

public function shouldPanic()
{
return ('earth' == $this->world->getName());
}

public function setIsFalseAlarm()
{
$this->falseAlarm = true;
}

public function getIsFalseAlarm()
{
return $this->falseAlarm;
}
}
Comment on lines +102 to +137
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Modern PHP with types and updated Symfony namespace for Symfony 5+

Suggested change
<?php
// plugins\HelloWorldBundle\Event\ArmageddonEvent.php
namespace MauticPlugin\HelloWorldBundle\Event;
use Symfony\Component\EventDispatcher\Event;
use MauticPlugin\HelloWorldBundle\Entity\World;
class ArmageddonEvent extends Event
{
/** @var World */
protected $world;
/** @var bool */
protected $falseAlarm = false;
public function __construct(World $world)
{
$this->world = $world;
}
public function shouldPanic()
{
return ('earth' == $this->world->getName());
}
public function setIsFalseAlarm()
{
$this->falseAlarm = true;
}
public function getIsFalseAlarm()
{
return $this->falseAlarm;
}
}
<?php
// plugins\HelloWorldBundle\Event\ArmageddonEvent.php
namespace MauticPlugin\HelloWorldBundle\Event;
use Symfony\Contracts\EventDispatcher\Event;
use MauticPlugin\HelloWorldBundle\Entity\World;
final class ArmageddonEvent extends Event
{
private bool $falseAlarm = false;
public function __construct(private World $world)
{
}
public function shouldPanic(): bool
{
return ('earth' == $this->world->getName());
}
public function setIsFalseAlarm(): void
{
$this->falseAlarm = true;
}
public function getIsFalseAlarm(): bool
{
return $this->falseAlarm;
}
}

// ...


3) The code that dispatches the event where appropriate using the ``event_dispatcher`` service.

.. code-block:: php

<?php

$dispatcher = $this->get('event_dispatcher');
if ($dispatcher->hasListeners(HelloWorldEvents::ARMAGEDDON)) {
$event = $dispatcher->dispatch(HelloWorldEvents::ARMAGEDDON, new ArmageddonEvent($world));

if ($event->shouldPanic()) {
throw new \Exception("Run for the hills!");
}
}


Loading