Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / core / lib / Drupal / Core / Ajax / InsertCommand.php
1 <?php
2
3 namespace Drupal\Core\Ajax;
4
5 /**
6  * Generic AJAX command for inserting content.
7  *
8  * This command instructs the client to insert the given HTML using whichever
9  * jQuery DOM manipulation method has been specified in the #ajax['method']
10  * variable of the element that triggered the request.
11  *
12  * This command is implemented by Drupal.AjaxCommands.prototype.insert()
13  * defined in misc/ajax.js.
14  *
15  * @ingroup ajax
16  */
17 class InsertCommand implements CommandInterface, CommandWithAttachedAssetsInterface {
18
19   use CommandWithAttachedAssetsTrait;
20
21   /**
22    * A CSS selector string.
23    *
24    * If the command is a response to a request from an #ajax form element then
25    * this value can be NULL.
26    *
27    * @var string
28    */
29   protected $selector;
30
31   /**
32    * The content for the matched element(s).
33    *
34    * Either a render array or an HTML string.
35    *
36    * @var string|array
37    */
38   protected $content;
39
40   /**
41    * A settings array to be passed to any attached JavaScript behavior.
42    *
43    * @var array
44    */
45   protected $settings;
46
47   /**
48    * Constructs an InsertCommand object.
49    *
50    * @param string $selector
51    *   A CSS selector.
52    * @param string|array $content
53    *   The content that will be inserted in the matched element(s), either a
54    *   render array or an HTML string.
55    * @param array $settings
56    *   An array of JavaScript settings to be passed to any attached behaviors.
57    */
58   public function __construct($selector, $content, array $settings = NULL) {
59     $this->selector = $selector;
60     $this->content = $content;
61     $this->settings = $settings;
62   }
63
64   /**
65    * Implements Drupal\Core\Ajax\CommandInterface:render().
66    */
67   public function render() {
68
69     return [
70       'command' => 'insert',
71       'method' => NULL,
72       'selector' => $this->selector,
73       'data' => $this->getRenderedContent(),
74       'settings' => $this->settings,
75     ];
76   }
77
78 }