You are here

public static function EntityBrowserBlock::updateCallback in Entity Browser Block 8

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

File

src/Plugin/Block/EntityBrowserBlock.php, line 229

Class

EntityBrowserBlock
Defines a generic entity browser block type.

Namespace

Drupal\entity_browser_block\Plugin\Block

Code

public static function updateCallback(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('remove_', '', $trigger['#name']);
    unset($selection['table'][$id]);
    $value = explode(' ', $selection['entity_browser']['entity_ids']['#value']);
    $selection['entity_browser']['entity_ids']['#value'] = array_diff($value, [
      $id,
    ]);
  }
  else {
    $parents = array_slice($trigger['#array_parents'], 0, -2);
    $selection = NestedArray::getValue($form, $parents);
  }
  return $selection;
}