class EventsExampleForm in Examples for Developers 8
Same name and namespace in other branches
- 3.x modules/events_example/src/Form/EventsExampleForm.php \Drupal\events_example\Form\EventsExampleForm
Implements the SimpleForm form controller.
The submitForm() method of this class demonstrates using the event dispatcher service to dispatch an event.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\events_example\Form\EventsExampleForm
Expanded class hierarchy of EventsExampleForm
See also
\Drupal\events_exampl\Event\IncidentEvents
\Drupal\events_example\Event\IncidentReportEvent
\Symfony\Component\EventDispatcher\EventDispatcherInterface
\Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher
Related topics
1 string reference to 'EventsExampleForm'
- events_example.routing.yml in events_example/
events_example.routing.yml - events_example/events_example.routing.yml
File
- events_example/
src/ Form/ EventsExampleForm.php, line 25
Namespace
Drupal\events_example\FormView source
class EventsExampleForm extends FormBase {
/**
* The event dispatcher service.
*
* @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
*/
protected $eventDispatcher;
/**
* Constructs a new UserLoginForm.
*
* @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher
* The event dispatcher service.
*/
public function __construct(EventDispatcherInterface $event_dispatcher) {
// The event dispatcher service is an implementation of
// \Symfony\Component\EventDispatcher\EventDispatcherInterface. In Drupal
// this is generally and instance of the
// \Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher service.
// This dispatcher improves performance when dispatching events by compiling
// a list of subscribers into the service container so that they do not need
// to be looked up every time.
$this->eventDispatcher = $event_dispatcher;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('event_dispatcher'));
}
/**
* {@inheritdoc}
*/
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;
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'events_example_form';
}
/**
* {@inheritdoc}
*/
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);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
EventsExampleForm:: |
protected | property | The event dispatcher service. | |
EventsExampleForm:: |
public | function |
Form constructor. Overrides FormInterface:: |
|
EventsExampleForm:: |
public static | function |
Instantiates a new instance of this class. Overrides FormBase:: |
|
EventsExampleForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
EventsExampleForm:: |
public | function |
Form submission handler. Overrides FormInterface:: |
|
EventsExampleForm:: |
public | function | Constructs a new UserLoginForm. | |
FormBase:: |
protected | property | The config factory. | 1 |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Retrieves a configuration object. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 1 |
FormBase:: |
private | function | Returns the service container. | |
FormBase:: |
protected | function | Gets the current user. | |
FormBase:: |
protected | function | Gets the request object. | |
FormBase:: |
protected | function | Gets the route match. | |
FormBase:: |
protected | function | Gets the logger for a specific channel. | |
FormBase:: |
protected | function |
Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: |
|
FormBase:: |
public | function | Resets the configuration factory. | |
FormBase:: |
public | function | Sets the config factory for this form. | |
FormBase:: |
public | function | Sets the request stack object to use. | |
FormBase:: |
public | function |
Form validation handler. Overrides FormInterface:: |
62 |
LinkGeneratorTrait:: |
protected | property | The link generator. | 1 |
LinkGeneratorTrait:: |
protected | function | Returns the link generator. | |
LinkGeneratorTrait:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
LinkGeneratorTrait:: |
public | function | Sets the link generator service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. |