ConfigItem.php 9.64 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
<?php

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

namespace Drupal\views_ui\Form\Ajax;

use Drupal\views\ViewStorageInterface;
use Drupal\views\ViewExecutable;
12
use Drupal\views\Views;
13
14
15
16
17
18
19
20
21
22
23
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98

/**
 * Provides a form for configuring an item in the Views UI.
 */
class ConfigItem extends ViewsFormBase {

  /**
   * Constucts a new ConfigItem object.
   */
  public function __construct($type = NULL, $id = NULL) {
    $this->setType($type);
    $this->setID($id);
  }

  /**
   * Implements \Drupal\views_ui\Form\Ajax\ViewsFormInterface::getFormKey().
   */
  public function getFormKey() {
    return 'config-item';
  }

  /**
   * Overrides \Drupal\views_ui\Form\Ajax\ViewsFormBase::getForm().
   */
  public function getForm(ViewStorageInterface $view, $display_id, $js, $type = NULL, $id = NULL) {
    $this->setType($type);
    $this->setID($id);
    return parent::getForm($view, $display_id, $js);
  }

  /**
   * Implements \Drupal\Core\Form\FormInterface::getFormID().
   */
  public function getFormID() {
    return 'views_ui_config_item_form';
  }

  /**
   * Implements \Drupal\Core\Form\FormInterface::buildForm().
   */
  public function buildForm(array $form, array &$form_state) {
    $view = &$form_state['view'];
    $display_id = $form_state['display_id'];
    $type = $form_state['type'];
    $id = $form_state['id'];

    $form = array(
      'options' => array(
        '#tree' => TRUE,
        '#theme_wrappers' => array('container'),
        '#attributes' => array('class' => array('scroll')),
      ),
    );
    $executable = $view->get('executable');
    $save_ui_cache = FALSE;
    if (!$executable->setDisplay($display_id)) {
      views_ajax_error(t('Invalid display id @display', array('@display' => $display_id)));
    }
    $item = $executable->getItem($display_id, $type, $id);

    if ($item) {
      $handler = $executable->display_handler->getHandler($type, $id);
      if (empty($handler)) {
        $form['markup'] = array('#markup' => t("Error: handler for @table > @field doesn't exist!", array('@table' => $item['table'], '@field' => $item['field'])));
      }
      else {
        $types = ViewExecutable::viewsHandlerTypes();

        // If this item can come from the default display, show a dropdown
        // that lets the user choose which display the changes should apply to.
        if ($executable->display_handler->defaultableSections($types[$type]['plural'])) {
          $form_state['section'] = $types[$type]['plural'];
          views_ui_standard_display_dropdown($form, $form_state, $form_state['section']);
        }

        // A whole bunch of code to figure out what relationships are valid for
        // this item.
        $relationships = $executable->display_handler->getOption('relationships');
        $relationship_options = array();

        foreach ($relationships as $relationship) {
          // relationships can't link back to self. But also, due to ordering,
          // relationships can only link to prior relationships.
          if ($type == 'relationship' && $id == $relationship['id']) {
            break;
          }
99
          $relationship_handler = views_get_handler($relationship, 'relationship');
100
101
102
103
104
105
          // ignore invalid/broken relationships.
          if (empty($relationship_handler)) {
            continue;
          }

          // If this relationship is valid for this type, add it to the list.
106
          $data = Views::viewsData()->get($relationship['table']);
107
108
109
110
          $base = $data[$relationship['field']]['relationship']['base'];
          $base_fields = views_fetch_fields($base, $form_state['type'], $executable->display_handler->useGroupBy());
          if (isset($base_fields[$item['table'] . '.' . $item['field']])) {
            $relationship_handler->init($executable, $executable->display_handler, $relationship);
111
            $relationship_options[$relationship['id']] = $relationship_handler->adminLabel();
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
          }
        }

        if (!empty($relationship_options)) {
          // Make sure the existing relationship is even valid. If not, force
          // it to none.
          $base_fields = views_fetch_fields($view->get('base_table'), $form_state['type'], $executable->display_handler->useGroupBy());
          if (isset($base_fields[$item['table'] . '.' . $item['field']])) {
            $relationship_options = array_merge(array('none' => t('Do not use a relationship')), $relationship_options);
          }
          $rel = empty($item['relationship']) ? 'none' : $item['relationship'];
          if (empty($relationship_options[$rel])) {
            // Pick the first relationship.
            $rel = key($relationship_options);
            // We want this relationship option to get saved even if the user
            // skips submitting the form.
            $executable->setItemOption($display_id, $type, $id, 'relationship', $rel);
            $save_ui_cache = TRUE;
          }

          $form['options']['relationship'] = array(
            '#type' => 'select',
            '#title' => t('Relationship'),
            '#options' => $relationship_options,
            '#default_value' => $rel,
            '#weight' => -500,
          );
        }
        else {
          $form['options']['relationship'] = array(
            '#type' => 'value',
            '#value' => 'none',
          );
        }

        $form['#title'] = t('Configure @type: @item', array('@type' => $types[$type]['lstitle'], '@item' => $handler->adminLabel()));

        if (!empty($handler->definition['help'])) {
          $form['options']['form_description'] = array(
            '#markup' => $handler->definition['help'],
            '#theme_wrappers' => array('container'),
            '#attributes' => array('class' => array('form-item description')),
            '#weight' => -1000,
          );
        }

        $form['#section'] = $display_id . '-' . $type . '-' . $id;

        // Get form from the handler.
        $handler->buildOptionsForm($form['options'], $form_state);
        $form_state['handler'] = &$handler;
      }

      $name = NULL;
      if (isset($form_state['update_name'])) {
        $name = $form_state['update_name'];
      }

      $view->getStandardButtons($form, $form_state, 'views_ui_config_item_form', $name);
      // Add a 'remove' button.
      $form['buttons']['remove'] = array(
        '#type' => 'submit',
        '#value' => t('Remove'),
        '#submit' => array(array($this, 'remove')),
        '#limit_validation_errors' => array(array('override')),
      );
    }

    if ($save_ui_cache) {
181
      $view->cacheSet();
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
    }

    return $form;
  }

  /**
   * Overrides \Drupal\views_ui\Form\Ajax\ViewsFormBase::validateForm().
   */
  public function validateForm(array &$form, array &$form_state) {
    $form_state['handler']->validateOptionsForm($form['options'], $form_state);

    if (form_get_errors()) {
      $form_state['rerender'] = TRUE;
    }
  }

  /**
   * Overrides \Drupal\views_ui\Form\Ajax\ViewsFormBase::submitForm().
   */
  public function submitForm(array &$form, array &$form_state) {
    // Run it through the handler's submit function.
    $form_state['handler']->submitOptionsForm($form['options'], $form_state);
    $item = $form_state['handler']->options;
    $types = ViewExecutable::viewsHandlerTypes();

    // For footer/header $handler_type is area but $type is footer/header.
    // For all other handle types it's the same.
    $handler_type = $type = $form_state['type'];
    if (!empty($types[$type]['type'])) {
      $handler_type = $types[$type]['type'];
    }

    $override = NULL;
    $executable = $form_state['view']->get('executable');
    if ($executable->display_handler->useGroupBy() && !empty($item['group_type'])) {
      if (empty($executable->query)) {
        $executable->initQuery();
      }
220
      $aggregate = $executable->query->getAggregationInfo();
221
222
223
224
225
226
227
      if (!empty($aggregate[$item['group_type']]['handler'][$type])) {
        $override = $aggregate[$item['group_type']]['handler'][$type];
      }
    }

    // Create a new handler and unpack the options from the form onto it. We
    // can use that for storage.
228
    $handler = views_get_handler($item, $handler_type, $override);
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
    $handler->init($executable, $executable->display_handler, $item);

    // Add the incoming options to existing options because items using
    // the extra form may not have everything in the form here.
    $options = $form_state['values']['options'] + $form_state['handler']->options;

    // This unpacks only options that are in the definition, ensuring random
    // extra stuff on the form is not sent through.
    $handler->unpackOptions($handler->options, $options, NULL, FALSE);

    // Store the item back on the view
    $executable->setItem($form_state['display_id'], $form_state['type'], $form_state['id'], $handler->options);

    // Ensure any temporary options are removed.
    if (isset($form_state['view']->temporary_options[$type][$form_state['id']])) {
      unset($form_state['view']->temporary_options[$type][$form_state['id']]);
    }

    // Write to cache
248
    $form_state['view']->cacheSet();
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
  }

  /**
   * Submit handler for removing an item from a view
   */
  public function remove(&$form, &$form_state) {
    // Store the item back on the view
    list($was_defaulted, $is_defaulted) = $form_state['view']->getOverrideValues($form, $form_state);
    $executable = $form_state['view']->get('executable');
    // If the display selection was changed toggle the override value.
    if ($was_defaulted != $is_defaulted) {
      $display = &$executable->displayHandlers->get($form_state['display_id']);
      $display->optionsOverride($form, $form_state);
    }
    $executable->removeItem($form_state['display_id'], $form_state['type'], $form_state['id']);

    // Write to cache
266
    $form_state['view']->cacheSet();
267
268
269
  }

}