class AjaxResponse

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/Ajax/AjaxResponse.php \Drupal\Core\Ajax\AjaxResponse
  2. 8.9.x core/lib/Drupal/Core/Ajax/AjaxResponse.php \Drupal\Core\Ajax\AjaxResponse
  3. 10 core/lib/Drupal/Core/Ajax/AjaxResponse.php \Drupal\Core\Ajax\AjaxResponse

JSON response object for AJAX requests.

Hierarchy

Expanded class hierarchy of AjaxResponse

Related topics

48 files declare their use of AjaxResponse
ActiveWorkspaceTestForm.php in core/modules/workspaces/tests/modules/workspaces_test/src/Form/ActiveWorkspaceTestForm.php
AddFormBase.php in core/modules/media_library/src/Form/AddFormBase.php
AjaxRenderer.php in core/lib/Drupal/Core/Render/MainContent/AjaxRenderer.php
AjaxResponseSubscriber.php in core/lib/Drupal/Core/EventSubscriber/AjaxResponseSubscriber.php
AjaxResponseTest.php in core/tests/Drupal/Tests/Core/Ajax/AjaxResponseTest.php

... See full list

File

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

Namespace

Drupal\Core\Ajax
View source
class AjaxResponse extends JsonResponse implements AttachmentsInterface {
  use AttachmentsTrait;
  
  /**
   * The array of ajax commands.
   *
   * @var array
   */
  protected $commands = [];
  
  /**
   * Add an AJAX command to the response.
   *
   * @param \Drupal\Core\Ajax\CommandInterface $command
   *   An AJAX command object implementing CommandInterface.
   * @param bool $prepend
   *   A boolean which determines whether the new command should be executed
   *   before previously added commands. Defaults to FALSE.
   *
   * @return $this
   *   The current AjaxResponse.
   */
  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;
  }
  
  /**
   * Merges other ajax response with this one.
   *
   * Adds commands and merges attachments from the other ajax response.
   *
   * @param \Drupal\Core\Ajax\AjaxResponse $other
   *   An AJAX response to merge.
   *
   * @return $this
   *   Returns this after merging.
   */
  public function mergeWith(AjaxResponse $other) : AjaxResponse {
    $this->commands = array_merge($this->getCommands(), $other->getCommands());
    $this->attachments = BubbleableMetadata::mergeAttachments($this->getAttachments(), $other->getAttachments());
    return $this;
  }
  
  /**
   * Gets all AJAX commands.
   *
   * @return array
   *   Returns render arrays for all previously added commands.
   */
  public function &getCommands() {
    return $this->commands;
  }

}

Members

Title Sort descending Modifiers Object type Summary
AjaxResponse::$commands protected property The array of ajax commands.
AjaxResponse::addCommand public function Add an AJAX command to the response.
AjaxResponse::getCommands public function Gets all AJAX commands.
AjaxResponse::mergeWith public function Merges other ajax response with this one.
AttachmentsTrait::$attachments protected property The attachments for this response.
AttachmentsTrait::addAttachments public function
AttachmentsTrait::getAttachments public function
AttachmentsTrait::setAttachments public function

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.