public static function MultiStepDisplay::handleAjaxCommand in Entity Browser 8
Same name and namespace in other branches
- 8.2 src/Plugin/EntityBrowser/SelectionDisplay/MultiStepDisplay.php \Drupal\entity_browser\Plugin\EntityBrowser\SelectionDisplay\MultiStepDisplay::handleAjaxCommand()
Handler to generate Ajax response, after command is executed.
Parameters
array $form: Form.
\Drupal\Core\Form\FormStateInterface $form_state: Form state object.
Return value
\Drupal\Core\Ajax\AjaxResponse Return Ajax response with commands.
File
- src/
Plugin/ EntityBrowser/ SelectionDisplay/ MultiStepDisplay.php, line 270
Class
- MultiStepDisplay
- Show current selection and delivers selected entities.
Namespace
Drupal\entity_browser\Plugin\EntityBrowser\SelectionDisplayCode
public static function handleAjaxCommand(array $form, FormStateInterface $form_state) {
$ajax = new AjaxResponse();
if (($triggering_element = $form_state
->getTriggeringElement()) && $triggering_element['#name'] === 'ajax_commands_handler' && !empty($triggering_element['#value'])) {
$commands = json_decode($triggering_element['#value'], TRUE);
// Entity IDs that are affected by this command.
if (isset($commands['add'])) {
/** @var \Drupal\Core\Render\RendererInterface $renderer */
$renderer = \Drupal::service('renderer');
$entity_ids = $commands['add'];
$selected_entities =& $form_state
->get([
'entity_browser',
'selected_entities',
]);
// Get entities added by this command and generate JS commands for them.
$selected_entity_keys = array_keys($selected_entities);
$key_index = count($selected_entity_keys) - count($entity_ids);
foreach ($entity_ids as $entity_pair_info) {
$last_entity_id = $selected_entities[$selected_entity_keys[$key_index]]
->id();
$html = $renderer
->render($form['selection_display']['selected']['items_' . $last_entity_id . '_' . $selected_entity_keys[$key_index]]);
$ajax
->addCommand(new ReplaceCommand('div[id="' . $entity_pair_info['proxy_id'] . '"]', static::trimSingleHtmlTag($html)));
$key_index++;
}
// Check if action buttons should be added to form. When number of added
// entities is equal to number of selected entities. Then form buttons
// should be also rendered: use_selected and show_selection.
if (count($selected_entities) === count($entity_ids)) {
// Order is important, since commands are executed one after another.
$ajax
->addCommand(new AfterCommand('.entities-list', static::trimSingleHtmlTag($renderer
->render($form['selection_display']['show_selection']))));
$ajax
->addCommand(new AfterCommand('.entities-list', static::trimSingleHtmlTag($renderer
->render($form['selection_display']['use_selected']))));
}
}
// Add Invoke command to trigger loading of entities that are queued
// during execution of current Ajax request.
$ajax
->addCommand(new InvokeCommand('[name=ajax_commands_handler]', 'trigger', [
'execute-commands',
]));
}
return $ajax;
}