-
Notifications
You must be signed in to change notification settings - Fork 0
/
views_handler_filter.inc
1410 lines (1256 loc) · 49.6 KB
/
views_handler_filter.inc
1
2
3
4
5
6
7
8
9
10
11
12
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
99
100
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
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
181
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* @file
* @todo.
*/
/**
* @defgroup views_filter_handlers Views filter handlers
* @{
* Handlers to tell Views how to filter queries.
*
* Definition items:
* - allow empty: If true, the 'IS NULL' and 'IS NOT NULL' operators become
* available as standard operators.
*
* Object flags:
* You can set some specific behavior by setting up the following flags on
* your custom class.
*
* - always_multiple:
* Disable the possibility to force a single value.
* - no_operator:
* Disable the possibility to use operators.
* - always_required:
* Disable the possibility to allow a exposed input to be optional.
*/
/**
* Base class for filters.
*
* @ingroup views_filter_handlers
*/
class views_handler_filter extends views_handler {
/**
* Contains the actual value of the field,either configured in the views ui
* or entered in the exposed filters.
*/
var $value = NULL;
/**
* Contains the operator which is used on the query.
*/
var $operator = '=';
/**
* Contains the information of the selected item in a gruped filter.
*/
var $group_info = NULL;
/**
* @var bool
* Disable the possibility to force a single value.
*/
var $always_multiple = FALSE;
/**
* @var bool
* Disable the possibility to use operators.
*/
var $no_operator = FALSE;
/**
* @var bool
* Disable the possibility to allow a exposed input to be optional.
*/
var $always_required = FALSE;
/**
* Provide some extra help to get the operator/value easier to use.
*
* This likely has to be overridden by filters which are more complex
* than simple operator/value.
*/
function init(&$view, &$options) {
parent::init($view, $options);
$this->operator = $this->options['operator'];
$this->value = $this->options['value'];
$this->group_info = $this->options['group_info']['default_group'];
// Compatibility: The new UI changed several settingsqu.
if (!empty($options['exposed']) && !empty($options['expose']['optional']) && !isset($options['expose']['required'])) {
$this->options['expose']['required'] = !$options['expose']['optional'];
}
if (!empty($options['exposed']) && !empty($options['expose']['single']) && !isset($options['expose']['multiple'])) {
$this->options['expose']['multiple'] = !$options['expose']['single'];
}
if (!empty($options['exposed']) && !empty($options['expose']['operator']) && !isset($options['expose']['operator_id'])) {
$this->options['expose']['operator_id'] = $options['expose']['operator_id'] = $options['expose']['operator'];
}
if ($this->multiple_exposed_input()) {
$this->group_info = array_filter($options['group_info']['default_group_multiple']);
$this->options['expose']['multiple'] = TRUE;
}
// If there are relationships in the view, allow empty should be true
// so that we can do IS NULL checks on items. Not all filters respect
// allow empty, but string and numeric do and that covers enough.
if ($this->view->display_handler->get_option('relationships')) {
$this->definition['allow empty'] = TRUE;
}
}
function option_definition() {
$options = parent::option_definition();
$options['operator'] = array('default' => '=');
$options['value'] = array('default' => '');
$options['group'] = array('default' => '1');
$options['exposed'] = array('default' => FALSE, 'bool' => TRUE);
$options['expose'] = array(
'contains' => array(
'operator_id' => array('default' => FALSE),
'label' => array('default' => '', 'translatable' => TRUE),
'description' => array('default' => '', 'translatable' => TRUE),
'use_operator' => array('default' => FALSE, 'bool' => TRUE),
'operator' => array('default' => ''),
'identifier' => array('default' => ''),
'required' => array('default' => FALSE, 'bool' => TRUE),
'remember' => array('default' => FALSE, 'bool' => TRUE),
'multiple' => array('default' => FALSE, 'bool' => TRUE),
'remember_roles' => array('default' => array(
DRUPAL_AUTHENTICATED_RID => DRUPAL_AUTHENTICATED_RID,
)),
),
);
// A group is a combination of a filter, an operator and a value
// operating like a single filter.
// Users can choose from a select box which group they want to apply.
// Views will filter the view according to the defined values.
// Because it acts as a standard filter, we have to define
// an identifier and other settings like the widget and the label.
// This settings are saved in another array to allow users to switch
// between a normal filter and a group of filters with a single click.
$options['is_grouped'] = array('default' => FALSE, 'bool' => TRUE);
$options['group_info'] = array(
'contains' => array(
'label' => array('default' => '', 'translatable' => TRUE),
'description' => array('default' => '', 'translatable' => TRUE),
'identifier' => array('default' => ''),
'optional' => array('default' => TRUE, 'bool' => TRUE),
'widget' => array('default' => 'select'),
'multiple' => array('default' => FALSE, 'bool' => TRUE),
'remember' => array('default' => 0),
'default_group' => array('default' => 'All'),
'default_group_multiple' => array('default' => array()),
'group_items' => array('default' => array()),
),
);
return $options;
}
/**
* Display the filter on the administrative summary
*/
function admin_summary() {
return check_plain((string) $this->operator) . ' ' . check_plain((string) $this->value);
}
/**
* Determine if a filter can be exposed.
*/
function can_expose() { return TRUE; }
/**
* Determine if a filter can be converted into a group.
* Only exposed filters with operators available can be converted into groups.
*/
function can_build_group() {
return $this->is_exposed() && (count($this->operator_options()) > 0);
}
/**
* Returns TRUE if the exposed filter works like a grouped filter.
*/
function is_a_group() {
return $this->is_exposed() && !empty($this->options['is_grouped']);
}
/**
* Provide the basic form which calls through to subforms.
* If overridden, it is best to call through to the parent,
* or to at least make sure all of the functions in this form
* are called.
*/
function options_form(&$form, &$form_state) {
parent::options_form($form, $form_state);
if ($this->can_expose()) {
$this->show_expose_button($form, $form_state);
}
if ($this->can_build_group()) {
$this->show_build_group_button($form, $form_state);
}
$form['clear_markup_start'] = array(
'#markup' => '<div class="clearfix">',
);
if ($this->is_a_group()) {
if ($this->can_build_group()) {
$form['clear_markup_start'] = array(
'#markup' => '<div class="clearfix">',
);
// Render the build group form.
$this->show_build_group_form($form, $form_state);
$form['clear_markup_end'] = array(
'#markup' => '</div>',
);
}
}
else {
// Add the subform from operator_form().
$this->show_operator_form($form, $form_state);
// Add the subform from value_form().
$this->show_value_form($form, $form_state);
$form['clear_markup_end'] = array(
'#markup' => '</div>',
);
if ($this->can_expose()) {
// Add the subform from expose_form().
$this->show_expose_form($form, $form_state);
}
}
}
/**
* Simple validate handler
*/
function options_validate(&$form, &$form_state) {
$this->operator_validate($form, $form_state);
$this->value_validate($form, $form_state);
if (!empty($this->options['exposed']) && !$this->is_a_group()) {
$this->expose_validate($form, $form_state);
}
if ($this->is_a_group()) {
$this->build_group_validate($form, $form_state);
}
}
/**
* Simple submit handler
*/
function options_submit(&$form, &$form_state) {
unset($form_state['values']['expose_button']); // don't store this.
unset($form_state['values']['group_button']); // don't store this.
if (!$this->is_a_group()) {
$this->operator_submit($form, $form_state);
$this->value_submit($form, $form_state);
}
if (!empty($this->options['exposed'])) {
$this->expose_submit($form, $form_state);
}
if ($this->is_a_group()) {
$this->build_group_submit($form, $form_state);
}
}
/**
* Shortcut to display the operator form.
*/
function show_operator_form(&$form, &$form_state) {
$this->operator_form($form, $form_state);
$form['operator']['#prefix'] = '<div class="views-group-box views-left-30">';
$form['operator']['#suffix'] = '</div>';
}
/**
* Options form subform for setting the operator.
*
* This may be overridden by child classes, and it must
* define $form['operator'];
*
* @see options_form()
*/
function operator_form(&$form, &$form_state) {
$options = $this->operator_options();
if (!empty($options)) {
$form['operator'] = array(
'#type' => count($options) < 10 ? 'radios' : 'select',
'#title' => t('Operator'),
'#default_value' => $this->operator,
'#options' => $options,
);
}
}
/**
* Provide a list of options for the default operator form.
* Should be overridden by classes that don't override operator_form
*/
function operator_options() { return array(); }
/**
* Validate the operator form.
*/
function operator_validate($form, &$form_state) { }
/**
* Perform any necessary changes to the form values prior to storage.
* There is no need for this function to actually store the data.
*/
function operator_submit($form, &$form_state) { }
/**
* Shortcut to display the value form.
*/
function show_value_form(&$form, &$form_state) {
$this->value_form($form, $form_state);
if (empty($this->no_operator)) {
$form['value']['#prefix'] = '<div class="views-group-box views-right-70">' . (isset($form['value']['#prefix']) ? $form['value']['#prefix'] : '');
$form['value']['#suffix'] = (isset($form['value']['#suffix']) ? $form['value']['#suffix'] : '') . '</div>';
}
}
/**
* Options form subform for setting options.
*
* This should be overridden by all child classes and it must
* define $form['value']
*
* @see options_form()
*/
function value_form(&$form, &$form_state) { $form['value'] = array(); }
/**
* Validate the options form.
*/
function value_validate($form, &$form_state) { }
/**
* Perform any necessary changes to the form values prior to storage.
* There is no need for this function to actually store the data.
*/
function value_submit($form, &$form_state) { }
/**
* Shortcut to display the exposed options form.
*/
function show_build_group_form(&$form, &$form_state) {
if (empty($this->options['is_grouped'])) {
return;
}
$this->build_group_form($form, $form_state);
// When we click the expose button, we add new gadgets to the form but they
// have no data in $_POST so their defaults get wiped out. This prevents
// these defaults from getting wiped out. This setting will only be TRUE
// during a 2nd pass rerender.
if (!empty($form_state['force_build_group_options'])) {
foreach (element_children($form['group_info']) as $id) {
if (isset($form['group_info'][$id]['#default_value']) && !isset($form['group_info'][$id]['#value'])) {
$form['group_info'][$id]['#value'] = $form['group_info'][$id]['#default_value'];
}
}
}
}
/**
* Shortcut to display the build_group/hide button.
*/
function show_build_group_button(&$form, &$form_state) {
$form['group_button'] = array(
'#prefix' => '<div class="views-grouped clearfix">',
'#suffix' => '</div>',
// Should always come after the description and the relationship.
'#weight' => -190,
);
$grouped_description = t('Grouped filters allow a choice between predefined operator|value pairs.');
$form['group_button']['radios'] = array(
'#theme_wrappers' => array('container'),
'#attributes' => array('class' => array('js-only')),
);
$form['group_button']['radios']['radios'] = array(
'#title' => t('Filter type to expose'),
'#description' => $grouped_description,
'#type' => 'radios',
'#options' => array(
t('Single filter'),
t('Grouped filters'),
),
);
if (empty($this->options['is_grouped'])) {
$form['group_button']['markup'] = array(
'#markup' => '<div class="description grouped-description">' . $grouped_description . '</div>',
);
$form['group_button']['button'] = array(
'#limit_validation_errors' => array(),
'#type' => 'submit',
'#value' => t('Grouped filters'),
'#submit' => array('views_ui_config_item_form_build_group'),
);
$form['group_button']['radios']['radios']['#default_value'] = 0;
}
else {
$form['group_button']['button'] = array(
'#limit_validation_errors' => array(),
'#type' => 'submit',
'#value' => t('Single filter'),
'#submit' => array('views_ui_config_item_form_build_group'),
);
$form['group_button']['radios']['radios']['#default_value'] = 1;
}
}
/**
* Shortcut to display the expose/hide button.
*/
function show_expose_button(&$form, &$form_state) {
$form['expose_button'] = array(
'#prefix' => '<div class="views-expose clearfix">',
'#suffix' => '</div>',
// Should always come after the description and the relationship.
'#weight' => -200,
);
// Add a checkbox for JS users, which will have behavior attached to it
// so it can replace the button.
$form['expose_button']['checkbox'] = array(
'#theme_wrappers' => array('container'),
'#attributes' => array('class' => array('js-only')),
);
$form['expose_button']['checkbox']['checkbox'] = array(
'#title' => t('Expose this filter to visitors, to allow them to change it'),
'#type' => 'checkbox',
);
// Then add the button itself.
if (empty($this->options['exposed'])) {
$form['expose_button']['markup'] = array(
'#markup' => '<div class="description exposed-description">' . t('This filter is not exposed. Expose it to allow the users to change it.') . '</div>',
);
$form['expose_button']['button'] = array(
'#limit_validation_errors' => array(),
'#type' => 'submit',
'#value' => t('Expose filter'),
'#submit' => array('views_ui_config_item_form_expose'),
);
$form['expose_button']['checkbox']['checkbox']['#default_value'] = 0;
}
else {
$form['expose_button']['markup'] = array(
'#markup' => '<div class="description exposed-description">' . t('This filter is exposed. If you hide it, users will not be able to change it.') . '</div>',
);
$form['expose_button']['button'] = array(
'#limit_validation_errors' => array(),
'#type' => 'submit',
'#value' => t('Hide filter'),
'#submit' => array('views_ui_config_item_form_expose'),
);
$form['expose_button']['checkbox']['checkbox']['#default_value'] = 1;
}
}
/**
* Options form subform for exposed filter options.
*
* @see options_form()
*/
function expose_form(&$form, &$form_state) {
$form['#theme'] = 'views_ui_expose_filter_form';
// #flatten will move everything from $form['expose'][$key] to $form[$key]
// prior to rendering. That's why the pre_render for it needs to run first,
// so that when the next pre_render (the one for fieldsets) runs, it gets
// the flattened data.
array_unshift($form['#pre_render'], 'views_ui_pre_render_flatten_data');
$form['expose']['#flatten'] = TRUE;
if (empty($this->always_required)) {
$form['expose']['required'] = array(
'#type' => 'checkbox',
'#title' => t('Required'),
'#default_value' => $this->options['expose']['required'],
);
}
else {
$form['expose']['required'] = array(
'#type' => 'value',
'#value' => TRUE,
);
}
$form['expose']['label'] = array(
'#type' => 'textfield',
'#default_value' => $this->options['expose']['label'],
'#title' => t('Label'),
'#size' => 40,
);
$form['expose']['description'] = array(
'#type' => 'textfield',
'#default_value' => $this->options['expose']['description'],
'#title' => t('Description'),
'#size' => 60,
);
if (!empty($form['operator']['#type'])) {
// Increase the width of the left (operator) column.
$form['operator']['#prefix'] = '<div class="views-group-box views-left-40">';
$form['operator']['#suffix'] = '</div>';
$form['value']['#prefix'] = '<div class="views-group-box views-right-60">';
$form['value']['#suffix'] = '</div>';
$form['expose']['use_operator'] = array(
'#type' => 'checkbox',
'#title' => t('Expose operator'),
'#description' => t('Allow the user to choose the operator.'),
'#default_value' => !empty($this->options['expose']['use_operator']),
);
$form['expose']['operator_id'] = array(
'#type' => 'textfield',
'#default_value' => $this->options['expose']['operator_id'],
'#title' => t('Operator identifier'),
'#size' => 40,
'#description' => t('This will appear in the URL after the ? to identify this operator.'),
'#dependency' => array(
'edit-options-expose-use-operator' => array(1)
),
'#fieldset' => 'more',
);
}
else {
$form['expose']['operator_id'] = array(
'#type' => 'value',
'#value' => '',
);
}
if (empty($this->always_multiple)) {
$form['expose']['multiple'] = array(
'#type' => 'checkbox',
'#title' => t('Allow multiple selections'),
'#description' => t('Enable to allow users to select multiple items.'),
'#default_value' => $this->options['expose']['multiple'],
);
}
$form['expose']['remember'] = array(
'#type' => 'checkbox',
'#title' => t('Remember the last selection'),
'#description' => t('Enable to remember the last selection made by the user.'),
'#default_value' => $this->options['expose']['remember'],
);
$role_options = array_map('check_plain', user_roles());
$form['expose']['remember_roles'] = array(
'#type' => 'checkboxes',
'#title' => t('User roles'),
'#description' => t('Remember exposed selection only for the selected user role(s). If you select no roles, the exposed data will never be stored.'),
'#default_value' => $this->options['expose']['remember_roles'],
'#options' => $role_options,
'#dependency' => array(
'edit-options-expose-remember' => array(1),
),
);
$form['expose']['identifier'] = array(
'#type' => 'textfield',
'#default_value' => $this->options['expose']['identifier'],
'#title' => t('Filter identifier'),
'#size' => 40,
'#description' => t('This will appear in the URL after the ? to identify this filter. Cannot be blank.'),
'#fieldset' => 'more',
);
}
/**
* Validate the options form.
*/
function expose_validate($form, &$form_state) {
if (empty($form_state['values']['options']['expose']['identifier'])) {
form_error($form['expose']['identifier'], t('The identifier is required if the filter is exposed.'));
}
if (!empty($form_state['values']['options']['expose']['identifier']) && $form_state['values']['options']['expose']['identifier'] == 'value') {
form_error($form['expose']['identifier'], t('This identifier is not allowed.'));
}
if (!$this->view->display_handler->is_identifier_unique($form_state['id'], $form_state['values']['options']['expose']['identifier'])) {
form_error($form['expose']['identifier'], t('This identifier is used by another handler.'));
}
}
/**
* Validate the build group options form.
*/
function build_group_validate($form, &$form_state) {
if (!empty($form_state['values']['options']['group_info'])) {
if (empty($form_state['values']['options']['group_info']['identifier'])) {
form_error($form['group_info']['identifier'], t('The identifier is required if the filter is exposed.'));
}
if (!empty($form_state['values']['options']['group_info']['identifier']) && $form_state['values']['options']['group_info']['identifier'] == 'value') {
form_error($form['group_info']['identifier'], t('This identifier is not allowed.'));
}
if (!$this->view->display_handler->is_identifier_unique($form_state['id'], $form_state['values']['options']['group_info']['identifier'])) {
form_error($form['group_info']['identifier'], t('This identifier is used by another handler.'));
}
}
if (!empty($form_state['values']['options']['group_info']['group_items'])) {
foreach ($form_state['values']['options']['group_info']['group_items'] as $id => $group) {
if (empty($group['remove'])) {
// Check if the title is defined but value wasn't defined.
if (!empty($group['title'])) {
if ((!is_array($group['value']) && trim($group['value']) == "") ||
(is_array($group['value']) && count(array_filter($group['value'], '_views_array_filter_zero')) == 0)) {
form_error($form['group_info']['group_items'][$id]['value'],
t('The value is required if title for this item is defined.'));
}
}
// Check if the value is defined but title wasn't defined.
if ((!is_array($group['value']) && trim($group['value']) != "") ||
(is_array($group['value']) && count(array_filter($group['value'], '_views_array_filter_zero')) > 0)) {
if (empty($group['title'])) {
form_error($form['group_info']['group_items'][$id]['title'],
t('The title is required if value for this item is defined.'));
}
}
}
}
}
}
/**
* Save new group items, re-enumerates and remove groups marked to delete.
*/
function build_group_submit($form, &$form_state) {
$groups = array();
uasort($form_state['values']['options']['group_info']['group_items'], 'drupal_sort_weight');
// Filter out removed items.
// Start from 1 to avoid problems with #default_value in the widget.
$new_id = 1;
$new_default = 'All';
foreach ($form_state['values']['options']['group_info']['group_items'] as $id => $group) {
if (empty($group['remove'])) {
// Don't store this.
unset($group['remove']);
unset($group['weight']);
$groups[$new_id] = $group;
if ($form_state['values']['options']['group_info']['default_group'] === $id) {
$new_default = $new_id;
}
}
$new_id++;
}
if ($new_default != 'All') {
$form_state['values']['options']['group_info']['default_group'] = $new_default;
}
$filter_default_multiple = array_filter($form_state['values']['options']['group_info']['default_group_multiple']);
$form_state['values']['options']['group_info']['default_group_multiple'] = $filter_default_multiple;
$form_state['values']['options']['group_info']['group_items'] = $groups;
}
/**
* Provide default options for exposed filters.
*/
function expose_options() {
$this->options['expose'] = array(
'use_operator' => FALSE,
'operator' => $this->options['id'] . '_op',
'identifier' => $this->options['id'],
'label' => $this->definition['title'],
'description' => NULL,
'remember' => FALSE,
'multiple' => FALSE,
'required' => FALSE,
);
}
/**
* Provide default options for exposed filters.
*/
function build_group_options() {
$this->options['group_info'] = array(
'label' => $this->definition['title'],
'description' => NULL,
'identifier' => $this->options['id'],
'optional' => TRUE,
'widget' => 'select',
'multiple' => FALSE,
'remember' => FALSE,
'default_group' => 'All',
'default_group_multiple' => array(),
'group_items' => array(),
);
}
/**
* Build a form containing a group of operator | values to apply as a
* single filter.
*/
function group_form(&$form, &$form_state) {
if (!empty($this->options['group_info']['optional']) && !$this->multiple_exposed_input()) {
$old_any = $this->options['group_info']['widget'] == 'select' ? '<Any>' : '<Any>';
$any_label = variable_get('views_exposed_filter_any_label', 'new_any') == 'old_any' ? $old_any : t('- Any -');
$groups = array('All' => $any_label);
}
foreach ($this->options['group_info']['group_items'] as $id => $group) {
if (!empty($group['title'])) {
$groups[$id] = $id != 'All' ? t($group['title']) : $group['title'];
}
}
if (count($groups)) {
$value = $this->options['group_info']['identifier'];
$form[$value] = array(
'#type' => $this->options['group_info']['widget'],
'#default_value' => $this->group_info,
'#options' => $groups,
);
if (!empty($this->options['group_info']['multiple'])) {
if (count($groups) < 5) {
$form[$value]['#type'] = 'checkboxes';
}
else {
$form[$value]['#type'] = 'select';
$form[$value]['#size'] = 5;
$form[$value]['#multiple'] = TRUE;
}
unset($form[$value]['#default_value']);
if (empty($form_state['input'])) {
$form_state['input'][$value] = $this->group_info;
}
}
$this->options['expose']['label'] = '';
}
}
/**
* Render our chunk of the exposed filter form when selecting
*
* You can override this if it doesn't do what you expect.
*/
function exposed_form(&$form, &$form_state) {
if (empty($this->options['exposed'])) {
return;
}
// Build the exposed form, when its based on an operator.
if (!empty($this->options['expose']['use_operator']) && !empty($this->options['expose']['operator_id'])) {
$operator = $this->options['expose']['operator_id'];
$this->operator_form($form, $form_state);
$form[$operator] = $form['operator'];
if (isset($form[$operator]['#title'])) {
unset($form[$operator]['#title']);
}
$this->exposed_translate($form[$operator], 'operator');
unset($form['operator']);
}
// Build the form and set the value based on the identifier.
if (!empty($this->options['expose']['identifier'])) {
$value = $this->options['expose']['identifier'];
$this->value_form($form, $form_state);
$form[$value] = $form['value'];
if (isset($form[$value]['#title']) && !empty($form[$value]['#type']) && $form[$value]['#type'] != 'checkbox') {
unset($form[$value]['#title']);
}
$this->exposed_translate($form[$value], 'value');
if (!empty($form['#type']) && ($form['#type'] == 'checkboxes' || ($form['#type'] == 'select' && !empty($form['#multiple'])))) {
unset($form[$value]['#default_value']);
}
if (!empty($form['#type']) && $form['#type'] == 'select' && empty($form['#multiple'])) {
$form[$value]['#default_value'] = 'All';
}
if ($value != 'value') {
unset($form['value']);
}
}
}
/**
* Build the form to let users create the group of exposed filters.
* This form is displayed when users click on button 'Build group'
*/
function build_group_form(&$form, &$form_state) {
if (empty($this->options['exposed']) || empty($this->options['is_grouped'])) {
return;
}
$form['#theme'] = 'views_ui_build_group_filter_form';
// #flatten will move everything from $form['group_info'][$key] to $form[$key]
// prior to rendering. That's why the pre_render for it needs to run first,
// so that when the next pre_render (the one for fieldsets) runs, it gets
// the flattened data.
array_unshift($form['#pre_render'], 'views_ui_pre_render_flatten_data');
$form['group_info']['#flatten'] = TRUE;
if (!empty($this->options['group_info']['identifier'])) {
$identifier = $this->options['group_info']['identifier'];
}
else {
$identifier = 'group_' . $this->options['expose']['identifier'];
}
$form['group_info']['identifier'] = array(
'#type' => 'textfield',
'#default_value' => $identifier,
'#title' => t('Filter identifier'),
'#size' => 40,
'#description' => t('This will appear in the URL after the ? to identify this filter. Cannot be blank.'),
'#fieldset' => 'more',
);
$form['group_info']['label'] = array(
'#type' => 'textfield',
'#default_value' => $this->options['group_info']['label'],
'#title' => t('Label'),
'#size' => 40,
);
$form['group_info']['optional'] = array(
'#type' => 'checkbox',
'#title' => t('Optional'),
'#description' => t('This exposed filter is optional and will have added options to allow it not to be set.'),
'#default_value' => $this->options['group_info']['optional'],
);
$form['group_info']['multiple'] = array(
'#type' => 'checkbox',
'#title' => t('Allow multiple selections'),
'#description' => t('Enable to allow users to select multiple items.'),
'#default_value' => $this->options['group_info']['multiple'],
);
$form['group_info']['widget'] = array(
'#type' => 'radios',
'#default_value' => $this->options['group_info']['widget'],
'#title' => t('Widget type'),
'#options' => array(
'radios' => t('Radios'),
'select' => t('Select'),
),
'#description' => t('Select which kind of widget will be used to render the group of filters'),
);
$form['group_info']['remember'] = array(
'#type' => 'checkbox',
'#title' => t('Remember'),
'#description' => t('Remember the last setting the user gave this filter.'),
'#default_value' => $this->options['group_info']['remember'],
);
if (!empty($this->options['group_info']['identifier'])) {
$identifier = $this->options['group_info']['identifier'];
}
else {
$identifier = 'group_' . $this->options['expose']['identifier'];
}
$form['group_info']['identifier'] = array(
'#type' => 'textfield',
'#default_value' => $identifier,
'#title' => t('Filter identifier'),
'#size' => 40,
'#description' => t('This will appear in the URL after the ? to identify this filter. Cannot be blank.'),
'#fieldset' => 'more',
);
$form['group_info']['label'] = array(
'#type' => 'textfield',
'#default_value' => $this->options['group_info']['label'],
'#title' => t('Label'),
'#size' => 40,
);
$form['group_info']['description'] = array(
'#type' => 'textfield',
'#default_value' => $this->options['group_info']['description'],
'#title' => t('Description'),
'#size' => 60,
);
$form['group_info']['optional'] = array(
'#type' => 'checkbox',
'#title' => t('Optional'),
'#description' => t('This exposed filter is optional and will have added options to allow it not to be set.'),
'#default_value' => $this->options['group_info']['optional'],
);
$form['group_info']['widget'] = array(
'#type' => 'radios',
'#default_value' => $this->options['group_info']['widget'],
'#title' => t('Widget type'),
'#options' => array(
'radios' => t('Radios'),
'select' => t('Select'),
),
'#description' => t('Select which kind of widget will be used to render the group of filters'),
);
$form['group_info']['remember'] = array(
'#type' => 'checkbox',
'#title' => t('Remember'),
'#description' => t('Remember the last setting the user gave this filter.'),
'#default_value' => $this->options['group_info']['remember'],
);
$groups = array('All' => '- Any -'); // The string '- Any -' will not be rendered see @theme_views_ui_build_group_filter_form
// Provide 3 options to start when we are in a new group.
if (count($this->options['group_info']['group_items']) == 0) {
$this->options['group_info']['group_items'] = array_fill(1, 3, array());
}
// After the general settings, comes a table with all the existent groups.
$default_weight = 0;
foreach ($this->options['group_info']['group_items'] as $item_id => $item) {
if (!empty($form_state['values']['options']['group_info']['group_items'][$item_id]['remove'])) {
continue;
}
// Each rows contains three widgets:
// a) The title, where users define how they identify a pair of operator | value
// b) The operator
// c) The value (or values) to use in the filter with the selected operator
// In each row, we have to display the operator form and the value from
// $row acts as a fake form to render each widget in a row.
$row = array();
$groups[$item_id] = '';
$this->operator_form($row, $form_state);
// Force the operator form to be a select box. Some handlers uses
// radios and they occupy a lot of space in a table row.
$row['operator']['#type'] = 'select';
$row['operator']['#title'] = '';
$this->value_form($row, $form_state);
// Fix the dependencies to update value forms when operators
// changes. This is needed because forms are inside a new form and
// their ids changes. Dependencies are used when operator changes
// from to 'Between', 'Not Between', etc, and two or more widgets
// are displayed.
$without_children = TRUE;
foreach (element_children($row['value']) as $children) {
if (isset($row['value'][$children]['#dependency']['edit-options-operator'])) {
$row['value'][$children]['#dependency']["edit-options-group-info-group-items-$item_id-operator"] = $row['value'][$children]['#dependency']['edit-options-operator'];
unset($row['value'][$children]['#dependency']['edit-options-operator']);
$row['value'][$children]['#title'] = '';
if (!empty($this->options['group_info']['group_items'][$item_id]['value'][$children])) {
$row['value'][$children]['#default_value'] = $this->options['group_info']['group_items'][$item_id]['value'][$children];
}
}
$without_children = FALSE;
}
if ($without_children) {
if (!empty($this->options['group_info']['group_items'][$item_id]['value'])) {
$row['value']['#default_value'] = $this->options['group_info']['group_items'][$item_id]['value'];
}
}
if (!empty($this->options['group_info']['group_items'][$item_id]['operator'])) {
$row['operator']['#default_value'] = $this->options['group_info']['group_items'][$item_id]['operator'];
}
$default_title = '';
if (!empty($this->options['group_info']['group_items'][$item_id]['title'])) {
$default_title = $this->options['group_info']['group_items'][$item_id]['title'];
}
// Per item group, we have a title that identifies it.
$form['group_info']['group_items'][$item_id] = array(
'title' => array(
'#type' => 'textfield',
'#size' => 20,
'#default_value' => $default_title,
),
'operator' => $row['operator'],
'value' => $row['value'],
'remove' => array(
'#type' => 'checkbox',
'#id' => 'views-removed-' . $item_id,
'#attributes' => array('class' => array('views-remove-checkbox')),
'#default_value' => 0,
),
'weight' => array(
'#type' => 'weight',
'#delta' => 10,
'#default_value' => $default_weight++,
'#attributes' => array('class' => array('weight')),
),
);
}
// From all groups, let chose which is the default.
$form['group_info']['default_group'] = array(
'#type' => 'radios',
'#options' => $groups,
'#default_value' => $this->options['group_info']['default_group'],
'#required' => TRUE,