You are here

public static function MediaLibrary::updateFormElement in Media Library Form API Element 2.x

Same name and namespace in other branches
  1. 8 src/Element/MediaLibrary.php \Drupal\media_library_form_element\Element\MediaLibrary::updateFormElement()

AJAX callback to update the widget when the selection changes.

Parameters

array $form: The form array.

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

Return value

array An AJAX response to update the selection.

File

src/Element/MediaLibrary.php, line 364

Class

MediaLibrary
Provides a Media library form element.

Namespace

Drupal\media_library_form_element\Element

Code

public static function updateFormElement(array $form, FormStateInterface $form_state) : array {
  $triggering_element = $form_state
    ->getTriggeringElement();

  // This callback is either invoked from the remove button or the update
  // button, which have different nesting levels.
  $is_remove_button = end($triggering_element['#parents']) === 'remove_button';
  $length = $is_remove_button ? -4 : -1;
  if (count($triggering_element['#array_parents']) < abs($length)) {
    throw new \LogicException('The element that triggered the form element update was at an unexpected depth. Triggering element parents were: ' . implode(',', $triggering_element['#array_parents']));
  }
  $parents = array_slice($triggering_element['#array_parents'], 0, $length);
  return NestedArray::getValue($form, $parents);
}