You are here

public static function EntityReferenceBrowserWidget::updateWidgetCallback in Entity Browser 8

Same name and namespace in other branches
  1. 8.2 src/Plugin/Field/FieldWidget/EntityReferenceBrowserWidget.php \Drupal\entity_browser\Plugin\Field\FieldWidget\EntityReferenceBrowserWidget::updateWidgetCallback()

AJAX form callback.

File

src/Plugin/Field/FieldWidget/EntityReferenceBrowserWidget.php, line 483

Class

EntityReferenceBrowserWidget
Plugin implementation of the 'entity_reference' widget for entity browser.

Namespace

Drupal\entity_browser\Plugin\Field\FieldWidget

Code

public static function updateWidgetCallback(array &$form, FormStateInterface $form_state) {
  $trigger = $form_state
    ->getTriggeringElement();
  $reopen_browser = FALSE;

  // AJAX requests can be triggered by hidden "target_id" element when
  // entities are added or by one of the "Remove" buttons. Depending on that
  // we need to figure out where root of the widget is in the form structure
  // and use this information to return correct part of the form.
  $parents = [];
  if (!empty($trigger['#ajax']['event']) && $trigger['#ajax']['event'] == 'entity_browser_value_updated') {
    $parents = array_slice($trigger['#array_parents'], 0, -1);
  }
  elseif ($trigger['#type'] == 'submit' && strpos($trigger['#name'], '_remove_')) {
    $parents = array_slice($trigger['#array_parents'], 0, -static::$deleteDepth);
  }
  elseif ($trigger['#type'] == 'submit' && strpos($trigger['#name'], '_replace_')) {
    $parents = array_slice($trigger['#array_parents'], 0, -static::$deleteDepth);

    // We need to re-open the browser. Instead of just passing "TRUE", send
    // to the JS the unique part of the button's name that needs to be clicked
    // on to relaunch the browser.
    $reopen_browser = implode("-", array_slice($trigger['#parents'], 0, -static::$deleteDepth));
  }
  $parents = NestedArray::getValue($form, $parents);
  $parents['#attached']['drupalSettings']['entity_browser_reopen_browser'] = $reopen_browser;
  return $parents;
}