Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / lib / Drupal / Core / Ajax / AjaxResponse.php
1 <?php
2
3 namespace Drupal\Core\Ajax;
4
5 use Drupal\Core\Render\BubbleableMetadata;
6 use Drupal\Core\Render\AttachmentsInterface;
7 use Drupal\Core\Render\AttachmentsTrait;
8 use Symfony\Component\HttpFoundation\JsonResponse;
9
10 /**
11  * JSON response object for AJAX requests.
12  *
13  * @ingroup ajax
14  */
15 class AjaxResponse extends JsonResponse implements AttachmentsInterface {
16
17   use AttachmentsTrait;
18
19   /**
20    * The array of ajax commands.
21    *
22    * @var array
23    */
24   protected $commands = [];
25
26   /**
27    * Add an AJAX command to the response.
28    *
29    * @param \Drupal\Core\Ajax\CommandInterface $command
30    *   An AJAX command object implementing CommandInterface.
31    * @param bool $prepend
32    *   A boolean which determines whether the new command should be executed
33    *   before previously added commands. Defaults to FALSE.
34    *
35    * @return AjaxResponse
36    *   The current AjaxResponse.
37    */
38   public function addCommand(CommandInterface $command, $prepend = FALSE) {
39     if ($prepend) {
40       array_unshift($this->commands, $command->render());
41     }
42     else {
43       $this->commands[] = $command->render();
44     }
45     if ($command instanceof CommandWithAttachedAssetsInterface) {
46       $assets = $command->getAttachedAssets();
47       $attachments = [
48         'library' => $assets->getLibraries(),
49         'drupalSettings' => $assets->getSettings(),
50       ];
51       $attachments = BubbleableMetadata::mergeAttachments($this->getAttachments(), $attachments);
52       $this->setAttachments($attachments);
53     }
54
55     return $this;
56   }
57
58   /**
59    * Gets all AJAX commands.
60    *
61    * @return \Drupal\Core\Ajax\CommandInterface[]
62    *   Returns all previously added AJAX commands.
63    */
64   public function &getCommands() {
65     return $this->commands;
66   }
67
68 }