3 namespace Drupal\Core\Ajax;
5 use Drupal\Component\Render\PlainTextOutput;
8 * Defines an AJAX command to open certain content in a dialog.
12 class OpenDialogCommand implements CommandInterface, CommandWithAttachedAssetsInterface {
14 use CommandWithAttachedAssetsTrait;
17 * The selector of the dialog.
24 * The title of the dialog.
31 * The content for the dialog.
33 * Either a render array or an HTML string.
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.
45 protected $dialogOptions;
48 * Custom settings that will be passed to the Drupal behaviors on the content
56 * Constructs an OpenDialogCommand object.
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
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.
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;
83 * Returns the dialog options.
87 public function getDialogOptions() {
88 return $this->dialogOptions;
92 * Sets the dialog options array.
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.
98 public function setDialogOptions($dialog_options) {
99 $this->dialogOptions = $dialog_options;
103 * Sets a single dialog option value.
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.
111 public function setDialogOption($key, $value) {
112 $this->dialogOptions[$key] = $value;
116 * Sets the dialog title (an alias of setDialogOptions).
118 * @param string $title
119 * The new title of the dialog.
121 public function setDialogTitle($title) {
122 $this->setDialogOption('title', $title);
126 * Implements \Drupal\Core\Ajax\CommandInterface:render().
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'];
132 'command' => 'openDialog',
133 'selector' => $this->selector,
134 'settings' => $this->settings,
135 'data' => $this->getRenderedContent(),
136 'dialogOptions' => $this->dialogOptions,