9 * @defgroup views_filter_handlers Views filter handlers
11 * Handlers to tell Views how to filter queries.
14 * - allow empty: If true, the 'IS NULL' and 'IS NOT NULL' operators become
15 * available as standard operators.
18 * You can set some specific behavior by setting up the following flags on
22 * Disable the possibility to force a single value.
24 * Disable the possibility to use operators.
26 * Disable the possibility to allow a exposed input to be optional.
30 * Base class for filters.
32 * @ingroup views_filter_handlers
34 class views_handler_filter extends views_handler {
36 * Contains the actual value of the field,either configured in the views ui
37 * or entered in the exposed filters.
42 * Contains the operator which is used on the query.
47 * Contains the information of the selected item in a gruped filter.
49 var $group_info = NULL;
53 * Disable the possibility to force a single value.
55 var $always_multiple = FALSE;
59 * Disable the possibility to use operators.
61 var $no_operator = FALSE;
65 * Disable the possibility to allow a exposed input to be optional.
67 var $always_required = FALSE;
70 * Provide some extra help to get the operator/value easier to use.
72 * This likely has to be overridden by filters which are more complex
73 * than simple operator/value.
75 function init(&$view, &$options) {
76 parent::init($view, $options);
78 $this->operator = $this->options['operator'];
79 $this->value = $this->options['value'];
80 $this->group_info = $this->options['group_info']['default_group'];
82 // Compatibility: The new UI changed several settings.
83 if (!empty($options['exposed']) && !empty($options['expose']['optional']) && !isset($options['expose']['required'])) {
84 $this->options['expose']['required'] = !$options['expose']['optional'];
86 if (!empty($options['exposed']) && !empty($options['expose']['single']) && !isset($options['expose']['multiple'])) {
87 $this->options['expose']['multiple'] = !$options['expose']['single'];
89 if (!empty($options['exposed']) && !empty($options['expose']['operator']) && !isset($options['expose']['operator_id'])) {
90 $this->options['expose']['operator_id'] = $options['expose']['operator_id'] = $options['expose']['operator'];
93 if ($this->multiple_exposed_input()) {
94 $this->group_info = NULL;
95 if (!empty($options['group_info']['default_group_multiple'])) {
96 $this->group_info = array_filter($options['group_info']['default_group_multiple']);
99 $this->options['expose']['multiple'] = TRUE;
102 // If there are relationships in the view, allow empty should be true
103 // so that we can do IS NULL checks on items. Not all filters respect
104 // allow empty, but string and numeric do and that covers enough.
105 if ($this->view->display_handler->get_option('relationships')) {
106 $this->definition['allow empty'] = TRUE;
110 function option_definition() {
111 $options = parent::option_definition();
113 $options['operator'] = array('default' => '=');
114 $options['value'] = array('default' => '');
115 $options['group'] = array('default' => '1');
116 $options['exposed'] = array('default' => FALSE, 'bool' => TRUE);
117 $options['expose'] = array(
119 'operator_id' => array('default' => FALSE),
120 'label' => array('default' => '', 'translatable' => TRUE),
121 'description' => array('default' => '', 'translatable' => TRUE),
122 'use_operator' => array('default' => FALSE, 'bool' => TRUE),
123 'operator_label' => array('default' => '', 'translatable' => TRUE),
124 'operator' => array('default' => ''),
125 'identifier' => array('default' => ''),
126 'required' => array('default' => FALSE, 'bool' => TRUE),
127 'remember' => array('default' => FALSE, 'bool' => TRUE),
128 'multiple' => array('default' => FALSE, 'bool' => TRUE),
129 'remember_roles' => array('default' => array(
130 DRUPAL_AUTHENTICATED_RID => DRUPAL_AUTHENTICATED_RID,
135 // A group is a combination of a filter, an operator and a value
136 // operating like a single filter.
137 // Users can choose from a select box which group they want to apply.
138 // Views will filter the view according to the defined values.
139 // Because it acts as a standard filter, we have to define
140 // an identifier and other settings like the widget and the label.
141 // This settings are saved in another array to allow users to switch
142 // between a normal filter and a group of filters with a single click.
143 $options['is_grouped'] = array('default' => FALSE, 'bool' => TRUE);
144 $options['group_info'] = array(
146 'label' => array('default' => '', 'translatable' => TRUE),
147 'description' => array('default' => '', 'translatable' => TRUE),
148 'identifier' => array('default' => ''),
149 'optional' => array('default' => TRUE, 'bool' => TRUE),
150 'widget' => array('default' => 'select'),
151 'multiple' => array('default' => FALSE, 'bool' => TRUE),
152 'remember' => array('default' => 0),
153 'default_group' => array('default' => 'All'),
154 'default_group_multiple' => array('default' => array()),
155 'group_items' => array('default' => array()),
163 * Display the filter on the administrative summary
165 function admin_summary() {
166 return check_plain((string) $this->operator) . ' ' . check_plain((string) $this->value);
170 * Determine if a filter can be exposed.
172 function can_expose() { return TRUE; }
175 * Determine if a filter can be converted into a group.
176 * Only exposed filters with operators available can be converted into groups.
178 function can_build_group() {
179 return $this->is_exposed() && (count($this->operator_options()) > 0);
183 * Returns TRUE if the exposed filter works like a grouped filter.
185 function is_a_group() {
186 return $this->is_exposed() && !empty($this->options['is_grouped']);
190 * Provide the basic form which calls through to subforms.
191 * If overridden, it is best to call through to the parent,
192 * or to at least make sure all of the functions in this form
195 function options_form(&$form, &$form_state) {
196 parent::options_form($form, $form_state);
197 if ($this->can_expose()) {
198 $this->show_expose_button($form, $form_state);
200 if ($this->can_build_group()) {
201 $this->show_build_group_button($form, $form_state);
203 $form['clear_markup_start'] = array(
204 '#markup' => '<div class="clearfix">',
206 if ($this->is_a_group()) {
207 if ($this->can_build_group()) {
208 $form['clear_markup_start'] = array(
209 '#markup' => '<div class="clearfix">',
211 // Render the build group form.
212 $this->show_build_group_form($form, $form_state);
213 $form['clear_markup_end'] = array(
214 '#markup' => '</div>',
219 // Add the subform from operator_form().
220 $this->show_operator_form($form, $form_state);
221 // Add the subform from value_form().
222 $this->show_value_form($form, $form_state);
223 $form['clear_markup_end'] = array(
224 '#markup' => '</div>',
226 if ($this->can_expose()) {
227 // Add the subform from expose_form().
228 $this->show_expose_form($form, $form_state);
234 * Simple validate handler
236 function options_validate(&$form, &$form_state) {
237 $this->operator_validate($form, $form_state);
238 $this->value_validate($form, $form_state);
239 if (!empty($this->options['exposed']) && !$this->is_a_group()) {
240 $this->expose_validate($form, $form_state);
242 if ($this->is_a_group()) {
243 $this->build_group_validate($form, $form_state);
248 * Simple submit handler
250 function options_submit(&$form, &$form_state) {
251 unset($form_state['values']['expose_button']); // don't store this.
252 unset($form_state['values']['group_button']); // don't store this.
253 if (!$this->is_a_group()) {
254 $this->operator_submit($form, $form_state);
255 $this->value_submit($form, $form_state);
257 if (!empty($this->options['exposed'])) {
258 $this->expose_submit($form, $form_state);
260 if ($this->is_a_group()) {
261 $this->build_group_submit($form, $form_state);
266 * Shortcut to display the operator form.
268 function show_operator_form(&$form, &$form_state) {
269 $this->operator_form($form, $form_state);
270 $form['operator']['#prefix'] = '<div class="views-group-box views-left-30">';
271 $form['operator']['#suffix'] = '</div>';
275 * Options form subform for setting the operator.
277 * This may be overridden by child classes, and it must
278 * define $form['operator'];
280 * @see options_form()
282 function operator_form(&$form, &$form_state) {
283 $options = $this->operator_options();
284 if (!empty($options)) {
285 $form['operator'] = array(
286 '#type' => count($options) < 10 ? 'radios' : 'select',
287 '#title' => t('Operator'),
288 '#default_value' => $this->operator,
289 '#options' => $options,
295 * Provide a list of options for the default operator form.
296 * Should be overridden by classes that don't override operator_form
298 function operator_options() { return array(); }
301 * Validate the operator form.
303 function operator_validate($form, &$form_state) { }
306 * Perform any necessary changes to the form values prior to storage.
307 * There is no need for this function to actually store the data.
309 function operator_submit($form, &$form_state) { }
312 * Shortcut to display the value form.
314 function show_value_form(&$form, &$form_state) {
315 $this->value_form($form, $form_state);
316 if (empty($this->no_operator)) {
317 $form['value']['#prefix'] = '<div class="views-group-box views-right-70">' . (isset($form['value']['#prefix']) ? $form['value']['#prefix'] : '');
318 $form['value']['#suffix'] = (isset($form['value']['#suffix']) ? $form['value']['#suffix'] : '') . '</div>';
323 * Options form subform for setting options.
325 * This should be overridden by all child classes and it must
326 * define $form['value']
328 * @see options_form()
330 function value_form(&$form, &$form_state) { $form['value'] = array(); }
333 * Validate the options form.
335 function value_validate($form, &$form_state) { }
338 * Perform any necessary changes to the form values prior to storage.
339 * There is no need for this function to actually store the data.
341 function value_submit($form, &$form_state) { }
344 * Shortcut to display the exposed options form.
346 function show_build_group_form(&$form, &$form_state) {
347 if (empty($this->options['is_grouped'])) {
351 $this->build_group_form($form, $form_state);
353 // When we click the expose button, we add new gadgets to the form but they
354 // have no data in $_POST so their defaults get wiped out. This prevents
355 // these defaults from getting wiped out. This setting will only be TRUE
356 // during a 2nd pass rerender.
357 if (!empty($form_state['force_build_group_options'])) {
358 foreach (element_children($form['group_info']) as $id) {
359 if (isset($form['group_info'][$id]['#default_value']) && !isset($form['group_info'][$id]['#value'])) {
360 $form['group_info'][$id]['#value'] = $form['group_info'][$id]['#default_value'];
367 * Shortcut to display the build_group/hide button.
369 function show_build_group_button(&$form, &$form_state) {
371 $form['group_button'] = array(
372 '#prefix' => '<div class="views-grouped clearfix">',
373 '#suffix' => '</div>',
374 // Should always come after the description and the relationship.
378 $grouped_description = t('Grouped filters allow a choice between predefined operator|value pairs.');
379 $form['group_button']['radios'] = array(
380 '#theme_wrappers' => array('container'),
381 '#attributes' => array('class' => array('js-only')),
383 $form['group_button']['radios']['radios'] = array(
384 '#title' => t('Filter type to expose'),
385 '#description' => $grouped_description,
389 t('Grouped filters'),
393 if (empty($this->options['is_grouped'])) {
394 $form['group_button']['markup'] = array(
395 '#markup' => '<div class="description grouped-description">' . $grouped_description . '</div>',
397 $form['group_button']['button'] = array(
398 '#limit_validation_errors' => array(),
400 '#value' => t('Grouped filters'),
401 '#submit' => array('views_ui_config_item_form_build_group'),
403 $form['group_button']['radios']['radios']['#default_value'] = 0;
406 $form['group_button']['button'] = array(
407 '#limit_validation_errors' => array(),
409 '#value' => t('Single filter'),
410 '#submit' => array('views_ui_config_item_form_build_group'),
412 $form['group_button']['radios']['radios']['#default_value'] = 1;
416 * Shortcut to display the expose/hide button.
418 function show_expose_button(&$form, &$form_state) {
419 $form['expose_button'] = array(
420 '#prefix' => '<div class="views-expose clearfix">',
421 '#suffix' => '</div>',
422 // Should always come after the description and the relationship.
426 // Add a checkbox for JS users, which will have behavior attached to it
427 // so it can replace the button.
428 $form['expose_button']['checkbox'] = array(
429 '#theme_wrappers' => array('container'),
430 '#attributes' => array('class' => array('js-only')),
432 $form['expose_button']['checkbox']['checkbox'] = array(
433 '#title' => t('Expose this filter to visitors, to allow them to change it'),
434 '#type' => 'checkbox',
437 // Then add the button itself.
438 if (empty($this->options['exposed'])) {
439 $form['expose_button']['markup'] = array(
440 '#markup' => '<div class="description exposed-description">' . t('This filter is not exposed. Expose it to allow the users to change it.') . '</div>',
442 $form['expose_button']['button'] = array(
443 '#limit_validation_errors' => array(),
445 '#value' => t('Expose filter'),
446 '#submit' => array('views_ui_config_item_form_expose'),
448 $form['expose_button']['checkbox']['checkbox']['#default_value'] = 0;
451 $form['expose_button']['markup'] = array(
452 '#markup' => '<div class="description exposed-description">' . t('This filter is exposed. If you hide it, users will not be able to change it.') . '</div>',
454 $form['expose_button']['button'] = array(
455 '#limit_validation_errors' => array(),
457 '#value' => t('Hide filter'),
458 '#submit' => array('views_ui_config_item_form_expose'),
460 $form['expose_button']['checkbox']['checkbox']['#default_value'] = 1;
465 * Options form subform for exposed filter options.
467 * @see options_form()
469 function expose_form(&$form, &$form_state) {
470 $form['#theme'] = 'views_ui_expose_filter_form';
471 // #flatten will move everything from $form['expose'][$key] to $form[$key]
472 // prior to rendering. That's why the pre_render for it needs to run first,
473 // so that when the next pre_render (the one for fieldsets) runs, it gets
474 // the flattened data.
475 array_unshift($form['#pre_render'], 'views_ui_pre_render_flatten_data');
476 $form['expose']['#flatten'] = TRUE;
478 if (empty($this->always_required)) {
479 $form['expose']['required'] = array(
480 '#type' => 'checkbox',
481 '#title' => t('Required'),
482 '#default_value' => $this->options['expose']['required'],
486 $form['expose']['required'] = array(
491 $form['expose']['label'] = array(
492 '#type' => 'textfield',
493 '#default_value' => $this->options['expose']['label'],
494 '#title' => t('Label'),
498 $form['expose']['description'] = array(
499 '#type' => 'textfield',
500 '#default_value' => $this->options['expose']['description'],
501 '#title' => t('Description'),
505 if (!empty($form['operator']['#type'])) {
506 // Increase the width of the left (operator) column.
507 $form['operator']['#prefix'] = '<div class="views-group-box views-left-40">';
508 $form['operator']['#suffix'] = '</div>';
509 $form['value']['#prefix'] = '<div class="views-group-box views-right-60">';
510 $form['value']['#suffix'] = '</div>';
512 $form['expose']['use_operator'] = array(
513 '#type' => 'checkbox',
514 '#title' => t('Expose operator'),
515 '#description' => t('Allow the user to choose the operator.'),
516 '#default_value' => !empty($this->options['expose']['use_operator']),
518 $form['expose']['operator_label'] = array(
519 '#type' => 'textfield',
520 '#default_value' => $this->options['expose']['operator_label'],
521 '#title' => t('Operator label'),
523 '#description' => t('This will appear before your operator select field.'),
524 '#dependency' => array(
525 'edit-options-expose-use-operator' => array(1)
528 $form['expose']['operator_id'] = array(
529 '#type' => 'textfield',
530 '#default_value' => $this->options['expose']['operator_id'],
531 '#title' => t('Operator identifier'),
533 '#description' => t('This will appear in the URL after the ? to identify this operator.'),
534 '#dependency' => array(
535 'edit-options-expose-use-operator' => array(1)
537 '#fieldset' => 'more',
541 $form['expose']['operator_id'] = array(
547 if (empty($this->always_multiple)) {
548 $form['expose']['multiple'] = array(
549 '#type' => 'checkbox',
550 '#title' => t('Allow multiple selections'),
551 '#description' => t('Enable to allow users to select multiple items.'),
552 '#default_value' => $this->options['expose']['multiple'],
555 $form['expose']['remember'] = array(
556 '#type' => 'checkbox',
557 '#title' => t('Remember the last selection'),
558 '#description' => t('Enable to remember the last selection made by the user.'),
559 '#default_value' => $this->options['expose']['remember'],
562 $role_options = array_map('check_plain', user_roles());
563 $form['expose']['remember_roles'] = array(
564 '#type' => 'checkboxes',
565 '#title' => t('User roles'),
566 '#description' => t('Remember exposed selection only for the selected user role(s). If you select no roles, the exposed data will never be stored.'),
567 '#default_value' => $this->options['expose']['remember_roles'],
568 '#options' => $role_options,
569 '#dependency' => array(
570 'edit-options-expose-remember' => array(1),
574 $form['expose']['identifier'] = array(
575 '#type' => 'textfield',
576 '#default_value' => $this->options['expose']['identifier'],
577 '#title' => t('Filter identifier'),
579 '#description' => t('This will appear in the URL after the ? to identify this filter. Cannot be blank.'),
580 '#fieldset' => 'more',
585 * Validate the options form.
587 function expose_validate($form, &$form_state) {
588 if (empty($form_state['values']['options']['expose']['identifier'])) {
589 form_error($form['expose']['identifier'], t('The identifier is required if the filter is exposed.'));
592 if (!empty($form_state['values']['options']['expose']['identifier']) && $form_state['values']['options']['expose']['identifier'] == 'value') {
593 form_error($form['expose']['identifier'], t('This identifier is not allowed.'));
596 if (!$this->view->display_handler->is_identifier_unique($form_state['id'], $form_state['values']['options']['expose']['identifier'])) {
597 form_error($form['expose']['identifier'], t('This identifier is used by another handler.'));
602 * Validate the build group options form.
604 function build_group_validate($form, &$form_state) {
605 if (!empty($form_state['values']['options']['group_info'])) {
606 if (empty($form_state['values']['options']['group_info']['identifier'])) {
607 form_error($form['group_info']['identifier'], t('The identifier is required if the filter is exposed.'));
610 if (!empty($form_state['values']['options']['group_info']['identifier']) && $form_state['values']['options']['group_info']['identifier'] == 'value') {
611 form_error($form['group_info']['identifier'], t('This identifier is not allowed.'));
614 if (!$this->view->display_handler->is_identifier_unique($form_state['id'], $form_state['values']['options']['group_info']['identifier'])) {
615 form_error($form['group_info']['identifier'], t('This identifier is used by another handler.'));
619 if (!empty($form_state['values']['options']['group_info']['group_items'])) {
620 foreach ($form_state['values']['options']['group_info']['group_items'] as $id => $group) {
621 if (empty($group['remove'])) {
623 // Check if the title is defined but value wasn't defined.
624 if (!empty($group['title'])) {
625 if ((!is_array($group['value']) && trim($group['value']) == "") ||
626 (is_array($group['value']) && count(array_filter($group['value'], '_views_array_filter_zero')) == 0)) {
627 form_error($form['group_info']['group_items'][$id]['value'],
628 t('The value is required if title for this item is defined.'));
632 // Check if the value is defined but title wasn't defined.
633 if ((!is_array($group['value']) && trim($group['value']) != "") ||
634 (is_array($group['value']) && count(array_filter($group['value'], '_views_array_filter_zero')) > 0)) {
635 if (empty($group['title'])) {
636 form_error($form['group_info']['group_items'][$id]['title'],
637 t('The title is required if value for this item is defined.'));
646 * Save new group items, re-enumerates and remove groups marked to delete.
648 function build_group_submit($form, &$form_state) {
650 uasort($form_state['values']['options']['group_info']['group_items'], 'drupal_sort_weight');
651 // Filter out removed items.
653 // Start from 1 to avoid problems with #default_value in the widget.
655 $new_default = 'All';
656 foreach ($form_state['values']['options']['group_info']['group_items'] as $id => $group) {
657 if (empty($group['remove'])) {
659 unset($group['remove']);
660 unset($group['weight']);
661 $groups[$new_id] = $group;
663 if ($form_state['values']['options']['group_info']['default_group'] === $id) {
664 $new_default = $new_id;
669 if ($new_default != 'All') {
670 $form_state['values']['options']['group_info']['default_group'] = $new_default;
672 $filter_default_multiple = array_filter($form_state['values']['options']['group_info']['default_group_multiple']);
673 $form_state['values']['options']['group_info']['default_group_multiple'] = $filter_default_multiple;
675 $form_state['values']['options']['group_info']['group_items'] = $groups;
679 * Provide default options for exposed filters.
681 function expose_options() {
682 $this->options['expose'] = array(
683 'use_operator' => FALSE,
684 'operator' => $this->options['id'] . '_op',
685 'identifier' => $this->options['id'],
686 'label' => $this->definition['title'],
687 'description' => NULL,
695 * Provide default options for exposed filters.
697 function build_group_options() {
698 $this->options['group_info'] = array(
699 'label' => $this->definition['title'],
700 'description' => NULL,
701 'identifier' => $this->options['id'],
703 'widget' => 'select',
706 'default_group' => 'All',
707 'default_group_multiple' => array(),
708 'group_items' => array(),
713 * Build a form containing a group of operator | values to apply as a
716 function group_form(&$form, &$form_state) {
717 if (!empty($this->options['group_info']['optional']) && !$this->multiple_exposed_input()) {
719 $old_any = $this->options['group_info']['widget'] == 'select' ? '<Any>' : '<Any>';
720 $any_label = variable_get('views_exposed_filter_any_label', 'new_any') == 'old_any' ? $old_any : t('- Any -');
721 $groups = array('All' => $any_label);
723 foreach ($this->options['group_info']['group_items'] as $id => $group) {
724 if (!empty($group['title'])) {
725 $groups[$id] = $id != 'All' ? t($group['title']) : $group['title'];
729 if (count($groups)) {
730 $value = $this->options['group_info']['identifier'];
732 $form[$value] = array(
733 '#type' => $this->options['group_info']['widget'],
734 '#default_value' => $this->group_info,
735 '#options' => $groups,
737 if (!empty($this->options['group_info']['multiple'])) {
738 if (count($groups) < 5) {
739 $form[$value]['#type'] = 'checkboxes';
742 $form[$value]['#type'] = 'select';
743 $form[$value]['#size'] = 5;
744 $form[$value]['#multiple'] = TRUE;
746 unset($form[$value]['#default_value']);
747 if (empty($form_state['input'])) {
748 $form_state['input'][$value] = $this->group_info;
752 $this->options['expose']['label'] = '';
758 * Render our chunk of the exposed filter form when selecting
760 * You can override this if it doesn't do what you expect.
762 function exposed_form(&$form, &$form_state) {
763 if (empty($this->options['exposed'])) {
767 // Build the exposed form, when its based on an operator.
768 if (!empty($this->options['expose']['use_operator']) && !empty($this->options['expose']['operator_id'])) {
769 $operator = $this->options['expose']['operator_id'];
770 $this->operator_form($form, $form_state);
771 $form[$operator] = $form['operator'];
772 $form[$operator]['#title'] = $this->options['expose']['operator_label'];
773 $form[$operator]['#title_display'] = 'invisible';
775 $this->exposed_translate($form[$operator], 'operator');
777 unset($form['operator']);
780 // Build the form and set the value based on the identifier.
781 if (!empty($this->options['expose']['identifier'])) {
782 $value = $this->options['expose']['identifier'];
783 $this->value_form($form, $form_state);
784 $form[$value] = $form['value'];
786 if (isset($form[$value]['#title']) && !empty($form[$value]['#type']) && $form[$value]['#type'] != 'checkbox') {
787 unset($form[$value]['#title']);
790 $this->exposed_translate($form[$value], 'value');
792 if (!empty($form['#type']) && ($form['#type'] == 'checkboxes' || ($form['#type'] == 'select' && !empty($form['#multiple'])))) {
793 unset($form[$value]['#default_value']);
796 if (!empty($form['#type']) && $form['#type'] == 'select' && empty($form['#multiple'])) {
797 $form[$value]['#default_value'] = 'All';
800 if ($value != 'value') {
801 unset($form['value']);
807 * Build the form to let users create the group of exposed filters.
808 * This form is displayed when users click on button 'Build group'
810 function build_group_form(&$form, &$form_state) {
811 if (empty($this->options['exposed']) || empty($this->options['is_grouped'])) {
814 $form['#theme'] = 'views_ui_build_group_filter_form';
816 // #flatten will move everything from $form['group_info'][$key] to $form[$key]
817 // prior to rendering. That's why the pre_render for it needs to run first,
818 // so that when the next pre_render (the one for fieldsets) runs, it gets
819 // the flattened data.
820 array_unshift($form['#pre_render'], 'views_ui_pre_render_flatten_data');
821 $form['group_info']['#flatten'] = TRUE;
823 if (!empty($this->options['group_info']['identifier'])) {
824 $identifier = $this->options['group_info']['identifier'];
827 $identifier = 'group_' . $this->options['expose']['identifier'];
829 $form['group_info']['identifier'] = array(
830 '#type' => 'textfield',
831 '#default_value' => $identifier,
832 '#title' => t('Filter identifier'),
834 '#description' => t('This will appear in the URL after the ? to identify this filter. Cannot be blank.'),
835 '#fieldset' => 'more',
837 $form['group_info']['label'] = array(
838 '#type' => 'textfield',
839 '#default_value' => $this->options['group_info']['label'],
840 '#title' => t('Label'),
844 $form['group_info']['optional'] = array(
845 '#type' => 'checkbox',
846 '#title' => t('Optional'),
847 '#description' => t('This exposed filter is optional and will have added options to allow it not to be set.'),
848 '#default_value' => $this->options['group_info']['optional'],
850 $form['group_info']['multiple'] = array(
851 '#type' => 'checkbox',
852 '#title' => t('Allow multiple selections'),
853 '#description' => t('Enable to allow users to select multiple items.'),
854 '#default_value' => $this->options['group_info']['multiple'],
856 $form['group_info']['widget'] = array(
858 '#default_value' => $this->options['group_info']['widget'],
859 '#title' => t('Widget type'),
861 'radios' => t('Radios'),
862 'select' => t('Select'),
864 '#description' => t('Select which kind of widget will be used to render the group of filters'),
866 $form['group_info']['remember'] = array(
867 '#type' => 'checkbox',
868 '#title' => t('Remember'),
869 '#description' => t('Remember the last setting the user gave this filter.'),
870 '#default_value' => $this->options['group_info']['remember'],
873 if (!empty($this->options['group_info']['identifier'])) {
874 $identifier = $this->options['group_info']['identifier'];
877 $identifier = 'group_' . $this->options['expose']['identifier'];
879 $form['group_info']['identifier'] = array(
880 '#type' => 'textfield',
881 '#default_value' => $identifier,
882 '#title' => t('Filter identifier'),
884 '#description' => t('This will appear in the URL after the ? to identify this filter. Cannot be blank.'),
885 '#fieldset' => 'more',
887 $form['group_info']['label'] = array(
888 '#type' => 'textfield',
889 '#default_value' => $this->options['group_info']['label'],
890 '#title' => t('Label'),
893 $form['group_info']['description'] = array(
894 '#type' => 'textfield',
895 '#default_value' => $this->options['group_info']['description'],
896 '#title' => t('Description'),
899 $form['group_info']['optional'] = array(
900 '#type' => 'checkbox',
901 '#title' => t('Optional'),
902 '#description' => t('This exposed filter is optional and will have added options to allow it not to be set.'),
903 '#default_value' => $this->options['group_info']['optional'],
905 $form['group_info']['widget'] = array(
907 '#default_value' => $this->options['group_info']['widget'],
908 '#title' => t('Widget type'),
910 'radios' => t('Radios'),
911 'select' => t('Select'),
913 '#description' => t('Select which kind of widget will be used to render the group of filters'),
915 $form['group_info']['remember'] = array(
916 '#type' => 'checkbox',
917 '#title' => t('Remember'),
918 '#description' => t('Remember the last setting the user gave this filter.'),
919 '#default_value' => $this->options['group_info']['remember'],
922 $groups = array('All' => '- Any -'); // The string '- Any -' will not be rendered see @theme_views_ui_build_group_filter_form
924 // Provide 3 options to start when we are in a new group.
925 if (count($this->options['group_info']['group_items']) == 0) {
926 $this->options['group_info']['group_items'] = array_fill(1, 3, array());
929 // After the general settings, comes a table with all the existent groups.
931 foreach ($this->options['group_info']['group_items'] as $item_id => $item) {
932 if (!empty($form_state['values']['options']['group_info']['group_items'][$item_id]['remove'])) {
935 // Each rows contains three widgets:
936 // a) The title, where users define how they identify a pair of operator | value
938 // c) The value (or values) to use in the filter with the selected operator
940 // In each row, we have to display the operator form and the value from
941 // $row acts as a fake form to render each widget in a row.
943 $groups[$item_id] = '';
944 $this->operator_form($row, $form_state);
945 // Force the operator form to be a select box. Some handlers uses
946 // radios and they occupy a lot of space in a table row.
947 $row['operator']['#type'] = 'select';
948 $row['operator']['#title'] = '';
949 $this->value_form($row, $form_state);
951 // Fix the dependencies to update value forms when operators
952 // changes. This is needed because forms are inside a new form and
953 // their ids changes. Dependencies are used when operator changes
954 // from to 'Between', 'Not Between', etc, and two or more widgets
956 $without_children = TRUE;
957 foreach (element_children($row['value']) as $children) {
958 if (isset($row['value'][$children]['#dependency']['edit-options-operator'])) {
959 $row['value'][$children]['#dependency']["edit-options-group-info-group-items-$item_id-operator"] = $row['value'][$children]['#dependency']['edit-options-operator'];
960 unset($row['value'][$children]['#dependency']['edit-options-operator']);
961 $row['value'][$children]['#title'] = '';
963 if (!empty($this->options['group_info']['group_items'][$item_id]['value'][$children])) {
964 $row['value'][$children]['#default_value'] = $this->options['group_info']['group_items'][$item_id]['value'][$children];
967 $without_children = FALSE;
970 if ($without_children) {
971 if (!empty($this->options['group_info']['group_items'][$item_id]['value'])) {
972 $row['value']['#default_value'] = $this->options['group_info']['group_items'][$item_id]['value'];
976 if (!empty($this->options['group_info']['group_items'][$item_id]['operator'])) {
977 $row['operator']['#default_value'] = $this->options['group_info']['group_items'][$item_id]['operator'];
981 if (!empty($this->options['group_info']['group_items'][$item_id]['title'])) {
982 $default_title = $this->options['group_info']['group_items'][$item_id]['title'];
985 // Per item group, we have a title that identifies it.
986 $form['group_info']['group_items'][$item_id] = array(
988 '#type' => 'textfield',
990 '#default_value' => $default_title,
992 'operator' => $row['operator'],
993 'value' => $row['value'],
995 '#type' => 'checkbox',
996 '#id' => 'views-removed-' . $item_id,
997 '#attributes' => array('class' => array('views-remove-checkbox')),
998 '#default_value' => 0,
1001 '#type' => 'weight',
1003 '#default_value' => $default_weight++,
1004 '#attributes' => array('class' => array('weight')),
1008 // From all groups, let chose which is the default.
1009 $form['group_info']['default_group'] = array(
1010 '#type' => 'radios',
1011 '#options' => $groups,
1012 '#default_value' => $this->options['group_info']['default_group'],
1013 '#required' => TRUE,
1014 '#attributes' => array(
1015 'class' => array('default-radios'),
1018 // From all groups, let chose which is the default.
1019 $form['group_info']['default_group_multiple'] = array(
1020 '#type' => 'checkboxes',
1021 '#options' => $groups,
1022 '#default_value' => $this->options['group_info']['default_group_multiple'],
1023 '#attributes' => array(
1024 'class' => array('default-checkboxes'),
1028 $form['group_info']['add_group'] = array(
1029 '#prefix' => '<div class="views-build-group clear-block">',
1030 '#suffix' => '</div>',
1031 '#type' => 'submit',
1032 '#value' => t('Add another item'),
1033 '#submit' => array('views_ui_config_item_form_add_group'),
1037 $js['tableDrag']['views-filter-groups']['weight'][0] = array(
1038 'target' => 'weight',
1040 'relationship' => 'sibling',
1041 'action' => 'order',
1045 if (!empty($form_state['js settings']) && is_array($js)) {
1046 $form_state['js settings'] = array_merge($form_state['js settings'], $js);
1049 $form_state['js settings'] = $js;
1055 * Make some translations to a form item to make it more suitable to
1058 function exposed_translate(&$form, $type) {
1059 if (!isset($form['#type'])) {
1063 if ($form['#type'] == 'radios') {
1064 $form['#type'] = 'select';
1066 // Checkboxes don't work so well in exposed forms due to GET conversions.
1067 if ($form['#type'] == 'checkboxes') {
1068 if (empty($form['#no_convert']) || empty($this->options['expose']['multiple'])) {
1069 $form['#type'] = 'select';
1071 if (!empty($this->options['expose']['multiple'])) {
1072 $form['#multiple'] = TRUE;
1075 if (empty($this->options['expose']['multiple']) && isset($form['#multiple'])) {
1076 unset($form['#multiple']);
1077 $form['#size'] = NULL;
1080 // Cleanup in case the translated element's (radios or checkboxes) display value contains html.
1081 if ($form['#type'] == 'select') {
1082 $this->prepare_filter_select_options($form['#options']);
1085 if ($type == 'value' && empty($this->always_required) && empty($this->options['expose']['required']) && $form['#type'] == 'select' && empty($form['#multiple'])) {
1086 $any_label = variable_get('views_exposed_filter_any_label', 'new_any') == 'old_any' ? t('<Any>') : t('- Any -');
1087 $form['#options'] = array('All' => $any_label) + $form['#options'];
1088 $form['#default_value'] = 'All';
1091 if (!empty($this->options['expose']['required'])) {
1092 $form['#required'] = TRUE;
1099 * Sanitizes the HTML select element's options.
1101 * The function is recursive to support optgroups.
1103 function prepare_filter_select_options(&$options) {
1104 foreach ($options as $value => $label) {
1105 // Recurse for optgroups.
1106 if (is_array($label)) {
1107 $this->prepare_filter_select_options($options[$value]);
1109 // FAPI has some special value to allow hierarchy.
1110 // @see _form_options_flatten
1111 elseif (is_object($label)) {
1112 $this->prepare_filter_select_options($options[$value]->option);
1115 $options[$value] = strip_tags(decode_entities($label));
1121 * Tell the renderer about our exposed form. This only needs to be
1122 * overridden for particularly complex forms. And maybe not even then.
1124 * @return array|null
1125 * For standard exposed filters. An array with the following keys:
1126 * - operator: The $form key of the operator. Set to NULL if no operator.
1127 * - value: The $form key of the value. Set to NULL if no value.
1128 * - label: The label to use for this piece.
1129 * For grouped exposed filters. An array with the following keys:
1130 * - value: The $form key of the value. Set to NULL if no value.
1131 * - label: The label to use for this piece.
1133 function exposed_info() {
1134 if (empty($this->options['exposed'])) {
1138 if ($this->is_a_group()) {
1140 'value' => $this->options['group_info']['identifier'],
1141 'label' => $this->options['group_info']['label'],
1142 'description' => $this->options['group_info']['description'],
1147 'operator' => $this->options['expose']['operator_id'],
1148 'value' => $this->options['expose']['identifier'],
1149 'label' => $this->options['expose']['label'],
1150 'description' => $this->options['expose']['description'],
1155 * Transform the input from a grouped filter into a standard filter.
1157 * When a filter is a group, find the set of operator and values
1158 * that the choosed item represents, and inform views that a normal
1159 * filter was submitted by telling the operator and the value selected.
1161 * The param $selected_group_id is only passed when the filter uses the
1162 * checkboxes widget, and this function will be called for each item
1163 * choosed in the checkboxes.
1165 function convert_exposed_input(&$input, $selected_group_id = NULL) {
1166 if ($this->is_a_group()) {
1167 // If it is already defined the selected group, use it. Only valid
1168 // when the filter uses checkboxes for widget.
1169 if (!empty($selected_group_id)) {
1170 $selected_group = $selected_group_id;
1173 $selected_group = $input[$this->options['group_info']['identifier']];
1175 if ($selected_group == 'All' && !empty($this->options['group_info']['optional'])) {
1178 if ($selected_group != 'All' && empty($this->options['group_info']['group_items'][$selected_group])) {
1181 if (isset($selected_group) && isset($this->options['group_info']['group_items'][$selected_group])) {
1182 $input[$this->options['expose']['operator']] = $this->options['group_info']['group_items'][$selected_group]['operator'];
1184 // Value can be optional, For example for 'empty' and 'not empty' filters.
1185 if (!empty($this->options['group_info']['group_items'][$selected_group]['value'])) {
1186 $input[$this->options['expose']['identifier']] = $this->options['group_info']['group_items'][$selected_group]['value'];
1188 $this->options['expose']['use_operator'] = TRUE;
1190 $this->group_info = $input[$this->options['group_info']['identifier']];
1200 * Returns the options available for a grouped filter that users checkboxes
1201 * as widget, and therefore has to be applied several times, one per
1204 function group_multiple_exposed_input(&$input) {
1205 if (!empty($input[$this->options['group_info']['identifier']])) {
1206 return array_filter($input[$this->options['group_info']['identifier']]);
1212 * Returns TRUE if users can select multiple groups items of a
1213 * grouped exposed filter.
1215 function multiple_exposed_input() {
1216 return $this->is_a_group() && !empty($this->options['group_info']['multiple']);
1220 * If set to remember exposed input in the session, store it there.
1221 * This function is similar to store_exposed_input but modified to
1222 * work properly when the filter is a group.
1224 function store_group_input($input, $status) {
1225 if (!$this->is_a_group() || empty($this->options['group_info']['identifier'])) {
1229 if (empty($this->options['group_info']['remember'])) {
1233 // Figure out which display id is responsible for the filters, so we
1234 // know where to look for session stored values.
1235 $display_id = ($this->view->display_handler->is_defaulted('filters')) ? 'default' : $this->view->current_display;
1237 // false means that we got a setting that means to recuse ourselves,
1238 // so we should erase whatever happened to be there.
1239 if ($status === FALSE && isset($_SESSION['views'][$this->view->name][$display_id])) {
1240 $session = &$_SESSION['views'][$this->view->name][$display_id];
1242 if (isset($session[$this->options['group_info']['identifier']])) {
1243 unset($session[$this->options['group_info']['identifier']]);
1247 if ($status !== FALSE) {
1248 if (!isset($_SESSION['views'][$this->view->name][$display_id])) {
1249 $_SESSION['views'][$this->view->name][$display_id] = array();
1252 $session = &$_SESSION['views'][$this->view->name][$display_id];
1254 $session[$this->options['group_info']['identifier']] = $input[$this->options['group_info']['identifier']];
1259 * Check to see if input from the exposed filters should change
1260 * the behavior of this filter.
1262 function accept_exposed_input($input) {
1263 if (empty($this->options['exposed'])) {
1268 if (!empty($this->options['expose']['use_operator']) && !empty($this->options['expose']['operator_id']) && isset($input[$this->options['expose']['operator_id']])) {
1269 $this->operator = $input[$this->options['expose']['operator_id']];
1272 if (!empty($this->options['expose']['identifier'])) {
1273 $value = $input[$this->options['expose']['identifier']];
1275 // Various ways to check for the absence of non-required input.
1276 if (empty($this->options['expose']['required'])) {
1277 if (($this->operator == 'empty' || $this->operator == 'not empty') && $value === '') {
1281 if ($this->operator != 'empty' && $this->operator != 'not empty') {
1282 if ($value == 'All' || $value === array()) {
1287 if (!empty($this->always_multiple) && $value === '') {
1293 if (isset($value)) {
1294 $this->value = $value;
1295 if (empty($this->always_multiple) && empty($this->options['expose']['multiple'])) {
1296 $this->value = array($value);
1307 function store_exposed_input($input, $status) {
1308 if (empty($this->options['exposed']) || empty($this->options['expose']['identifier'])) {
1312 if (empty($this->options['expose']['remember'])) {
1316 // Check if we store exposed value for current user.
1318 $allowed_rids = empty($this->options['expose']['remember_roles']) ? array() : array_filter($this->options['expose']['remember_roles']);
1319 $intersect_rids = array_intersect_key($allowed_rids, $user->roles);
1320 if (empty($intersect_rids)) {
1324 // Figure out which display id is responsible for the filters, so we
1325 // know where to look for session stored values.
1326 $display_id = ($this->view->display_handler->is_defaulted('filters')) ? 'default' : $this->view->current_display;
1329 $operator = !empty($this->options['expose']['use_operator']) && !empty($this->options['expose']['operator_id']);
1331 // false means that we got a setting that means to recuse ourselves,
1332 // so we should erase whatever happened to be there.
1333 if (!$status && isset($_SESSION['views'][$this->view->name][$display_id])) {
1334 $session = &$_SESSION['views'][$this->view->name][$display_id];
1335 if ($operator && isset($session[$this->options['expose']['operator_id']])) {
1336 unset($session[$this->options['expose']['operator_id']]);
1339 if (isset($session[$this->options['expose']['identifier']])) {
1340 unset($session[$this->options['expose']['identifier']]);
1345 if (!isset($_SESSION['views'][$this->view->name][$display_id])) {
1346 $_SESSION['views'][$this->view->name][$display_id] = array();
1349 $session = &$_SESSION['views'][$this->view->name][$display_id];
1351 if ($operator && isset($input[$this->options['expose']['operator_id']])) {
1352 $session[$this->options['expose']['operator_id']] = $input[$this->options['expose']['operator_id']];
1355 $session[$this->options['expose']['identifier']] = $input[$this->options['expose']['identifier']];
1360 * Add this filter to the query.
1362 * Due to the nature of fapi, the value and the operator have an unintended
1363 * level of indirection. You will find them in $this->operator
1364 * and $this->value respectively.
1367 $this->ensure_my_table();
1368 $this->query->add_where($this->options['group'], "$this->table_alias.$this->real_field", $this->value, $this->operator);
1372 * Can this filter be used in OR groups?
1374 * Some filters have complicated where clauses that cannot be easily used
1375 * with OR groups. Some filters must also use HAVING which also makes
1376 * them not groupable. These filters will end up in a special group
1377 * if OR grouping is in use.
1381 function can_group() {
1388 * A special handler to take the place of missing or broken handlers.
1390 * @ingroup views_filter_handlers
1392 class views_handler_filter_broken extends views_handler_filter {
1393 function ui_name($short = FALSE) {
1394 return t('Broken/missing handler');
1397 function ensure_my_table() { /* No table to ensure! */ }
1398 function query($group_by = FALSE) { /* No query to run */ }
1399 function options_form(&$form, &$form_state) {
1400 $form['markup'] = array(
1401 '#markup' => '<div class="form-item description">' . t('The handler for this item is broken or missing and cannot be used. If a module provided the handler and was disabled, re-enabling the module may restore it. Otherwise, you should probably delete this item.') . '</div>',
1406 * Determine if the handler is considered 'broken'
1408 function broken() { return TRUE; }
1412 * Filter by no empty values, though allow to use "0".
1416 function _views_array_filter_zero($var) {
1417 return trim($var) != "";