3 namespace Drupal\views_ui\Form\Ajax;
5 use Drupal\Component\Utility\Html;
6 use Drupal\Core\Ajax\AjaxResponse;
7 use Drupal\Core\Ajax\CloseModalDialogCommand;
8 use Drupal\Core\Ajax\OpenModalDialogCommand;
9 use Drupal\Core\Form\FormBase;
10 use Drupal\Core\Form\FormState;
11 use Drupal\Core\Form\FormStateInterface;
12 use Drupal\Core\Render\BubbleableMetadata;
13 use Drupal\Core\Render\RenderContext;
14 use Drupal\views\Ajax\HighlightCommand;
15 use Drupal\views\Ajax\ReplaceTitleCommand;
16 use Drupal\views\Ajax\ShowButtonsCommand;
17 use Drupal\views\Ajax\TriggerPreviewCommand;
18 use Drupal\views\ViewEntityInterface;
19 use Drupal\views_ui\Ajax\SetFormCommand;
20 use Symfony\Component\HttpFoundation\RedirectResponse;
23 * Provides a base class for Views UI AJAX forms.
25 abstract class ViewsFormBase extends FormBase implements ViewsFormInterface {
28 * The ID of the item this form is manipulating.
35 * The type of item this form is manipulating.
42 * Sets the ID for this form.
45 * The ID of the item this form is manipulating.
47 protected function setID($id) {
54 * Sets the type for this form.
57 * The type of the item this form is manipulating.
59 protected function setType($type) {
68 public function getFormState(ViewEntityInterface $view, $display_id, $js) {
69 // $js may already have been converted to a Boolean.
70 $ajax = is_string($js) ? $js === 'ajax' : $js;
71 return (new FormState())
72 ->set('form_id', $this->getFormId())
73 ->set('form_key', $this->getFormKey())
75 ->set('display_id', $display_id)
77 ->set('type', $this->type)
78 ->set('id', $this->id)
80 ->addBuildInfo('callback_object', $this);
86 public function getForm(ViewEntityInterface $view, $display_id, $js) {
87 $form_state = $this->getFormState($view, $display_id, $js);
88 $view = $form_state->get('view');
89 $key = $form_state->get('form_key');
91 // @todo Remove the need for this.
92 \Drupal::moduleHandler()->loadInclude('views_ui', 'inc', 'admin');
94 // Reset the cache of IDs. Drupal rather aggressively prevents ID
95 // duplication but this causes it to remember IDs that are no longer even
99 // check to see if this is the top form of the stack. If it is, pop
100 // it off; if it isn't, the user clicked somewhere else and the stack is
102 if (!empty($view->stack)) {
103 $identifier = implode('-', array_filter([$key, $view->id(), $display_id, $form_state->get('type'), $form_state->get('id')]));
104 // Retrieve the first form from the stack without changing the integer keys,
105 // as they're being used for the "2 of 3" progress indicator.
107 $key = key($view->stack);
108 $top = current($view->stack);
110 unset($view->stack[$key]);
112 if (array_shift($top) != $identifier) {
117 // Automatically remove the form cache if it is set and the key does
118 // not match. This way navigating away from the form without hitting
120 if (isset($view->form_cache) && $view->form_cache['key'] != $key) {
121 unset($view->form_cache);
124 $form_class = get_class($form_state->getFormObject());
125 $response = $this->ajaxFormWrapper($form_class, $form_state);
127 // If the form has not been submitted, or was not set for rerendering, stop.
128 if (!$form_state->isSubmitted() || $form_state->get('rerender')) {
132 // Sometimes we need to re-generate the form for multi-step type operations.
133 if (!empty($view->stack)) {
134 $stack = $view->stack;
135 $top = array_shift($stack);
137 // Build the new form state for the next form in the stack.
138 $reflection = new \ReflectionClass($view::$forms[$top[1]]);
139 /** @var $form_state \Drupal\Core\Form\FormStateInterface */
140 $form_state = $reflection->newInstanceArgs(array_slice($top, 3, 2))->getFormState($view, $top[2], $form_state->get('ajax'));
141 $form_class = get_class($form_state->getFormObject());
143 $form_state->setUserInput([]);
144 $form_url = views_ui_build_form_url($form_state);
145 if (!$form_state->get('ajax')) {
146 return new RedirectResponse($form_url->setAbsolute()->toString());
148 $form_state->set('url', $form_url);
149 $response = $this->ajaxFormWrapper($form_class, $form_state);
151 elseif (!$form_state->get('ajax')) {
152 // if nothing on the stack, non-js forms just go back to the main view editor.
153 $display_id = $form_state->get('display_id');
154 return new RedirectResponse($this->url('entity.view.edit_display_form', ['view' => $view->id(), 'display_id' => $display_id], ['absolute' => TRUE]));
157 $response = new AjaxResponse();
158 $response->addCommand(new CloseModalDialogCommand());
159 $response->addCommand(new ShowButtonsCommand(!empty($view->changed)));
160 $response->addCommand(new TriggerPreviewCommand());
161 if ($page_title = $form_state->get('page_title')) {
162 $response->addCommand(new ReplaceTitleCommand($page_title));
165 // If this form was for view-wide changes, there's no need to regenerate
166 // the display section of the form.
167 if ($display_id !== '') {
168 \Drupal::entityManager()->getFormObject('view', 'edit')->rebuildCurrentTab($view, $response, $display_id);
175 * Wrapper for handling AJAX forms.
177 * Wrapper around \Drupal\Core\Form\FormBuilderInterface::buildForm() to
178 * handle some AJAX stuff automatically.
179 * This makes some assumptions about the client.
181 * @param \Drupal\Core\Form\FormInterface|string $form_class
182 * The value must be one of the following:
183 * - The name of a class that implements \Drupal\Core\Form\FormInterface.
184 * - An instance of a class that implements \Drupal\Core\Form\FormInterface.
185 * @param \Drupal\Core\Form\FormStateInterface $form_state
186 * The current state of the form.
188 * @return \Drupal\Core\Ajax\AjaxResponse|string|array
189 * Returns one of three possible values:
190 * - A \Drupal\Core\Ajax\AjaxResponse object.
191 * - The rendered form, as a string.
192 * - A render array with the title in #title and the rendered form in the
195 protected function ajaxFormWrapper($form_class, FormStateInterface &$form_state) {
196 /** @var \Drupal\Core\Render\RendererInterface $renderer */
197 $renderer = \Drupal::service('renderer');
199 // This won't override settings already in.
200 if (!$form_state->has('rerender')) {
201 $form_state->set('rerender', FALSE);
203 $ajax = $form_state->get('ajax');
204 // Do not overwrite if the redirect has been disabled.
205 if (!$form_state->isRedirectDisabled()) {
206 $form_state->disableRedirect($ajax);
208 $form_state->disableCache();
210 // Builds the form in a render context in order to ensure that cacheable
211 // metadata is bubbled up.
212 $render_context = new RenderContext();
213 $callable = function () use ($form_class, &$form_state) {
214 return \Drupal::formBuilder()->buildForm($form_class, $form_state);
216 $form = $renderer->executeInRenderContext($render_context, $callable);
218 if (!$render_context->isEmpty()) {
219 BubbleableMetadata::createFromRenderArray($form)
220 ->merge($render_context->pop())
223 $output = $renderer->renderRoot($form);
225 // These forms have the title built in, so set the title here:
226 $title = $form_state->get('title') ?: '';
228 if ($ajax && (!$form_state->isExecuted() || $form_state->get('rerender'))) {
229 // If the form didn't execute and we're using ajax, build up an
230 // Ajax command list to execute.
231 $response = new AjaxResponse();
233 // Attach the library necessary for using the OpenModalDialogCommand and
234 // set the attachments for this Ajax response.
235 $form['#attached']['library'][] = 'core/drupal.dialog.ajax';
236 $response->setAttachments($form['#attached']);
239 $status_messages = ['#type' => 'status_messages'];
240 if ($messages = $renderer->renderRoot($status_messages)) {
241 $display = '<div class="views-messages">' . $messages . '</div>';
246 'dialogClass' => 'views-ui-dialog js-views-ui-dialog',
250 $response->addCommand(new OpenModalDialogCommand($title, $display, $options));
252 // Views provides its own custom handling of AJAX form submissions.
253 // Usually this happens at the same path, but custom paths may be
254 // specified in $form_state.
255 $form_url = $form_state->has('url') ? $form_state->get('url')->toString() : $this->url('<current>');
256 $response->addCommand(new SetFormCommand($form_url));
258 if ($section = $form_state->get('#section')) {
259 $response->addCommand(new HighlightCommand('.' . Html::cleanCssIdentifier($section)));
265 return $title ? ['#title' => $title, '#markup' => $output] : $output;
271 public function validateForm(array &$form, FormStateInterface $form_state) {
277 public function submitForm(array &$form, FormStateInterface $form_state) {