ExampleFormEventSubscribers.php in Hook Event Dispatcher 3.x
File
examples/ExampleFormEventSubscribers.php
View source
<?php
namespace Drupal\hook_event_dispatcher;
use Drupal\core_event_dispatcher\Event\Form\FormAlterEvent;
use Drupal\core_event_dispatcher\Event\Form\FormBaseAlterEvent;
use Drupal\core_event_dispatcher\Event\Form\FormIdAlterEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ExampleFormEventSubscribers implements EventSubscriberInterface {
public function alterForm(FormAlterEvent $event) : void {
$form =& $event
->getForm();
$form['extra_markup'] = [
'#markup' => 'This is really cool markup',
];
}
public function alterSearchForm(FormIdAlterEvent $event) : void {
$form =& $event
->getForm();
$form['keys']['#attributes']['placeholder'] = 'Search some things';
}
public function alterNodeForm(FormBaseAlterEvent $event) : void {
$form =& $event
->getForm();
$form['title']['widget'][0]['value']['#title'] = 'A new title!';
}
public static function getSubscribedEvents() : array {
return [
HookEventDispatcherInterface::FORM_ALTER => 'alterForm',
'hook_event_dispatcher.form_search_block_form.alter' => 'alterSearchForm',
'hook_event_dispatcher.form_base_node_form.alter' => 'alterNodeForm',
];
}
}