... an event is an action or occurrence recognised by software that may be handled by the software.
https://en.wikipedia.org/wiki/Event_(computing)
thing that happens or takes place, especially one of importance.
The NZ Pocket Oxford Dictionary
jQuery('#eric').click(function (event) {
console.log('Hello world!');
});
The EventDispatcher component provides tools that allow your application components to communicate with each other by dispatching events and listening to them.
https://symfony.com/doc/current/components/event_dispatcher.html
We have a form that collects a name and email.
public function submitForm(array &$form, FormStateInterface $form_state) {
$name = $form_state->getValue('name');
$email = $form_state->getValue('email');
$this->logger('example_form')->notice('Somebody submitted the form...');
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$name = $form_state->getValue('name');
$email = $form_state->getValue('email');
$this->logger('example_form')->notice('Somebody submitted the form....');
$this->mailer->mail('example', 'example_form', $email, $this->currentUser->getPreferredLangcode(), ['name' => $name]);
}
public function create(ContainerInterface $container) {
$form = new static();
$form->setCurrentUser($container->get('current_user'));
$form->setMailer($container->get('plugin.manager.mail'));
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$name = $form_state->getValue('name');
$email = $form_state->getValue('email');
$this->logger('example_form')->notice('Somebody submitted the form.');
$this->mailer->mail('example', 'example_form', $email, $this->currentUser->getPreferredLangcode(), ['name' => $name]);
$this->crmManager->add($email, ['name' => $name]);
$this->slack->sendMessage('A new user has submitted your awesome form', 'info');
$date = new \DateTime('@' . $this->time->getRequestTime();
if ($date->format('N') === 1 && $date->format('i') === 29) {
drupal_set_message('Congratulations you are a winner...');
}
}
public function create(ContainerInterface $container) {
$form = new static();
$form->setCurrentUser($container->get('current_user'));
$form->setMailer($container->get('plugin.manager.mail'));
$form->setCrmManager($container->get('custom.crm_manager'));
$form->setSlack($container->get('slack.manager'));
$form->setTime($container->get('datetime.time'));
return $form;
}
final class ExampleModuleEvents {
/** docBlock */
const SIGNUP_FORM_SUBMIT = 'module_name.signup_form_submit';
}
use Symfony\Component\EventDispatcher\Event;
class SignupFormEvent extends Event {
protected $submittedName;
protected $submittedEmail;
public function __construct($name, $email) {
$this->submittedName = $name;
$this->submittedEmail = $email;
}
public function getSubmittedName() {
return $this->submittedName;
}
public function getSubmittedEmail() {
return $this->submittedEmail;
}
}
public function create(ContainerInterface $container) {
$form = new static();
$form->setCurrentUser($container->get('current_user'));
$form->setMailer($container->get('plugin.manager.mail'));
$form->setCrmManager($container->get('custom.crm_manager'));
$form->setSlack($container->get('slack.manager'));
$form->setTime($container->get('datetime.time'));
return $form;
}
public function create(ContainerInterface $container) {
$form = new static();
$form->setEventDispatcher($container->get('event_dispatcher'));
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$name = $form_state->getValue('name');
$email = $form_state->getValue('email');
$event = new SignupFormEvent($name, $email);
$this->eventDispatcher->dispatch(ExampleModuleEvents::SIGNUP_FORM_SUBMIT, $event);
}
public static function getSubscribedEvents();
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class RegistrationMailer implements EventSubscriberInterface {
protected $mailManager;
protected $currentUser;
public function __construct(MailManagerInterface $mailManager, AccountProxyInterface $currentUser) {
$this->mailManager = $mailManager;
$this->currentUser = $currentUser;
};
public static function getSubscribedEvents() {
$events = [];
$events[ExampleModuleEvents::SIGNUP_FORM_SUBMIT][] = ['onRegister'];
return $events;
}
public function onRegister(SignupFormEvent $event) { ... }
}
public function onRegister($event) {
$module = 'event_demo';
$key = 'register_interest';
$to = $event->getSubmittedEmail();
$params = ['name' => $event->getSubmittedName()];
$language = $this->currentUser->getPreferredLangcode();
$this->mailManager->mail($module, $key, $to, $language, $params);
}
services:
example_module.registration_mailer:
class: "Drupal\example_crm_module\EventSubscriber\RegistrationMailer"
arguments: ["@plugin.manager.mail", "@current_user"]
tags:
- { name: event_subscriber }
Any PHP object that performs some sort of "global" task.
http://symfony.com/doc/current/book/service_container.html
Inject dependencies.
public function myMethod() {
$slack = new SlackClient();
$slack->sendMessage('This is bad.', 'info');
}
protected $slack;
public function __construct(SlackClientInterface $slack) {
$this->slack = $slack;
}
public function myMethod() {
$this->slack->sendMessage('This is better.', 'info');
}
arguments: ["@plugin.manager.mail"]
$container->get('@plugin.manager.mail')
public static function getSubscribedEvents() {
$events[ExampleModuleEvents::SIGNUP_FORM_SUBMIT][] = ['onRegister'];
return $events;
}
public static function getSubscribedEvents() {
$events[ExampleModuleEvents::SIGNUP_FORM_SUBMIT][] = ['onRegister', 0];
return $events;
}
public static function getSubscribedEvents() {
$events[ExampleModuleEvents::SIGNUP_FORM_SUBMIT][] = ['onRegister', 1000];
return $events;
}
public function onRegister($event) {
$event->stopPropagation();
}
private $kernel;
private $request;
private $requestType;
private $response;
public function getResponse()
{
return $this->response;
}
public function setResponse(Response $response)
{
$this->response = $response;
$this->stopPropagation();
}
$request = $event->getRequest();
$config = $this->configFactory->get('system.performance');
$exclude_paths = $config->get('fast_404.exclude_paths');
if ($config->get('fast_404.enabled') && $exclude_paths && !preg_match($exclude_paths, $request->getPathInfo())) {
$fast_paths = $config->get('fast_404.paths');
if ($fast_paths && preg_match($fast_paths, $request->getPathInfo())) {
$fast_404_html = strtr($config->get('fast_404.html'), ['@path' => Html::escape($request->getUri())]);
$response = new Response($fast_404_html, Response::HTTP_NOT_FOUND);
$event->setResponse($response);
}
}
$this->makeSubrequest($event, Url::fromRoute('system.404')->toString(), Response::HTTP_NOT_FOUND);