Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save rakeshjames/3efd61c04f3d42ed437f3d779e7a01fb to your computer and use it in GitHub Desktop.
Save rakeshjames/3efd61c04f3d42ed437f3d779e7a01fb to your computer and use it in GitHub Desktop.
Example for how to dispatch an example event in Drupal 8?
<?php
// Following is the example for How to dispatch an event in Drupal 8?
// Use the namespace of the ExampleEvent class
use Drupal\example_events\ExampleEvent;
// load the Symfony event dispatcher object through services
$dispatcher = \Drupal::service('event_dispatcher');
// creating our event class object.
$event = new ExampleEvent($form_state->getValue('name'));
// dispatching the event through the ‘dispatch’ method,
// passing event name and event object ‘$event’ as parameters.
$dispatcher->dispatch(ExampleEvent::SUBMIT, $event);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment