ExampleFieldEventSubscribers.php in Hook Event Dispatcher 3.x
File
examples/ExampleFieldEventSubscribers.php
View source
<?php
namespace Drupal\hook_event_dispatcher;
use Drupal\field_event_dispatcher\Event\Field\WidgetFormAlterEvent;
use Drupal\field_event_dispatcher\Event\Field\WidgetTypeFormAlterEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
final class ExampleFieldEventSubscribers implements EventSubscriberInterface {
public function alterWidgetForm(WidgetFormAlterEvent $event) : void {
$element =& $event
->getElement();
$element['extra_field'] = [
'#type' => 'textfield',
'#title' => 'I am an extra field!',
];
}
public function alterWidgetStringTextField(WidgetTypeFormAlterEvent $event) : void {
$element =& $event
->getElement();
$element['special_label'] = [
'type' => 'label',
'label' => 'Extra special label',
];
}
public static function getSubscribedEvents() : array {
return [
HookEventDispatcherInterface::WIDGET_FORM_ALTER => 'alterWidgetForm',
'hook_event_dispatcher.widget_string_textfield.alter' => 'alterWidgetStringTextField',
];
}
}