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