You are here

class FormEntityDisplayEditAlterEventSubscriber in Hook Event Dispatcher 8.2

Same name and namespace in other branches
  1. 3.x modules/field_event_dispatcher/src/EventSubscriber/Form/FormEntityDisplayEditAlterEventSubscriber.php \Drupal\field_event_dispatcher\EventSubscriber\Form\FormEntityDisplayEditAlterEventSubscriber

Event subscriber for 'entity_(view/form)_display_edit_form' form alters.

Hierarchy

Expanded class hierarchy of FormEntityDisplayEditAlterEventSubscriber

1 file declares its use of FormEntityDisplayEditAlterEventSubscriber
AbstractFormEntityDisplayEditAlterEventSubscriberTestCase.php in modules/field_event_dispatcher/tests/src/Unit/Field/AbstractFormEntityDisplayEditAlterEventSubscriberTestCase.php
1 string reference to 'FormEntityDisplayEditAlterEventSubscriber'
field_event_dispatcher.services.yml in modules/field_event_dispatcher/field_event_dispatcher.services.yml
modules/field_event_dispatcher/field_event_dispatcher.services.yml
1 service uses FormEntityDisplayEditAlterEventSubscriber
field_event_dispatcher.form.entity_display_edit.alter.subscriber in modules/field_event_dispatcher/field_event_dispatcher.services.yml
Drupal\field_event_dispatcher\EventSubscriber\Form\FormEntityDisplayEditAlterEventSubscriber

File

modules/field_event_dispatcher/src/EventSubscriber/Form/FormEntityDisplayEditAlterEventSubscriber.php, line 12

Namespace

Drupal\field_event_dispatcher\EventSubscriber\Form
View source
class FormEntityDisplayEditAlterEventSubscriber implements EventSubscriberInterface {

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() : array {
    return [
      'hook_event_dispatcher.form_entity_view_display_edit_form.alter' => 'formAlter',
      'hook_event_dispatcher.form_entity_form_display_edit_form.alter' => 'formAlter',
    ];
  }

  /**
   * Alter the entity edit form third-party elements structure.
   *
   * This works around the problem that Drupal core nests each invokation of
   * 'field_formatter_third_party_settings_form' and
   * 'field_widget_third_party_settings_form' under the machine name of the
   * module that implements it, which would be the name of this module and not
   * the modules that subscribe to our event.
   *
   * @param \Drupal\core_event_dispatcher\Event\Form\FormIdAlterEvent $event
   *   The event object.
   *
   * @see \Drupal\field_ui\Form\EntityViewDisplayEditForm::thirdPartySettingsForm()
   *   This is where Drupal core nests each invokation of
   *   'field_formatter_third_party_settings_form' under the machine name
   *   of the module that implements it.
   *
   * @see \Drupal\field_ui\Form\EntityFormDisplayEditForm::thirdPartySettingsForm()
   *   This is where Drupal core nests each invokation of
   *   'field_widget_third_party_settings_form' under the machine name of
   *   the module that implements it.
   *
   * @see \Drupal\field_event_dispatcher\Event\Field\AbstractFieldThirdPartySettingsFormEvent::addElements()
   *   This is where event subscribers add their third-party form elements.
   */
  public function formAlter(FormIdAlterEvent $event) : void {
    $form =& $event
      ->getForm();

    // $form['#fields'] lists all field name keys on this entity.
    foreach ($form['#fields'] as $fieldName) {

      // Skip any fields that have no field_event_dispatcher third-party
      // settings.
      if (!isset($form['fields'][$fieldName]['plugin']['settings_edit_form']['third_party_settings']['field_event_dispatcher'])) {
        continue;
      }
      $thirdPartySettings =& $form['fields'][$fieldName]['plugin']['settings_edit_form']['third_party_settings'];
      $thirdPartySettings = NestedArray::mergeDeep($thirdPartySettings, $thirdPartySettings['field_event_dispatcher']);
      unset($thirdPartySettings['field_event_dispatcher']);
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FormEntityDisplayEditAlterEventSubscriber::formAlter public function Alter the entity edit form third-party elements structure.
FormEntityDisplayEditAlterEventSubscriber::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.