ViewsFormBase.php 6.33 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php

/**
 * @file
 * Contains \Drupal\views_ui\Form\Ajax\ViewsFormBase.
 */

namespace Drupal\views_ui\Form\Ajax;

10
use Drupal\Component\Utility\Html;
11
use Drupal\Core\Form\FormBase;
12 13
use Drupal\Core\Form\FormState;
use Drupal\Core\Form\FormStateInterface;
14 15 16
use Drupal\views\ViewStorageInterface;
use Drupal\views\Ajax;
use Drupal\Core\Ajax\AjaxResponse;
17
use Drupal\Core\Ajax\CloseModalDialogCommand;
18 19 20 21 22
use Symfony\Component\HttpFoundation\RedirectResponse;

/**
 * Provides a base class for Views UI AJAX forms.
 */
23
abstract class ViewsFormBase extends FormBase implements ViewsFormInterface {
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63

  /**
   * The ID of the item this form is manipulating.
   *
   * @var string
   */
  protected $id;

  /**
   * The type of item this form is manipulating.
   *
   * @var string
   */
  protected $type;

  /**
   * Sets the ID for this form.
   *
   * @param string $id
   *   The ID of the item this form is manipulating.
   */
  protected function setID($id) {
    if ($id) {
      $this->id = $id;
    }
  }

  /**
   * Sets the type for this form.
   *
   * @param string $type
   *   The type of the item this form is manipulating.
   */
  protected function setType($type) {
    if ($type) {
      $this->type = $type;
    }
  }

  /**
64
   * {@inheritdoc}
65 66 67 68
   */
  public function getFormState(ViewStorageInterface $view, $display_id, $js) {
    // $js may already have been converted to a Boolean.
    $ajax = is_string($js) ? $js === 'ajax' : $js;
69
    return new FormState(array(
70
      'form_id' => $this->getFormId(),
71 72 73 74 75 76 77 78 79 80 81
      'form_key' => $this->getFormKey(),
      'ajax' => $ajax,
      'display_id' => $display_id,
      'view' => $view,
      'type' => $this->type,
      'id' => $this->id,
      'no_redirect' => TRUE,
      'build_info' => array(
        'args' => array(),
        'callback_object' => $this,
      ),
82
    ));
83 84 85
  }

  /**
86
   * {@inheritdoc}
87 88 89 90 91 92 93
   */
  public function getForm(ViewStorageInterface $view, $display_id, $js) {
    $form_state = $this->getFormState($view, $display_id, $js);
    $view = $form_state['view'];
    $key = $form_state['form_key'];

    // @todo Remove the need for this.
94 95
    \Drupal::moduleHandler()->loadInclude('views_ui', 'inc', 'admin');
    \Drupal::moduleHandler()->loadInclude('views', 'inc', 'includes/ajax');
96 97 98 99

    // Reset the cache of IDs. Drupal rather aggressively prevents ID
    // duplication but this causes it to remember IDs that are no longer even
    // being used.
100
    Html::resetSeenIds();
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126

    // check to see if this is the top form of the stack. If it is, pop
    // it off; if it isn't, the user clicked somewhere else and the stack is
    // now irrelevant.
    if (!empty($view->stack)) {
      $identifier = implode('-', array_filter(array($key, $view->id(), $display_id, $form_state['type'], $form_state['id'])));
      // Retrieve the first form from the stack without changing the integer keys,
      // as they're being used for the "2 of 3" progress indicator.
      reset($view->stack);
      list($key, $top) = each($view->stack);
      unset($view->stack[$key]);

      if (array_shift($top) != $identifier) {
        $view->stack = array();
      }
    }

    // Automatically remove the form cache if it is set and the key does
    // not match. This way navigating away from the form without hitting
    // update will work.
    if (isset($view->form_cache) && $view->form_cache['key'] != $key) {
      unset($view->form_cache);
    }

    // With the below logic, we may end up rendering a form twice (or two forms
    // each sharing the same element ids), potentially resulting in
127
    // _drupal_add_js() being called twice to add the same setting. drupal_get_js()
128 129 130
    // is ok with that, but until \Drupal\Core\Ajax\AjaxResponse::ajaxRender()
    // is (http://drupal.org/node/208611), reset the _drupal_add_js() static
    // before rendering the second time.
131 132
    $drupal_add_js_original = _drupal_add_js();
    $drupal_add_js = &drupal_static('_drupal_add_js');
133 134
    $form_class = get_class($form_state['build_info']['callback_object']);
    $response = views_ajax_form_wrapper($form_class, $form_state);
135 136 137 138 139 140 141 142 143 144 145 146 147 148

    // If the form has not been submitted, or was not set for rerendering, stop.
    if (!$form_state['submitted'] || !empty($form_state['rerender'])) {
      return $response;
    }

    // Sometimes we need to re-generate the form for multi-step type operations.
    if (!empty($view->stack)) {
      $drupal_add_js = $drupal_add_js_original;
      $stack = $view->stack;
      $top = array_shift($stack);

      // Build the new form state for the next form in the stack.
      $reflection = new \ReflectionClass($view::$forms[$top[1]]);
149
      /** @var $form_state \Drupal\Core\Form\FormStateInterface */
150
      $form_state = $reflection->newInstanceArgs(array_slice($top, 3, 2))->getFormState($view, $top[2], $form_state['ajax']);
151
      $form_class = get_class($form_state['build_info']['callback_object']);
152

153
      $form_state->setUserInput(array());
154
      $form_path = views_ui_build_form_path($form_state);
155
      if (!$form_state['ajax']) {
156
        return new RedirectResponse(url($form_path, array('absolute' => TRUE)));
157
      }
158
      $form_state['path'] = $form_path;
159
      $response = views_ajax_form_wrapper($form_class, $form_state);
160 161 162
    }
    elseif (!$form_state['ajax']) {
      // if nothing on the stack, non-js forms just go back to the main view editor.
163 164
      $display_id = $form_state->get('display_id');
      return new RedirectResponse(url("admin/structure/views/view/{$view->id()}/edit/$display_id", array('absolute' => TRUE)));
165 166 167
    }
    else {
      $response = new AjaxResponse();
168
      $response->addCommand(new CloseModalDialogCommand());
169
      $response->addCommand(new Ajax\ShowButtonsCommand(!empty($view->changed)));
170 171 172 173 174 175 176 177
      $response->addCommand(new Ajax\TriggerPreviewCommand());
      if (!empty($form_state['#page_title'])) {
        $response->addCommand(new Ajax\ReplaceTitleCommand($form_state['#page_title']));
      }
    }
    // If this form was for view-wide changes, there's no need to regenerate
    // the display section of the form.
    if ($display_id !== '') {
178
      \Drupal::entityManager()->getFormObject('view', 'edit')->rebuildCurrentTab($view, $response, $display_id);
179 180 181 182 183 184
    }

    return $response;
  }

  /**
185
   * {@inheritdoc}
186
   */
187
  public function validateForm(array &$form, FormStateInterface $form_state) {
188 189 190
  }

  /**
191
   * {@inheritdoc}
192
   */
193
  public function submitForm(array &$form, FormStateInterface $form_state) {
194 195 196
  }

}