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 1 commit
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
151 changes: 151 additions & 0 deletions docs/plugins/event_listeners.rst
Original file line number Diff line number Diff line change
@@ -1,2 +1,153 @@
Event listeners
===============
ifeoluwafavour marked this conversation as resolved.
Show resolved Hide resolved

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

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

View workflow job for this annotation

GitHub Actions / prose

[vale] reported by reviewdog 🐶 [Google.WordList] Use 'capability' or 'feature' instead of 'functionality'. Raw Output: {"message": "[Google.WordList] Use 'capability' or 'feature' instead of 'functionality'.", "location": {"path": "docs/plugins/event_listeners.rst", "range": {"start": {"line": 4, "column": 145}}}, "severity": "WARNING"}
ifeoluwafavour marked this conversation as resolved.
Show resolved Hide resolved

.. 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
}
}
// ...


Subscribers
-----------
ifeoluwafavour marked this conversation as resolved.
Show resolved Hide resolved
The easiest way to listen to various events is to use an event subscriber. Read more about subscribers in `Symfony's documentation<http://symfony.com/doc/current/components/event_dispatcher/introduction.html#using-event-subscribers>`_.
ifeoluwafavour marked this conversation as resolved.
Show resolved Hide resolved

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 bundles's config file. See :ref:`plugins/config:Service config items` for more information on registering event services.

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.Spelling] Did you really mean 'bundles's'? Raw Output: {"message": "[Vale.Spelling] Did you really mean 'bundles's'?", "location": {"path": "docs/plugins/event_listeners.rst", "range": {"start": {"line": 57, "column": 203}}}, "severity": "ERROR"}
ifeoluwafavour marked this conversation as resolved.
Show resolved Hide resolved

Available Events

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

View workflow job for this annotation

GitHub Actions / prose

[vale] reported by reviewdog 🐶 [Google.Headings] 'Available Events' should use sentence-style capitalization. Raw Output: {"message": "[Google.Headings] 'Available Events' should use sentence-style capitalization.", "location": {"path": "docs/plugins/event_listeners.rst", "range": {"start": {"line": 59, "column": 1}}}, "severity": "WARNING"}
ifeoluwafavour marked this conversation as resolved.
Show resolved Hide resolved
----------------
ifeoluwafavour marked this conversation as resolved.
Show resolved Hide resolved
ifeoluwafavour marked this conversation as resolved.
Show resolved Hide resolved
There are many events available throughout Mautic. Depending on the desired functionality, look at the core bundle's *Event.php file in the root of the bundle. For example, Lead related events are defined and described in ``app\bundles\LeadBundle\LeadEvents.php``. The final classes provide the names of the events to listen to. Always use the event constants to ensure future changes to event names will not break the plugin.

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

View workflow job for this annotation

GitHub Actions / prose

[vale] reported by reviewdog 🐶 [Google.WordList] Use 'capability' or 'feature' instead of 'functionality'. Raw Output: {"message": "[Google.WordList] Use 'capability' or 'feature' instead of 'functionality'.", "location": {"path": "docs/plugins/event_listeners.rst", "range": {"start": {"line": 61, "column": 77}}}, "severity": "WARNING"}

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

View workflow job for this annotation

GitHub Actions / prose

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

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

View workflow job for this annotation

GitHub Actions / prose

[vale] reported by reviewdog 🐶 [Google.Spacing] 'e. F' should have one space. Raw Output: {"message": "[Google.Spacing] 'e. F' should have one space.", "location": {"path": "docs/plugins/event_listeners.rst", "range": {"start": {"line": 61, "column": 158}}}, "severity": "ERROR"}

Check warning on line 61 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 'Contact' instead of 'Lead'. Raw Output: {"message": "[Mautic.FeatureList] Is this referring to a Mautic feature? If so, use 'Contact' instead of 'Lead'.", "location": {"path": "docs/plugins/event_listeners.rst", "range": {"start": {"line": 61, "column": 175}}}, "severity": "INFO"}

Check warning on line 61 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 ('are defined'). Raw Output: {"message": "[Google.Passive] In general, use active voice instead of passive voice ('are defined').", "location": {"path": "docs/plugins/event_listeners.rst", "range": {"start": {"line": 61, "column": 195}}}, "severity": "INFO"}

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

View workflow job for this annotation

GitHub Actions / prose

[vale] reported by reviewdog 🐶 [Google.Spacing] 'o. A' should have one space. Raw Output: {"message": "[Google.Spacing] 'o. A' should have one space.", "location": {"path": "docs/plugins/event_listeners.rst", "range": {"start": {"line": 61, "column": 328}}}, "severity": "ERROR"}

Check warning on line 61 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 'won't' instead of 'will not'. Raw Output: {"message": "[Google.Contractions] Feel free to use 'won't' instead of 'will not'.", "location": {"path": "docs/plugins/event_listeners.rst", "range": {"start": {"line": 61, "column": 403}}}, "severity": "INFO"}

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

View workflow job for this annotation

GitHub Actions / prose

[vale] reported by reviewdog 🐶 [Google.Will] Avoid using 'will'. Raw Output: {"message": "[Google.Will] Avoid using 'will'.", "location": {"path": "docs/plugins/event_listeners.rst", "range": {"start": {"line": 61, "column": 403}}}, "severity": "WARNING"}

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

View workflow job for this annotation

GitHub Actions / prose

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

Check warning on line 61 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 'Plugin' instead of 'plugin'. Raw Output: {"message": "[Mautic.FeatureList] Is this referring to a Mautic feature? If so, use 'Plugin' instead of 'plugin'.", "location": {"path": "docs/plugins/event_listeners.rst", "range": {"start": {"line": 61, "column": 422}}}, "severity": "INFO"}
ifeoluwafavour marked this conversation as resolved.
Show resolved Hide resolved

Custom Events

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

View workflow job for this annotation

GitHub Actions / prose

[vale] reported by reviewdog 🐶 [Google.Headings] 'Custom Events' should use sentence-style capitalization. Raw Output: {"message": "[Google.Headings] 'Custom Events' should use sentence-style capitalization.", "location": {"path": "docs/plugins/event_listeners.rst", "range": {"start": {"line": 63, "column": 1}}}, "severity": "WARNING"}
ifeoluwafavour marked this conversation as resolved.
Show resolved Hide resolved
-------------
ifeoluwafavour marked this conversation as resolved.
Show resolved Hide resolved
A plugin can create and dispatch its own events.

Check warning on line 65 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 'Plugin' instead of 'plugin'. Raw Output: {"message": "[Mautic.FeatureList] Is this referring to a Mautic feature? If so, use 'Plugin' instead of 'plugin'.", "location": {"path": "docs/plugins/event_listeners.rst", "range": {"start": {"line": 65, "column": 3}}}, "severity": "INFO"}

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

View workflow job for this annotation

GitHub Actions / prose

[vale] reported by reviewdog 🐶 [Vale.Terms] Use 'Plugin' instead of 'plugin'. Raw Output: {"message": "[Vale.Terms] Use 'Plugin' instead of 'plugin'.", "location": {"path": "docs/plugins/event_listeners.rst", "range": {"start": {"line": 65, "column": 3}}}, "severity": "ERROR"}
ifeoluwafavour marked this conversation as resolved.
Show resolved Hide resolved

Custom events require the following:

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

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

View workflow job for this annotation

GitHub Actions / prose

[vale] reported by reviewdog 🐶 [Vale.Terms] Use 'Plugin' instead of 'plugin'. Raw Output: {"message": "[Vale.Terms] Use 'Plugin' instead of 'plugin'.", "location": {"path": "docs/plugins/event_listeners.rst", "range": {"start": {"line": 69, "column": 52}}}, "severity": "ERROR"}
ifeoluwafavour marked this conversation as resolved.
Show resolved Hide resolved

.. 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 will be created when the event is dispatched and should have any information listeners need to act on it.
ifeoluwafavour marked this conversation as resolved.
Show resolved Hide resolved

.. 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