Namespace
Drupal\events_example\Form
File
-
modules/events_example/src/Form/EventsExampleForm.php
View source
<?php
namespace Drupal\events_example\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Drupal\events_example\Event\IncidentEvents;
use Drupal\events_example\Event\IncidentReportEvent;
class EventsExampleForm extends FormBase {
protected $eventDispatcher;
public function __construct(EventDispatcherInterface $event_dispatcher) {
$this->eventDispatcher = $event_dispatcher;
}
public static function create(ContainerInterface $container) {
return new static($container->get('event_dispatcher'));
}
public function buildForm(array $form, FormStateInterface $form_state) {
$form['intro'] = [
'#markup' => '<p>' . $this->t('This form demonstrates subscribing to, and dispatching, events. When the form is submitted an event is dispatched indicating a new report has been submitted. Event subscribers respond to this event with various messages depending on the incident type. Review the code for the events_example module to see how it works.') . '</p>',
];
$form['incident_type'] = [
'#type' => 'radios',
'#required' => TRUE,
'#title' => $this->t('What type of incident do you want to report?'),
'#options' => [
'stolen_princess' => $this->t('Missing princess'),
'cat' => $this->t('Cat stuck in tree'),
'joker' => $this->t('Something involving the Joker'),
],
];
$form['incident'] = [
'#type' => 'textarea',
'#required' => FALSE,
'#title' => $this->t('Incident report'),
'#description' => $this->t('Describe the incident in detail. This information will be passed along to all crime fighters.'),
'#cols' => 60,
'#rows' => 5,
];
$form['actions'] = [
'#type' => 'actions',
];
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this->t('Submit'),
];
return $form;
}
public function getFormId() {
return 'events_example_form';
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$type = $form_state->getValue('incident_type');
$report = $form_state->getValue('incident');
$event = new IncidentReportEvent($type, $report);
$this->eventDispatcher
->dispatch($event, IncidentEvents::NEW_REPORT);
}
}
Classes
| Title |
Deprecated |
Summary |
| EventsExampleForm |
|
Implements the SimpleForm form controller. |