You are here

public function EventsExampleForm::submitForm in Examples for Developers 8

Same name and namespace in other branches
  1. 3.x modules/events_example/src/Form/EventsExampleForm.php \Drupal\events_example\Form\EventsExampleForm::submitForm()

Form submission handler.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Overrides FormInterface::submitForm

File

events_example/src/Form/EventsExampleForm.php, line 110

Class

EventsExampleForm
Implements the SimpleForm form controller.

Namespace

Drupal\events_example\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $type = $form_state
    ->getValue('incident_type');
  $report = $form_state
    ->getValue('incident');

  // When dispatching, or triggering, an event start by constructing a new
  // event object. Then use the event dispatcher service to notify any event
  // subscribers. Event objects are used to transport relevant data to any
  // subscribers, as well as keep track of the current state of an event. It
  // is best practice to create a unique class wrapping
  // \Symfony\Component\EventDispatcher\Event.
  $event = new IncidentReportEvent($type, $report);

  // Dispatch an event by specifying which event, and providing an event
  // object. Rather than hard code the event name you should use a constant
  // to represent the event being dispatched. The constant serves as a
  // location for documentation of the event, and ensures your code is future
  // proofed against event name changes.
  $this->eventDispatcher
    ->dispatch(IncidentEvents::NEW_REPORT, $event);
}