Event dispatcher. Create event, Dispatch and Subscribe. | Drupal 8

Event dispatcher. Create event, Dispatch and Subscribe.

Submitted by editor on Fri, 01/19/2018 - 13:15

How to use Event dispatcher system of drupal 8 ?

Drupal's event system allow to create triggers when an event has been happened like create a custom hook on drupal 7.

Create an event.

The first step is, create an event on your custom module (example_events). This class must extend Symfony\Component\EventDispatcher\Event.
Example :
File : example_events/src/ExampleEvent.php

namespace Drupal\example_events;
use Symfony\Component\EventDispatcher\Event;
class ExampleEvent extends Event {
  const RUN = 'event.run';
  protected $data;
  public function __construct($data) {
    $this->data = $data;
  public function getData() {
    return $this->data;
  public function display() {
    drupal_set_message("This is as an example event : ".$this->data);

Dispatch the event.

You can dispatch the event anywate on your custom module.
Example :

// Use the namespace of the ExampleEvent class
use Drupal\example_events\ExampleEvent;
// Load dispatcher object through services.
$dispatcher = \Drupal::service('event_dispatcher');
// creating our event class object.
$event = new ExampleEvent("My Event");
// Dispatching the event through the ‘dispatch’  method,
// Passing event name and event object ‘$event’ as parameters.
$dispatcher->dispatch(ExampleEvent::RUN, $event);

Now you can Subscribe to this event in the same module or from another module.

Create event Subscriber : Subscribe to the event

Like any other event of drupal, now you can use ‘event_subscriber’ service (See this post)

File : example_events/src/EventSubscriber/ExampleEventSubScriber.php

 * @file
 * Contains \Drupal\example_events\ExampleEventSubScriber.
namespace Drupal\example_events\EventSubscriber;
use Drupal\Core\Config\ConfigCrudEvent;
use Drupal\Core\Config\ConfigEvents;
use Drupal\example_events\ExampleEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
 * Class ExampleEventSubScriber.
 * @package Drupal\example_events
class ExampleEventSubScriber implements EventSubscriberInterface {
   * {@inheritdoc}
  public static function getSubscribedEvents() {
    // Priority greater than 200 = No Cache (like hook_boot).
    $events[ExampleEvent::RUN][] = array('doSomeAction', 300);
    return $events;
   * Subscriber Callback for the event.
   * @param ExampleEvent $event
  public function doSomeAction(ExampleEvent $event) {
    // Get data:
    $data = $event->getData();
    drupal_set_message("The Example Event has been subscribed, event->getData() returned : '" . $event->getData() . "'");
    // Run display() action.
    $data = $event->display();

Tag Event Subscriber Class with event_subscriber

Finally add your Event Subscriber Class to .services.yml
File : example_events.services.yml

    class: Drupal\example_events\EventSubscriber\ExampleEventSubScriber
      - { name: 'event_subscriber' }


Example from : http://valuebound.com/resources/blog/how-to-define-an-event-dispatcher-and-subscriber-drupal-8


R HILDAN M (not verified)

Fri, 12/14/2018 - 17:02

ini ke 15 pusat plat astm516 70 pusat plat boiler pusat plat hardox pusat plat hb 400 pusat plat high strenght pusat plat high tensile sm490 yb pusat plat high tensile sm490ya pusat plat kapal pusat plat stainless ss304 pusat plat stainless ss310 Agen Plat Besi Baja Jual besi Pipa sch 80 baja Jual besi beton cakra steel cs Supplier Bondek Cor Supplier Bondek Cor Distributor Plat besi Jual bondek undangan pernikahan artis Jual Besi Pipa Baja Schedule 80 Toko Plat Besi Baja Supplier besi Pipa sch 80 baja Jual besi beton bjku Toko Bondek Cor Toko Bondek Cor Distributor besi h beam Jual wiremesh undangan pernikahan anti mainstream Jual Besi Pipa Baja Schedule 40 Jual Plat Besi Baja Toko besi Pipa sch 80 baja Jual besi beton Sni Ulir Polos Agen Bondek Cor Agen Bondek Cor Distributor besi wf Jual pipa besi baja sch 40 sch 80 undangan pernikahan amplop Jual Pipa Besi Hitam Medium Harga Plat Besi Baja Agen besi Pipa sch 80 baja Jual besi beton ais Pabrik Bondek Cor Pabrik Bondek Cor Distributor wiremesh Jual besi hollow undangan pernikahan mewah Jual Besi Wf Pabrik Plat Besi Baja Pabrik besi Pipa sch 80 baja Jual besi beton Distributor Welded Beam Distributor Welded Beam Distributor besi unp Jual besi beton Sni Ulir Polos undangan pernikahan anime Jual Besi H beam Distributor Besi H beam Baja Distributor Plat Bordes Harga Welded Beam Harga Welded Beam Distributor besi cnp Jual besi wf baja undangan pernikahan elegan Jual Besi Beton Supplier Besi H beam Baja Harga Plat Bordes www.pusatbesibaja.co.id Jual Welded Beam Jual Welded Beam Distributor besi hollow Jual besi unp undangan pernikahan contoh www.sentrabesibaja.com Agen Besi H beam Baja Jual Plat Bordes www.pusatbesibaja.com Supplier Welded Beam Supplier Welded Beam Distributor besi beton Sni Ulir Polos

Add new comment