Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\Core\Ajax;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Component\Render\PlainTextOutput;
|
Chris@0
|
6
|
Chris@0
|
7 /**
|
Chris@0
|
8 * Defines an AJAX command to open certain content in a dialog.
|
Chris@0
|
9 *
|
Chris@0
|
10 * @ingroup ajax
|
Chris@0
|
11 */
|
Chris@0
|
12 class OpenDialogCommand implements CommandInterface, CommandWithAttachedAssetsInterface {
|
Chris@0
|
13
|
Chris@0
|
14 use CommandWithAttachedAssetsTrait;
|
Chris@0
|
15
|
Chris@0
|
16 /**
|
Chris@0
|
17 * The selector of the dialog.
|
Chris@0
|
18 *
|
Chris@0
|
19 * @var string
|
Chris@0
|
20 */
|
Chris@0
|
21 protected $selector;
|
Chris@0
|
22
|
Chris@0
|
23 /**
|
Chris@0
|
24 * The title of the dialog.
|
Chris@0
|
25 *
|
Chris@0
|
26 * @var string
|
Chris@0
|
27 */
|
Chris@0
|
28 protected $title;
|
Chris@0
|
29
|
Chris@0
|
30 /**
|
Chris@0
|
31 * The content for the dialog.
|
Chris@0
|
32 *
|
Chris@0
|
33 * Either a render array or an HTML string.
|
Chris@0
|
34 *
|
Chris@0
|
35 * @var string|array
|
Chris@0
|
36 */
|
Chris@0
|
37 protected $content;
|
Chris@0
|
38
|
Chris@0
|
39 /**
|
Chris@0
|
40 * Stores dialog-specific options passed directly to jQuery UI dialogs. Any
|
Chris@0
|
41 * jQuery UI option can be used. See http://api.jqueryui.com/dialog.
|
Chris@0
|
42 *
|
Chris@0
|
43 * @var array
|
Chris@0
|
44 */
|
Chris@0
|
45 protected $dialogOptions;
|
Chris@0
|
46
|
Chris@0
|
47 /**
|
Chris@0
|
48 * Custom settings that will be passed to the Drupal behaviors on the content
|
Chris@0
|
49 * of the dialog.
|
Chris@0
|
50 *
|
Chris@0
|
51 * @var array
|
Chris@0
|
52 */
|
Chris@0
|
53 protected $settings;
|
Chris@0
|
54
|
Chris@0
|
55 /**
|
Chris@0
|
56 * Constructs an OpenDialogCommand object.
|
Chris@0
|
57 *
|
Chris@0
|
58 * @param string $selector
|
Chris@0
|
59 * The selector of the dialog.
|
Chris@0
|
60 * @param string $title
|
Chris@0
|
61 * The title of the dialog.
|
Chris@0
|
62 * @param string|array $content
|
Chris@0
|
63 * The content that will be placed in the dialog, either a render array
|
Chris@0
|
64 * or an HTML string.
|
Chris@0
|
65 * @param array $dialog_options
|
Chris@0
|
66 * (optional) Options to be passed to the dialog implementation. Any
|
Chris@0
|
67 * jQuery UI option can be used. See http://api.jqueryui.com/dialog.
|
Chris@0
|
68 * @param array|null $settings
|
Chris@0
|
69 * (optional) Custom settings that will be passed to the Drupal behaviors
|
Chris@0
|
70 * on the content of the dialog. If left empty, the settings will be
|
Chris@0
|
71 * populated automatically from the current request.
|
Chris@0
|
72 */
|
Chris@0
|
73 public function __construct($selector, $title, $content, array $dialog_options = [], $settings = NULL) {
|
Chris@0
|
74 $title = PlainTextOutput::renderFromHtml($title);
|
Chris@0
|
75 $dialog_options += ['title' => $title];
|
Chris@0
|
76 $this->selector = $selector;
|
Chris@0
|
77 $this->content = $content;
|
Chris@0
|
78 $this->dialogOptions = $dialog_options;
|
Chris@0
|
79 $this->settings = $settings;
|
Chris@0
|
80 }
|
Chris@0
|
81
|
Chris@0
|
82 /**
|
Chris@0
|
83 * Returns the dialog options.
|
Chris@0
|
84 *
|
Chris@0
|
85 * @return array
|
Chris@0
|
86 */
|
Chris@0
|
87 public function getDialogOptions() {
|
Chris@0
|
88 return $this->dialogOptions;
|
Chris@0
|
89 }
|
Chris@0
|
90
|
Chris@0
|
91 /**
|
Chris@0
|
92 * Sets the dialog options array.
|
Chris@0
|
93 *
|
Chris@0
|
94 * @param array $dialog_options
|
Chris@0
|
95 * Options to be passed to the dialog implementation. Any jQuery UI option
|
Chris@0
|
96 * can be used. See http://api.jqueryui.com/dialog.
|
Chris@0
|
97 */
|
Chris@0
|
98 public function setDialogOptions($dialog_options) {
|
Chris@0
|
99 $this->dialogOptions = $dialog_options;
|
Chris@0
|
100 }
|
Chris@0
|
101
|
Chris@0
|
102 /**
|
Chris@0
|
103 * Sets a single dialog option value.
|
Chris@0
|
104 *
|
Chris@0
|
105 * @param string $key
|
Chris@0
|
106 * Key of the dialog option. Any jQuery UI option can be used.
|
Chris@0
|
107 * See http://api.jqueryui.com/dialog.
|
Chris@0
|
108 * @param mixed $value
|
Chris@0
|
109 * Option to be passed to the dialog implementation.
|
Chris@0
|
110 */
|
Chris@0
|
111 public function setDialogOption($key, $value) {
|
Chris@0
|
112 $this->dialogOptions[$key] = $value;
|
Chris@0
|
113 }
|
Chris@0
|
114
|
Chris@0
|
115 /**
|
Chris@0
|
116 * Sets the dialog title (an alias of setDialogOptions).
|
Chris@0
|
117 *
|
Chris@0
|
118 * @param string $title
|
Chris@0
|
119 * The new title of the dialog.
|
Chris@0
|
120 */
|
Chris@0
|
121 public function setDialogTitle($title) {
|
Chris@12
|
122 $this->setDialogOption('title', $title);
|
Chris@0
|
123 }
|
Chris@0
|
124
|
Chris@0
|
125 /**
|
Chris@0
|
126 * Implements \Drupal\Core\Ajax\CommandInterface:render().
|
Chris@0
|
127 */
|
Chris@0
|
128 public function render() {
|
Chris@0
|
129 // For consistency ensure the modal option is set to TRUE or FALSE.
|
Chris@0
|
130 $this->dialogOptions['modal'] = isset($this->dialogOptions['modal']) && $this->dialogOptions['modal'];
|
Chris@0
|
131 return [
|
Chris@0
|
132 'command' => 'openDialog',
|
Chris@0
|
133 'selector' => $this->selector,
|
Chris@0
|
134 'settings' => $this->settings,
|
Chris@0
|
135 'data' => $this->getRenderedContent(),
|
Chris@0
|
136 'dialogOptions' => $this->dialogOptions,
|
Chris@0
|
137 ];
|
Chris@0
|
138 }
|
Chris@0
|
139
|
Chris@0
|
140 }
|