ExposedFormPluginBase.php 10.4 KB
Newer Older
merlinofchaos's avatar
merlinofchaos committed
1
2
3
4
<?php

/**
 * @file
aspilicious's avatar
aspilicious committed
5
 * Definition of Drupal\views\Plugin\views\exposed_form\ExposedFormPluginBase.
merlinofchaos's avatar
merlinofchaos committed
6
7
 */

8
namespace Drupal\views\Plugin\views\exposed_form;
9

10
use Drupal\views\ViewExecutable;
11
use Drupal\views\Plugin\views\display\DisplayPluginBase;
12
use Drupal\views\Plugin\views\PluginBase;
13

merlinofchaos's avatar
merlinofchaos committed
14
15
16
17
18
19
20
21
22
23
24
/**
 * @defgroup views_exposed_form_plugins Views exposed form plugins
 * @{
 * Plugins that handle the validation/submission and rendering of exposed forms.
 *
 * If needed, it is possible to use them to add additional form elements.
 */

/**
 * The base plugin to handle exposed filter forms.
 */
25
abstract class ExposedFormPluginBase extends PluginBase {
merlinofchaos's avatar
merlinofchaos committed
26

27
28
29
  /**
   * Overrides Drupal\views\Plugin\Plugin::$usesOptions.
   */
30
  protected $usesOptions = TRUE;
31

32
33
  protected function defineOptions() {
    $options = parent::defineOptions();
34
    $options['submit_button'] = array('default' => 'Apply', 'translatable' => TRUE);
merlinofchaos's avatar
merlinofchaos committed
35
36
37
    $options['reset_button'] = array('default' => FALSE, 'bool' => TRUE);
    $options['reset_button_label'] = array('default' => 'Reset', 'translatable' => TRUE);
    $options['exposed_sorts_label'] = array('default' => 'Sort by', 'translatable' => TRUE);
38
    $options['expose_sort_order'] = array('default' => TRUE, 'bool' => TRUE);
merlinofchaos's avatar
merlinofchaos committed
39
40
41
42
43
    $options['sort_asc_label'] = array('default' => 'Asc', 'translatable' => TRUE);
    $options['sort_desc_label'] = array('default' => 'Desc', 'translatable' => TRUE);
    return $options;
  }

44
45
  public function buildOptionsForm(&$form, &$form_state) {
    parent::buildOptionsForm($form, $form_state);
merlinofchaos's avatar
merlinofchaos committed
46
47
48
49
50
51
52
    $form['submit_button'] = array(
      '#type' => 'textfield',
      '#title' => t('Submit button text'),
      '#default_value' => $this->options['submit_button'],
      '#required' => TRUE,
    );

53
    $form['reset_button'] = array(
merlinofchaos's avatar
merlinofchaos committed
54
      '#type' => 'checkbox',
55
      '#title' => t('Include reset button (resets all applied exposed filters).'),
merlinofchaos's avatar
merlinofchaos committed
56
57
58
59
60
61
62
63
64
      '#default_value' => $this->options['reset_button'],
    );

    $form['reset_button_label'] = array(
     '#type' => 'textfield',
      '#title' => t('Reset button label'),
      '#description' => t('Text to display in the reset button of the exposed form.'),
      '#default_value' => $this->options['reset_button_label'],
      '#required' => TRUE,
65
66
67
68
      '#states' => array(
        'invisible' => array(
          'input[name="exposed_form_options[reset_button]"]' => array('checked' => FALSE),
        ),
merlinofchaos's avatar
merlinofchaos committed
69
70
71
72
73
74
75
76
77
78
      ),
    );

    $form['exposed_sorts_label'] = array(
      '#type' => 'textfield',
      '#title' => t('Exposed sorts label'),
      '#default_value' => $this->options['exposed_sorts_label'],
      '#required' => TRUE,
    );

79
80
    $form['expose_sort_order'] = array(
      '#type' => 'checkbox',
81
82
      '#title' => t('Allow people to choose the sort order'),
      '#description' => t('If sort order is not exposed, the sort criteria settings for each sort will determine its order.'),
83
84
85
      '#default_value' => $this->options['expose_sort_order'],
    );

merlinofchaos's avatar
merlinofchaos committed
86
87
    $form['sort_asc_label'] = array(
      '#type' => 'textfield',
88
      '#title' => t('Label for ascending sort'),
merlinofchaos's avatar
merlinofchaos committed
89
90
      '#default_value' => $this->options['sort_asc_label'],
      '#required' => TRUE,
91
92
93
94
95
      '#states' => array(
        'visible' => array(
          'input[name="exposed_form_options[expose_sort_order]"]' => array('checked' => TRUE),
        ),
      ),
merlinofchaos's avatar
merlinofchaos committed
96
97
98
99
    );

    $form['sort_desc_label'] = array(
      '#type' => 'textfield',
100
      '#title' => t('Label for descending sort'),
merlinofchaos's avatar
merlinofchaos committed
101
102
      '#default_value' => $this->options['sort_desc_label'],
      '#required' => TRUE,
103
104
105
106
107
      '#states' => array(
        'visible' => array(
          'input[name="exposed_form_options[expose_sort_order]"]' => array('checked' => TRUE),
        ),
      ),
merlinofchaos's avatar
merlinofchaos committed
108
109
110
111
112
113
114
115
116
117
    );
  }

  /**
   * Render the exposed filter form.
   *
   * This actually does more than that; because it's using FAPI, the form will
   * also assign data to the appropriate handlers for use in building the
   * query.
   */
118
  public function renderExposedForm($block = FALSE) {
merlinofchaos's avatar
merlinofchaos committed
119
120
121
    // Deal with any exposed filters we may have, before building.
    $form_state = array(
      'view' => &$this->view,
122
      'display' => &$this->view->display_handler->display,
merlinofchaos's avatar
merlinofchaos committed
123
124
125
126
127
128
129
130
131
      'method' => 'get',
      'rerender' => TRUE,
      'no_redirect' => TRUE,
      'always_process' => TRUE,
    );

    // Some types of displays (eg. attachments) may wish to use the exposed
    // filters of their parent displays instead of showing an additional
    // exposed filter form for the attachment as well as that for the parent.
132
    if (!$this->view->display_handler->displaysExposed() || (!$block && $this->view->display_handler->getOption('exposed_block'))) {
merlinofchaos's avatar
merlinofchaos committed
133
134
135
136
137
138
139
140
141
142
143
      unset($form_state['rerender']);
    }

    if (!empty($this->ajax)) {
      $form_state['ajax'] = TRUE;
    }

    $form_state['exposed_form_plugin'] = $this;
    $form = drupal_build_form('views_exposed_form', $form_state);
    $output = drupal_render($form);

144
    if (!$this->view->display_handler->displaysExposed() || (!$block && $this->view->display_handler->getOption('exposed_block'))) {
merlinofchaos's avatar
merlinofchaos committed
145
146
147
148
149
150
151
      return "";
    }
    else {
      return $output;
    }
  }

152
  public function query() {
merlinofchaos's avatar
merlinofchaos committed
153
154
155
156
157
158
159
160
161
    $view = $this->view;
    $exposed_data = isset($view->exposed_data) ? $view->exposed_data : array();
    $sort_by = isset($exposed_data['sort_by']) ? $exposed_data['sort_by'] : NULL;
    if (!empty($sort_by)) {
      // Make sure the original order of sorts is preserved
      // (e.g. a sticky sort is often first)
      if (isset($view->sort[$sort_by])) {
        $view->query->orderby = array();
        foreach ($view->sort as $key => $sort) {
162
          if (!$sort->isExposed()) {
merlinofchaos's avatar
merlinofchaos committed
163
164
            $sort->query();
          }
165
          elseif ($key == $sort_by) {
merlinofchaos's avatar
merlinofchaos committed
166
167
168
            if (isset($exposed_data['sort_order']) && in_array($exposed_data['sort_order'], array('ASC', 'DESC'))) {
              $sort->options['order'] = $exposed_data['sort_order'];
            }
169
            $sort->setRelationship();
merlinofchaos's avatar
merlinofchaos committed
170
171
172
173
174
175
176
177
178
179
180
            $sort->query();
          }
        }
      }
    }
  }

  function pre_render($values) { }

  function post_render(&$output) { }

181
  public function preExecute() { }
merlinofchaos's avatar
merlinofchaos committed
182

183
  public function postExecute() { }
merlinofchaos's avatar
merlinofchaos committed
184

185
  public function exposedFormAlter(&$form, &$form_state) {
merlinofchaos's avatar
merlinofchaos committed
186
187
188
189
    $form['submit']['#value'] = $this->options['submit_button'];
    // Check if there is exposed sorts for this view
    $exposed_sorts = array();
    foreach ($this->view->sort as $id => $handler) {
190
      if ($handler->canExpose() && $handler->isExposed()) {
merlinofchaos's avatar
merlinofchaos committed
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
        $exposed_sorts[$id] = check_plain($handler->options['expose']['label']);
      }
    }

    if (count($exposed_sorts)) {
      $form['sort_by'] = array(
        '#type' => 'select',
        '#options' => $exposed_sorts,
        '#title' => $this->options['exposed_sorts_label'],
      );
      $sort_order = array(
        'ASC' => $this->options['sort_asc_label'],
        'DESC' => $this->options['sort_desc_label'],
      );
      if (isset($form_state['input']['sort_by']) && isset($this->view->sort[$form_state['input']['sort_by']])) {
        $default_sort_order = $this->view->sort[$form_state['input']['sort_by']]->options['order'];
207
208
      }
      else {
merlinofchaos's avatar
merlinofchaos committed
209
210
211
212
213
214
215
216
217
        $first_sort = reset($this->view->sort);
        $default_sort_order = $first_sort->options['order'];
      }

      if (!isset($form_state['input']['sort_by'])) {
        $keys = array_keys($exposed_sorts);
        $form_state['input']['sort_by'] = array_shift($keys);
      }

218
219
220
221
222
223
224
225
      if ($this->options['expose_sort_order']) {
        $form['sort_order'] = array(
          '#type' => 'select',
          '#options' => $sort_order,
          '#title' => t('Order'),
          '#default_value' => $default_sort_order,
        );
      }
merlinofchaos's avatar
merlinofchaos committed
226
      $form['submit']['#weight'] = 10;
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
    }

    if (!empty($this->options['reset_button'])) {
      $form['reset'] = array(
        '#value' => $this->options['reset_button_label'],
        '#type' => 'submit',
        '#weight' => 10,
      );

      // Get an array of exposed filters, keyed by identifier option.
      foreach ($this->view->filter as $id => $handler) {
        if ($handler->canExpose() && $handler->isExposed() && !empty($handler->options['expose']['identifier'])) {
          $exposed_filters[$handler->options['expose']['identifier']] = $id;
        }
      }
      $all_exposed = array_merge($exposed_sorts, $exposed_filters);

      // Set the access to FALSE if there is no exposed input.
      if (!array_intersect_key($all_exposed, $this->view->exposed_input)) {
        $form['reset']['#access'] = FALSE;
merlinofchaos's avatar
merlinofchaos committed
247
248
249
      }
    }

250
    $pager = $this->view->display_handler->getPlugin('pager');
merlinofchaos's avatar
merlinofchaos committed
251
    if ($pager) {
252
      $pager->exposedFormAlter($form, $form_state);
merlinofchaos's avatar
merlinofchaos committed
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
      $form_state['pager_plugin'] = $pager;
    }
  }

  function exposed_form_validate(&$form, &$form_state) {
    if (isset($form_state['pager_plugin'])) {
      $form_state['pager_plugin']->exposed_form_validate($form, $form_state);
    }
  }

  /**
  * This function is executed when exposed form is submited.
  *
  * @param $form
  *   Nested array of form elements that comprise the form.
  * @param $form_state
  *   A keyed array containing the current state of the form.
  * @param $exclude
  *   Nested array of keys to exclude of insert into
  *   $view->exposed_raw_input
  */
274
  public function exposedFormSubmit(&$form, &$form_state, &$exclude) {
merlinofchaos's avatar
merlinofchaos committed
275
    if (!empty($form_state['values']['op']) && $form_state['values']['op'] == $this->options['reset_button_label']) {
276
      $this->resetForm($form, $form_state);
merlinofchaos's avatar
merlinofchaos committed
277
278
    }
    if (isset($form_state['pager_plugin'])) {
279
      $form_state['pager_plugin']->exposedFormSubmit($form, $form_state, $exclude);
merlinofchaos's avatar
merlinofchaos committed
280
281
282
283
      $exclude[] = 'pager_plugin';
    }
  }

284
  public function resetForm(&$form, &$form_state) {
merlinofchaos's avatar
merlinofchaos committed
285
286
287
288
289
290
    // _SESSION is not defined for users who are not logged in.

    // If filters are not overridden, store the 'remember' settings on the
    // default display. If they are, store them on this display. This way,
    // multiple displays in the same view can share the same filters and
    // remember settings.
291
    $display_id = ($this->view->display_handler->isDefaulted('filters')) ? 'default' : $this->view->current_display;
merlinofchaos's avatar
merlinofchaos committed
292

293
294
    if (isset($_SESSION['views'][$this->view->storage->id()][$display_id])) {
      unset($_SESSION['views'][$this->view->storage->id()][$display_id]);
merlinofchaos's avatar
merlinofchaos committed
295
296
297
298
299
300
301
302
303
304
305
    }

    // Set the form to allow redirect.
    if (empty($this->view->live_preview)) {
      $form_state['no_redirect'] = FALSE;
    }
    else {
      $form_state['rebuild'] = TRUE;
      $this->view->exposed_data = array();
    }

306
    $form_state['redirect'] = current_path();
merlinofchaos's avatar
merlinofchaos committed
307
308
    $form_state['values'] = array();
  }
309

merlinofchaos's avatar
merlinofchaos committed
310
311
312
313
314
}

/**
 * @}
 */