You are here

public static function FieldWrapperBase::onFormatterTypeChange in (Entity Reference) Field Formatters 8.2

Same name and namespace in other branches
  1. 8 src/Plugin/Field/FieldFormatter/FieldWrapperBase.php \Drupal\field_formatter\Plugin\Field\FieldFormatter\FieldWrapperBase::onFormatterTypeChange()
  2. 3.x src/Plugin/Field/FieldFormatter/FieldWrapperBase.php \Drupal\field_formatter\Plugin\Field\FieldFormatter\FieldWrapperBase::onFormatterTypeChange()

Ajax callback for fields with AJAX callback to update form substructure.

Parameters

array $form: The form.

\Drupal\Core\Form\FormStateInterface $form_state: The form state.

Return value

array The replaced form substructure.

File

src/Plugin/Field/FieldFormatter/FieldWrapperBase.php, line 163

Class

FieldWrapperBase
Wraps an existing field.

Namespace

Drupal\field_formatter\Plugin\Field\FieldFormatter

Code

public static function onFormatterTypeChange(array $form, FormStateInterface $form_state) {
  $triggeringElement = $form_state
    ->getTriggeringElement();

  // Dynamically return the dependent ajax for elements based on the
  // triggering element. This shouldn't be done statically because
  // settings forms may be different, e.g. for layout builder, core, ...
  if (!empty($triggeringElement['#array_parents'])) {
    $subformKeys = $triggeringElement['#array_parents'];

    // Remove the triggering element itself and add the 'settings' below key.
    array_pop($subformKeys);
    $subformKeys[] = 'settings';

    // Return the subform:
    return NestedArray::getValue($form, $subformKeys);
  }
}