You are here

public static function EntityBrowserFormTrait::updateEntityBrowserSelected in Helper 8

AJAX callback: Re-renders the Entity Browser button/table.

File

src/EntityBrowserFormTrait.php, line 208

Class

EntityBrowserFormTrait
Provides helpers for adding an entity browser element to a form.

Namespace

Drupal\helper

Code

public static function updateEntityBrowserSelected(array &$form, FormStateInterface $form_state) {
  $trigger = $form_state
    ->getTriggeringElement();
  if (isset($trigger['#op']) && $trigger['#op'] === 'remove') {
    $parents = array_slice($trigger['#array_parents'], 0, -4);
    $selection = NestedArray::getValue($form, $parents);
    $id = str_replace('entity_browser_remove_', '', $trigger['#name']);
    unset($selection['selected'][$id]);
    $value = explode(' ', $selection['browser']['entity_ids']['#value']);
    $selection['browser']['entity_ids']['#value'] = array_diff($value, [
      $id,
    ]);
  }
  else {
    $parents = array_slice($trigger['#array_parents'], 0, -2);
    $selection = NestedArray::getValue($form, $parents);
  }
  return $selection;
}