You are here

public function FormEntityDisplayEditAlterEventSubscriber::formAlter 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::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.

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.

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.

This is where event subscribers add their third-party form elements.

Parameters

\Drupal\core_event_dispatcher\Event\Form\FormIdAlterEvent $event: The event object.

See also

\Drupal\field_ui\Form\EntityViewDisplayEditForm::thirdPartySettingsForm()

\Drupal\field_ui\Form\EntityFormDisplayEditForm::thirdPartySettingsForm()

\Drupal\field_event_dispatcher\Event\Field\AbstractFieldThirdPartySettingsFormEvent::addElements()

File

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

Class

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

Namespace

Drupal\field_event_dispatcher\EventSubscriber\Form

Code

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']);
  }
}