-
Notifications
You must be signed in to change notification settings - Fork 29
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
base: 5.x
Are you sure you want to change the base?
Conversation
Documentation for Event Listeners component mautic#215
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks so much for the great work on this PR @ifeoluwafavour - it's fantastic to see these docs coming over to the new documentation!
I've left a bunch of suggestions to fix the many Vale suggestions from Reviewdog, have a look and see what you think!
Co-authored-by: Ruth Cheesley <[email protected]>
Co-authored-by: Ruth Cheesley <[email protected]>
Co-authored-by: Ruth Cheesley <[email protected]>
Co-authored-by: Ruth Cheesley <[email protected]>
Co-authored-by: Ruth Cheesley <[email protected]>
Co-authored-by: Ruth Cheesley <[email protected]>
Co-authored-by: Ruth Cheesley <[email protected]>
Co-authored-by: Ruth Cheesley <[email protected]>
Co-authored-by: Ruth Cheesley <[email protected]>
Co-authored-by: Ruth Cheesley <[email protected]>
referenced Symfony's docs event subscribers link file
Co-authored-by: Ruth Cheesley <[email protected]>
Hello @RCheesley I have applied the suggested changes. To accurately follow the external link syntax, I created a pull request to include Symfony's docs event subscribers link file to the link folder: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some tweaks to fix the heading nestings, and I've asked the devs to do a technical review, too! Thanks for the great work so far!
correct heading syntax Co-authored-by: Ruth Cheesley <[email protected]>
Co-authored-by: Ruth Cheesley <[email protected]>
Co-authored-by: Ruth Cheesley <[email protected]>
Co-authored-by: Ruth Cheesley <[email protected]>
changed to unique heading name instead of 'subscribers'
Thank you @RCheesley for your guidance so far! I changed 'Subscribers' to 'Event Sunscribers' in the recent commit. Is it a better heading name? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like just a vale fix and we're good to go! 🚀 Thanks @ifeoluwafavour for all the hard work here!
// ... | ||
|
||
|
||
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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
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 | ||
} | ||
} | ||
// ... |
There was a problem hiding this comment.
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:
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 | |
} | |
} | |
// ... |
***************** | ||
The easiest way to listen to various events is to use an event subscriber. Read more about subscribers in :xref:`symfony-event-subscribers`. | ||
|
||
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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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. |
/** | ||
* Class HelloWorldEvents | ||
*/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/** | |
* Class HelloWorldEvents | |
*/ |
The comment says the same thing as the class itself so it's not necessary
// ... | ||
|
||
|
||
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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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. |
<?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; | ||
} | ||
} |
There was a problem hiding this comment.
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+
<?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; | |
} | |
} |
Documentation for Event Listeners component #215