function EventsExampleSubscriber::notifyMario

Same name in other branches
  1. 4.0.x modules/events_example/src/EventSubscriber/EventsExampleSubscriber.php \Drupal\events_example\EventSubscriber\EventsExampleSubscriber::notifyMario()

If this incident is about a missing princess notify Mario.

Per our configuration above, this method is called whenever the IncidentEvents::NEW_REPORT event is dispatched. This method is where you place any custom logic that you want to perform when the specific event is triggered.

These responder methods receive an event object as their argument. The event object is usually, but not always, specific to the event being triggered and contains data about application state and configuration relative to what was happening when the event was triggered.

For example, when responding to an event triggered by saving a configuration change you'll get an event object that contains the relevant configuration object.

Parameters

\Drupal\events_example\Event\IncidentReportEvent $event: The event object containing the incident report.

File

modules/events_example/src/EventSubscriber/EventsExampleSubscriber.php, line 93

Class

EventsExampleSubscriber
Subscribe to IncidentEvents::NEW_REPORT events and react to new reports.

Namespace

Drupal\events_example\EventSubscriber

Code

public function notifyMario(IncidentReportEvent $event) {
    // You can use the event object to access information about the event passed
    // along by the event dispatcher.
    if ($event->getType() == 'stolen_princess') {
        $this->messenger()
            ->addStatus($this->t('Mario has been alerted. Thank you. This message was set by an event subscriber. See @method()', [
            '@method' => __METHOD__,
        ]));
        // Optionally use the event object to stop propagation.
        // If there are other subscribers that have not been called yet this will
        // cause them to be skipped.
        $event->stopPropagation();
    }
}