8.5.x AjaxResponse.php AjaxResponse::addCommand(CommandInterface $command, $prepend = FALSE)
8.0.x AjaxResponse.php AjaxResponse::addCommand(CommandInterface $command, $prepend = FALSE)
8.1.x AjaxResponse.php AjaxResponse::addCommand(CommandInterface $command, $prepend = FALSE)
8.2.x AjaxResponse.php AjaxResponse::addCommand(CommandInterface $command, $prepend = FALSE)
8.3.x AjaxResponse.php AjaxResponse::addCommand(CommandInterface $command, $prepend = FALSE)
8.4.x AjaxResponse.php AjaxResponse::addCommand(CommandInterface $command, $prepend = FALSE)
8.6.x AjaxResponse.php AjaxResponse::addCommand(CommandInterface $command, $prepend = FALSE)

Add an AJAX command to the response.

Parameters

\Drupal\Core\Ajax\CommandInterface $command: An AJAX command object implementing CommandInterface.

bool $prepend: A boolean which determines whether the new command should be executed before previously added commands. Defaults to FALSE.

Return value

AjaxResponse The current AjaxResponse.

File

core/lib/Drupal/Core/Ajax/AjaxResponse.php, line 38

Class

AjaxResponse
JSON response object for AJAX requests.

Namespace

Drupal\Core\Ajax

Code

public function addCommand(CommandInterface $command, $prepend = FALSE) {
  if ($prepend) {
    array_unshift($this->commands, $command
      ->render());
  }
  else {
    $this->commands[] = $command
      ->render();
  }
  if ($command instanceof CommandWithAttachedAssetsInterface) {
    $assets = $command
      ->getAttachedAssets();
    $attachments = [
      'library' => $assets
        ->getLibraries(),
      'drupalSettings' => $assets
        ->getSettings(),
    ];
    $attachments = BubbleableMetadata::mergeAttachments($this
      ->getAttachments(), $attachments);
    $this
      ->setAttachments($attachments);
  }
  return $this;
}