You are here

public static function EventsExampleSubscriber::getSubscribedEvents in Examples for Developers 3.x

Same name and namespace in other branches
  1. 8 events_example/src/EventSubscriber/EventsExampleSubscriber.php \Drupal\events_example\EventSubscriber\EventsExampleSubscriber::getSubscribedEvents()

File

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

Class

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

Namespace

Drupal\events_example\EventSubscriber

Code

public static function getSubscribedEvents() {

  // Return an array of events that you want to subscribe to mapped to the
  // method on this class that you would like called whenever the event is
  // triggered. A single class can subscribe to any number of events. For
  // organization purposes it's a good idea to create a new class for each
  // unique task/concept rather than just creating a catch-all class for all
  // event subscriptions.
  //
  // See EventSubscriberInterface::getSubscribedEvents() for an explanation
  // of the array's format.
  //
  // The array key is the name of the event your want to subscribe to. Best
  // practice is to use the constant that represents the event as defined by
  // the code responsible for dispatching the event. This way, if, for
  // example, the string name of an event changes your code will continue to
  // work. You can get a list of event constants for all events triggered by
  // core here:
  // https://api.drupal.org/api/drupal/core%21core.api.php/group/events/8.2.x.
  //
  // Since any module can define and trigger new events there may be
  // additional events available in your application. Look for classes with
  // the special @Event docblock indicator to discover other events.
  //
  // For each event key define an array of arrays composed of the method names
  // to call and optional priorities. The method name here refers to a method
  // on this class to call whenever the event is triggered.
  $events[IncidentEvents::NEW_REPORT][] = [
    'notifyMario',
  ];

  // Subscribers can optionally set a priority. If more than one subscriber is
  // listening to an event when it is triggered they will be executed in order
  // of priority. If no priority is set the default is 0.
  $events[IncidentEvents::NEW_REPORT][] = [
    'notifyBatman',
    -100,
  ];

  // We'll set an event listener with a very low priority to catch incident
  // types not yet defined. In practice, this will be the 'cat' incident.
  $events[IncidentEvents::NEW_REPORT][] = [
    'notifyDefault',
    -255,
  ];
  return $events;
}