You are here

final class ExampleFieldEventSubscribers in Hook Event Dispatcher 8.2

Same name and namespace in other branches
  1. 3.x examples/ExampleFieldEventSubscribers.php \Drupal\hook_event_dispatcher\ExampleFieldEventSubscribers

Class ExampleFieldEventSubscribers.

Don't forget to define your class as a service and tag it as an "event_subscriber":

services: hook_event_dispatcher.example_field_subscribers: class: Drupal\hook_event_dispatcher\ExampleFieldEventSubscribers tags:

  • { name: event_subscriber }

Hierarchy

Expanded class hierarchy of ExampleFieldEventSubscribers

File

examples/ExampleFieldEventSubscribers.php, line 21

Namespace

Drupal\hook_event_dispatcher
View source
final class ExampleFieldEventSubscribers implements EventSubscriberInterface {

  /**
   * Alter widget form.
   *
   * @param \Drupal\field_event_dispatcher\Event\Field\WidgetFormAlterEvent $event
   *   The event.
   */
  public function alterWidgetForm(WidgetFormAlterEvent $event) : void {
    $element =& $event
      ->getElement();
    $element['extra_field'] = [
      '#type' => 'textfield',
      '#title' => 'I am an extra field!',
    ];
  }

  /**
   * Alter widget string text field.
   *
   * @param \Drupal\field_event_dispatcher\Event\Field\WidgetTypeFormAlterEvent $event
   *   The event.
   */
  public function alterWidgetStringTextField(WidgetTypeFormAlterEvent $event) : void {
    $element =& $event
      ->getElement();

    // Do something cool.
    $element['special_label'] = [
      'type' => 'label',
      'label' => 'Extra special label',
    ];
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() : array {
    return [
      HookEventDispatcherInterface::WIDGET_FORM_ALTER => 'alterWidgetForm',
      'hook_event_dispatcher.widget_string_textfield.alter' => 'alterWidgetStringTextField',
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ExampleFieldEventSubscribers::alterWidgetForm public function Alter widget form.
ExampleFieldEventSubscribers::alterWidgetStringTextField public function Alter widget string text field.
ExampleFieldEventSubscribers::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.