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