You are here

class ExampleFormEventSubscribers in Hook Event Dispatcher 8

Class ExampleFormEventSubscribers.

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

services: hook_event_dispatcher.example_form_subscribers: class: '\Drupal\hook_event_dispatcher\Example\ExampleFormEventSubscribers' tags:

  • { name: 'event_subscriber' }

Hierarchy

  • class \Drupal\hook_event_dispatcher\Example\ExampleFormEventSubscribers implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of ExampleFormEventSubscribers

File

src/Example/ExampleFormEventSubscribers.php, line 25

Namespace

Drupal\hook_event_dispatcher\Example
View source
class ExampleFormEventSubscribers implements EventSubscriberInterface {

  /**
   * Alter form.
   *
   * @param \Drupal\hook_event_dispatcher\Event\Form\FormAlterEvent $event
   *   The event.
   */
  public function alterForm(FormAlterEvent $event) {
    $form =& $event
      ->getForm();
    $form['extra_markup'] = [
      '#markup' => 'This is really cool markup',
    ];
  }

  /**
   * Alter search form.
   *
   * @param \Drupal\hook_event_dispatcher\Event\Form\FormIdAlterEvent $event
   *   The event.
   */
  public function alterSearchForm(FormIdAlterEvent $event) {
    $form =& $event
      ->getForm();

    // Add placeholder.
    $form['keys']['#attributes']['placeholder'] = 'Search some things';
  }

  /**
   * Alter node form.
   *
   * @param \Drupal\hook_event_dispatcher\Event\Form\FormBaseAlterEvent $event
   *   The event.
   */
  public function alterNodeForm(FormBaseAlterEvent $event) {
    $form =& $event
      ->getForm();
    $form['title']['widget'][0]['value']['#title'] = 'A new title!';
  }

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

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

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

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    return [
      HookEventDispatcherInterface::FORM_ALTER => 'alterForm',
      // React on "search_block_form" form.
      'hook_event_dispatcher.form_search_block_form.alter' => 'alterSearchForm',
      // React on al forms with base id "node_form".
      'hook_event_dispatcher.form_base_node_form.alter' => 'alterNodeForm',
      'hook_event_dispatcher.widget_string_textfield.alter' => 'alterWidgetStringTextField',
    ];
  }

}

Members

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