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