-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathviews.theme.inc
1237 lines (1092 loc) · 44.3 KB
/
views.theme.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
* Preprocessors and helper functions to make theming easier.
*/
use Drupal\Component\Utility\Xss;
use Drupal\Core\Language\Language;
use Drupal\Core\Template\Attribute;
use Drupal\views\ViewExecutable;
/**
* Prepares variables for view templates.
*
* Default template: views-view.html.twig.
*
* @param array $variables
* An associative array containing:
* - view: The ViewExecutable object.
*/
function template_preprocess_views_view(&$variables) {
global $base_path;
$view = $variables['view'];
$variables['rows'] = (!empty($view->result) || $view->style_plugin->evenEmpty()) ? $view->style_plugin->render($view->result) : array();
// Force a render array so CSS/JS can be added.
if (!is_array($variables['rows'])) {
$variables['rows'] = array('#markup' => $variables['rows']);
}
$variables['css_name'] = drupal_clean_css_identifier($view->storage->id());
$variables['id'] = $view->storage->id();
$variables['display_id'] = $view->current_display;
// Basic classes.
$variables['css_class'] = '';
$variables['attributes']['class'] = array();
$variables['attributes']['class'][] = 'view';
$variables['attributes']['class'][] = 'view-' . drupal_clean_css_identifier($variables['id']);
$variables['attributes']['class'][] = 'view-id-' . $variables['id'];
$variables['attributes']['class'][] = 'view-display-id-' . $variables['display_id'];
$css_class = $view->display_handler->getOption('css_class');
if (!empty($css_class)) {
$variables['css_class'] = preg_replace('/[^a-zA-Z0-9- ]/', '-', $css_class);
$variables['attributes']['class'][] = $variables['css_class'];
}
$empty = empty($view->result);
$variables['header'] = $view->display_handler->renderArea('header', $empty);
$variables['footer'] = $view->display_handler->renderArea('footer', $empty);
$variables['empty'] = $empty ? $view->display_handler->renderArea('empty', $empty) : FALSE;
$variables['exposed'] = !empty($view->exposed_widgets) ? $view->exposed_widgets : '';
$variables['more'] = $view->display_handler->renderMoreLink();
$variables['feed_icon'] = !empty($view->feed_icon) ? $view->feed_icon : '';
$variables['pager'] = '';
// @todo: Figure out whether this belongs into views_ui_preprocess_views_view.
// Render title for the admin preview.
$variables['title'] = !empty($view->views_ui_context) ? filter_xss_admin($view->getTitle()) : '';
if ($view->display_handler->renderPager()) {
$exposed_input = isset($view->exposed_raw_input) ? $view->exposed_raw_input : NULL;
$variables['pager'] = $view->renderPager($exposed_input);
}
if (!empty($view->attachment_before)) {
$variables['attachment_before'] = $view->attachment_before;
}
else {
$variables['attachment_before'] = array();
}
if (!empty($view->attachment_after)) {
$variables['attachment_after'] = $view->attachment_after;
}
else {
$variables['attachment_after'] = array();
}
// Add contextual links to the view. We need to attach them to the dummy
// $view_array variable, since contextual_preprocess() requires that they be
// attached to an array (not an object) in order to process them. For our
// purposes, it doesn't matter what we attach them to, since once they are
// processed by contextual_preprocess() they will appear in the $title_suffix
// variable (which we will then render in views-view.html.twig).
views_add_contextual_links($variables['view_array'], 'view', $view, $view->current_display);
// Attachments are always updated with the outer view, never by themselves,
// so they do not have dom ids.
if (empty($view->is_attachment)) {
// Our JavaScript needs to have some means to find the HTML belonging to
// this view.
//
// It is true that the DIV wrapper has classes denoting the name of the view
// and its display ID, but this is not enough to unequivocally match a view
// with its HTML, because one view may appear several times on the page. So
// we set up a hash with the current time, $dom_id, to issue a "unique"
// identifier for each view. This identifier is written to both
// Drupal.settings and the DIV wrapper.
$variables['dom_id'] = $view->dom_id;
$variables['attributes']['class'][] = 'view-dom-id-' . $variables['dom_id'];
}
// If using AJAX, send identifying data about this view.
if ($view->ajaxEnabled() && empty($view->is_attachment) && empty($view->live_preview)) {
$settings = array(
'views' => array(
'ajax_path' => url('views/ajax'),
'ajaxViews' => array(
'views_dom_id:' . $variables['dom_id'] => array(
'view_name' => $view->storage->id(),
'view_display_id' => $view->current_display,
'view_args' => check_plain(implode('/', $view->args)),
'view_path' => check_plain(current_path()),
'view_base_path' => $view->getPath(),
'view_dom_id' => $variables['dom_id'],
// To fit multiple views on a page, the programmer may have
// overridden the display's pager_element.
'pager_element' => isset($view->pager) ? $view->pager->getPagerId() : 0,
),
),
),
);
$view->element['#attached']['js'][] = array('type' => 'setting', 'data' => $settings);
$view->element['#attached']['library'][] = array('views', 'views.ajax');
}
// If form fields were found in the view, reformat the view output as a form.
if (views_view_has_form_elements($view)) {
// Copy the rows so as not to modify them by reference when rendering.
$rows = $variables['rows'];
$rows = drupal_render($rows);
$output = !empty($rows) ? $rows : $variables['empty'];
$form = drupal_get_form(views_form_id($view), $view, $output);
// The form is requesting that all non-essential views elements be hidden,
// usually because the rendered step is not a view result.
if ($form['show_view_elements']['#value'] == FALSE) {
$variables['header'] = '';
$variables['exposed'] = '';
$variables['pager'] = '';
$variables['footer'] = '';
$variables['more'] = '';
$variables['feed_icon'] = '';
}
$variables['rows'] = $form;
}
}
/**
* Prepares variables for views fields templates.
*
* Default template: views-view-fields.html.twig.
*
* @param array $variables
* An associative array containing:
* - view: The view object.
* - options: An array of options. Each option contains:
* - inline: An array that contains the fields that are to be
* displayed inline.
* - default_field_elements: If default field wrapper
* elements are to be provided.
* - hide_empty: Whether the field is to be hidden if empty.
* - element_default_classes: If the default classes are to be added.
* - separator: A string to be placed between inline fields to keep them
* visually distinct.
* - row: An array containing information about the current row.
*/
function template_preprocess_views_view_fields(&$variables) {
$view = $variables['view'];
// Loop through the fields for this view.
$previous_inline = FALSE;
$variables['fields'] = array(); // ensure it's at least an empty array.
foreach ($view->field as $id => $field) {
// render this even if set to exclude so it can be used elsewhere.
$field_output = $view->style_plugin->getField($view->row_index, $id);
$empty = $field->isValueEmpty($field_output, $field->options['empty_zero']);
if (empty($field->options['exclude']) && (!$empty || (empty($field->options['hide_empty']) && empty($variables['options']['hide_empty'])))) {
$object = new stdClass();
$object->handler = &$view->field[$id];
$object->inline = !empty($variables['options']['inline'][$id]);
$object->element_type = $object->handler->elementType(TRUE, !$variables['options']['default_field_elements'], $object->inline);
if ($object->element_type) {
$attributes = array();
if ($object->handler->options['element_default_classes']) {
$attributes['class'][] = 'field-content';
}
if ($classes = $object->handler->elementClasses($view->row_index)) {
$attributes['class'][] = $classes;
}
$attributes = new Attribute($attributes);
$pre = '<' . $object->element_type;
$pre .= $attributes;
$field_output = $pre . '>' . $field_output . '</' . $object->element_type . '>';
}
// Protect ourself somewhat for backward compatibility. This will prevent
// old templates from producing invalid HTML when no element type is selected.
if (empty($object->element_type)) {
$object->element_type = 'span';
}
$object->content = $field_output;
if (isset($view->field[$id]->field_alias) && isset($variables['row']->{$view->field[$id]->field_alias})) {
$object->raw = $variables['row']->{$view->field[$id]->field_alias};
}
else {
$object->raw = NULL; // make sure it exists to reduce NOTICE
}
if (!empty($variables['options']['separator']) && $previous_inline && $object->inline && $object->content) {
$object->separator = filter_xss_admin($variables['options']['separator']);
}
$object->class = drupal_clean_css_identifier($id);
$previous_inline = $object->inline;
$object->inline_html = $object->handler->elementWrapperType(TRUE, TRUE);
if ($object->inline_html === '' && $variables['options']['default_field_elements']) {
$object->inline_html = $object->inline ? 'span' : 'div';
}
// Set up the wrapper HTML.
$object->wrapper_prefix = '';
$object->wrapper_suffix = '';
if ($object->inline_html) {
$attributes = array();
if ($object->handler->options['element_default_classes']) {
$attributes['class'][] = 'views-field';
$attributes['class'][] = 'views-field-' . $object->class;
}
if ($classes = $object->handler->elementWrapperClasses($view->row_index)) {
$attributes['class'][] = $classes;
}
$attributes = new Attribute($attributes);
$object->wrapper_prefix = '<' . $object->inline_html;
$object->wrapper_prefix .= $attributes;
$object->wrapper_prefix .= '>';
$object->wrapper_suffix = '</' . $object->inline_html . '>';
}
// Set up the label for the value and the HTML to make it easier
// on the template.
$object->label = check_plain($view->field[$id]->label());
$object->label_html = '';
if ($object->label) {
$object->label_html .= $object->label;
if ($object->handler->options['element_label_colon']) {
$object->label_html .= ': ';
}
$object->elementLabelType = $object->handler->elementLabelType(TRUE, !$variables['options']['default_field_elements']);
if ($object->elementLabelType) {
$attributes = array();
if ($object->handler->options['element_default_classes']) {
$attributes['class'][] = 'views-label';
$attributes['class'][] = 'views-label-' . $object->class;
}
$element_label_class = $object->handler->elementLabelClasses($view->row_index);
if ($element_label_class) {
$attributes['class'][] = $element_label_class;
}
$attributes = new Attribute($attributes);
$pre = '<' . $object->elementLabelType;
$pre .= $attributes;
$pre .= '>';
$object->label_html = $pre . $object->label_html . '</' . $object->elementLabelType . '>';
}
}
$variables['fields'][$id] = $object;
}
}
}
/**
* Returns HTML for multiple views fields.
*
* @param $variables
* An associative array containing:
* - fields: An array of field objects. Each field object contains:
* - separator: A string that separates the fields.
* - wrapper_suffix: A string added to the beginning of the fields.
* - label_html: An HTML string that labels the fields.
* - content: The fields.
* - wrapper_suffix: A string added to the end of the fields.
*
* @see template_preprocess_views_view_fields()
*/
function theme_views_view_fields($variables) {
$fields = $variables['fields'];
$output = '';
foreach ($fields as $id => $field) {
if (!empty($field->separator)) {
$output .= $field->separator;
}
$output .= $field->wrapper_prefix;
$output .= $field->label_html;
$output .= $field->content;
$output .= $field->wrapper_suffix;
}
return $output;
}
/**
* Prepares variables for views single grouping templates.
*
* Default template: views-view-grouping.html.twig.
*
* @param array $variables
* An associative array containing:
* - view: The view object.
* - rows: The rows returned from the view.
* - grouping_level: Integer indicating the hierarchical level of the
* grouping.
* - content: The content to be grouped.
* - title: The group heading.
*/
function template_preprocess_views_view_grouping(&$variables) {
$variables['content'] = $variables['view']->style_plugin->renderGroupingSets($variables['rows'], $variables['grouping_level']);
}
/**
* Display a single views field.
*
* Interesting bits of info:
* $field->field_alias says what the raw value in $row will be. Reach it like
* this: @code { $row->{$field->field_alias} @endcode
*/
function theme_views_view_field($variables) {
$view = $variables['view'];
$field = $variables['field'];
$row = $variables['row'];
return $variables['output'];
}
/**
* Prepares variables for views field templates.
*
* Default template: views-view-field.html.twig.
*
* @param array $variables
* An associative array containing:
* - field: The field handler object for the current field.
* - row: Object representing the raw result of the SQL query for the current
* field.
* - view: Instance of the ViewExecutable object for the parent view.
*/
function template_preprocess_views_view_field(&$variables) {
$variables['output'] = $variables['field']->advancedRender($variables['row']);
}
/**
* Prepares variables for views summary templates.
*
* The summary prints a single record from a row, with fields.
*
* Default template: views-view-summary.html.twig.
*
* @param array $variables
* An associative array containing:
* - view: A ViewExecutable object.
* - rows: The raw row data.
*/
function template_preprocess_views_view_summary(&$variables) {
$view = $variables['view'];
$argument = $view->argument[$view->build_info['summary_level']];
$url_options = array();
if (!empty($view->exposed_raw_input)) {
$url_options['query'] = $view->exposed_raw_input;
}
$active_urls = drupal_map_assoc(array(
url(current_path(), array('alias' => TRUE)), // force system path
url(current_path()), // could be an alias
));
// Collect all arguments foreach row, to be able to alter them for example
// by the validator. This is not done per single argument value, because this
// could cause performance problems.
$row_args = array();
foreach ($variables['rows'] as $id => $row) {
$row_args[$id] = $argument->summaryArgument($row);
}
$argument->processSummaryArguments($row_args);
foreach ($variables['rows'] as $id => $row) {
$variables['rows'][$id]->attributes = array();
$variables['rows'][$id]->link = $argument->summaryName($row);
$args = $view->args;
$args[$argument->position] = $row_args[$id];
$base_path = NULL;
if (!empty($argument->options['summary_options']['base_path'])) {
$base_path = $argument->options['summary_options']['base_path'];
}
$variables['rows'][$id]->url = url($view->getUrl($args, $base_path), $url_options);
$variables['rows'][$id]->count = intval($row->{$argument->count_alias});
if (isset($active_urls[$variables['rows'][$id]->url])) {
$variables['rows'][$id]->attributes['class'][] = 'active';
}
$variables['rows'][$id]->attributes = new Attribute($variables['rows'][$id]->attributes);
}
}
/**
* Prepares variables for unformatted summary view templates.
*
* Default template: views-view-summary-unformatted.html.twig.
*
* @param array $variables
* An associative array containing:
* - view: A ViewExecutable object.
* - rows: The raw row data.
* - options: An array of options. Each option contains:
* - separator: A string to be placed between inline fields to keep them
* visually distinct.
*/
function template_preprocess_views_view_summary_unformatted(&$variables) {
$view = $variables['view'];
$argument = $view->argument[$view->build_info['summary_level']];
$url_options = array();
if (!empty($view->exposed_raw_input)) {
$url_options['query'] = $view->exposed_raw_input;
}
$count = 0;
$active_urls = drupal_map_assoc(array(
// Force system path.
url(current_path(), array('alias' => TRUE)),
// Could be an alias.
url(current_path()),
));
// Collect all arguments for each row, to be able to alter them for example
// by the validator. This is not done per single argument value, because
// this could cause performance problems.
$row_args = array();
foreach ($variables['rows'] as $id => $row) {
$row_args[$id] = $argument->summaryArgument($row);
}
$argument->processSummaryArguments($row_args);
foreach ($variables['rows'] as $id => $row) {
// Only false on first time.
if ($count++) {
$variables['rows'][$id]->separator = filter_xss_admin($variables['options']['separator']);
}
$variables['rows'][$id]->attributes = array();
$variables['rows'][$id]->link = $argument->summaryName($row);
$args = $view->args;
$args[$argument->position] = $row_args[$id];
$base_path = NULL;
if (!empty($argument->options['summary_options']['base_path'])) {
$base_path = $argument->options['summary_options']['base_path'];
}
$variables['rows'][$id]->url = url($view->getUrl($args, $base_path), $url_options);
$variables['rows'][$id]->count = intval($row->{$argument->count_alias});
if (isset($active_urls[$variables['rows'][$id]->url])) {
$variables['rows'][$id]->attributes['class'][] = 'active';
}
$variables['rows'][$id]->attributes = new Attribute($variables['rows'][$id]->attributes);
}
}
/**
* Prepares variables for views table templates.
*
* Default template: views-view-table.html.twig.
*
* @param array $variables
* An associative array containing:
* - view: A ViewExecutable object.
* - rows: The raw row data.
*/
function template_preprocess_views_view_table(&$variables) {
$view = $variables['view'];
// We need the raw data for this grouping, which is passed in
// as $variables['rows'].
// However, the template also needs to use for the rendered fields. We
// therefore swap the raw data out to a new variable and reset $variables['rows']
// so that it can get rebuilt.
// Store rows so that they may be used by further preprocess functions.
$result = $variables['result'] = $variables['rows'];
$variables['rows'] = array();
$variables['header'] = array();
$options = $view->style_plugin->options;
$handler = $view->style_plugin;
$default_row_class = isset($options['default_row_class']) ? $options['default_row_class'] : TRUE;
$row_class_special = isset($options['row_class_special']) ? $options['row_class_special'] : TRUE;
$fields = &$view->field;
$columns = $handler->sanitizeColumns($options['columns'], $fields);
$active = !empty($handler->active) ? $handler->active : '';
$order = !empty($handler->order) ? $handler->order : 'asc';
// A boolean variable which stores whether the table has a responsive class.
$responsive = FALSE;
$query = tablesort_get_query_parameters();
if (isset($view->exposed_raw_input)) {
$query += $view->exposed_raw_input;
}
// A boolean to store whether the table's header has any labels.
$has_header_labels = FALSE;
foreach ($columns as $field => $column) {
// Create a second variable so we can easily find what fields we have and
// what the CSS classes should be.
$variables['fields'][$field] = drupal_clean_css_identifier($field);
if ($active == $field) {
$variables['fields'][$field] .= ' active';
}
// Render the header labels.
if ($field == $column && empty($fields[$field]->options['exclude'])) {
$label = check_plain(!empty($fields[$field]) ? $fields[$field]->label() : '');
if (empty($options['info'][$field]['sortable']) || !$fields[$field]->clickSortable()) {
$variables['header'][$field]['content'] = $label;
}
else {
$initial = !empty($options['info'][$field]['default_sort_order']) ? $options['info'][$field]['default_sort_order'] : 'asc';
if ($active == $field) {
$initial = ($order == 'asc') ? 'desc' : 'asc';
}
$title = t('sort by @s', array('@s' => $label));
if ($active == $field) {
$tablesort_indicator = array(
'#theme' => 'tablesort_indicator',
'#style' => $initial,
);
$label .= drupal_render($tablesort_indicator);
}
$query['order'] = $field;
$query['sort'] = $initial;
$link_options = array(
'html' => TRUE,
'attributes' => array('title' => $title),
'query' => $query,
);
$variables['header'][$field]['content'] = l($label, current_path(), $link_options);
}
// Set up the header label class.
$variables['header'][$field]['attributes'] = array();
if ($fields[$field]->options['element_default_classes']) {
$variables['header'][$field]['attributes']['class'][] = 'views-field';
$variables['header'][$field]['attributes']['class'][] = 'views-field-' . $variables['fields'][$field];
}
$class = $fields[$field]->elementLabelClasses(0);
if ($class) {
$variables['header'][$field]['attributes']['class'][] = $class;
}
// Add responsive header classes.
if (!empty($options['info'][$field]['responsive'])) {
$variables['header'][$field]['attributes']['class'][] = $options['info'][$field]['responsive'];
$responsive = TRUE;
}
// Add a CSS align class to each field if one was set.
if (!empty($options['info'][$field]['align'])) {
$variables['header'][$field]['attributes']['class'][] = drupal_clean_css_identifier($options['info'][$field]['align']);
}
// Add a header label wrapper if one was selected.
if ($variables['header'][$field]['content']) {
$element_label_type = $fields[$field]->elementLabelType(TRUE, TRUE);
if ($element_label_type) {
$variables['header'][$field]['content'] = '<' . $element_label_type . '>' . $variables['header'][$field]['content'] . '</' . $element_label_type . '>';
}
// Improves accessibility of complex tables.
$variables['header'][$field]['attributes']['id'] = drupal_html_id('view-' . $field . '-table-column');
}
// Check if header label is not empty.
if (!empty($variables['header'][$field]['content'])) {
$has_header_labels = TRUE;
}
$variables['header'][$field]['attributes'] = new Attribute($variables['header'][$field]['attributes']);
}
// Add a CSS align class to each field if one was set.
if (!empty($options['info'][$field]['align'])) {
$variables['fields'][$field] .= ' ' . drupal_clean_css_identifier($options['info'][$field]['align']);
}
// Render each field into its appropriate column.
foreach ($result as $num => $row) {
// Add field classes.
$variables['rows'][$num]['columns'][$column]['attributes'] = array();
if ($fields[$field]->options['element_default_classes']) {
$variables['rows'][$num]['columns'][$column]['attributes']['class'][] = 'views-field';
$variables['rows'][$num]['columns'][$column]['attributes']['class'][] = 'views-field-' . $variables['fields'][$field];
}
if ($classes = $fields[$field]->elementClasses($num)) {
$variables['rows'][$num]['columns'][$column]['attributes']['class'][] = $classes;
}
// Add responsive header classes.
if (!empty($options['info'][$field]['responsive'])) {
$variables['rows'][$num]['columns'][$column]['attributes']['class'][] = $options['info'][$field]['responsive'];
}
// Improves accessibility of complex tables.
if (isset($variables['header'][$field]['attributes']['id'])) {
$variables['rows'][$num]['columns'][$column]['attributes']['headers'] = array($variables['header'][$field]['attributes']['id']);
}
if (!empty($fields[$field]) && empty($fields[$field]->options['exclude'])) {
$field_output = $handler->getField($num, $field);
$element_type = $fields[$field]->elementType(TRUE, TRUE);
if ($element_type) {
$field_output = '<' . $element_type . '>' . $field_output . '</' . $element_type . '>';
}
// Only bother with separators and stuff if the field shows up.
if (!empty($field_output) && empty($variables['rows'][$num]['columns'][$column]['content'])) {
// Place the field into the column, along with an optional separator.
if (!empty($variables['rows'][$num][$column]['content'])) {
if (!empty($options['info'][$column]['separator'])) {
$variables['rows'][$num]['columns'][$column]['content'] .= filter_xss_admin($options['info'][$column]['separator']);
}
}
else {
$variables['rows'][$num]['columns'][$column]['content'] = '';
}
$variables['rows'][$num]['columns'][$column]['content'] .= $field_output;
}
}
$variables['rows'][$num]['columns'][$column]['attributes'] = new Attribute($variables['rows'][$num]['columns'][$column]['attributes']);
}
// Remove columns if the option is hide empty column is checked and the
// field is not empty.
if (!empty($options['info'][$field]['empty_column'])) {
$empty = TRUE;
foreach ($variables['rows'] as $num => $columns) {
$empty &= empty($columns[$column]);
}
if ($empty) {
foreach ($variables['rows'] as $num => &$column_items) {
unset($column_items[$column]);
unset($variables['header'][$column]);
}
}
}
}
// Hide table header if all labels are empty.
if (!$has_header_labels) {
$variables['header'] = array();
}
$count = 0;
foreach ($variables['rows'] as $num => $row) {
$variables['rows'][$num]['attributes'] = array();
if ($row_class_special) {
$variables['rows'][$num]['attributes']['class'][] = ($count++ % 2 == 0) ? 'odd' : 'even';
if ($num === 0) {
$variables['rows'][$num]['attributes']['class'][] = 'views-row-first';
}
elseif ($num === (count($variables['rows']) - 1)) {
$variables['rows'][$num]['attributes']['class'][] = 'views-row-last';
}
}
if ($row_class = $handler->getRowClass($num)) {
$variables['rows'][$num]['attributes']['class'][] = $row_class;
}
$variables['rows'][$num]['attributes'] = new Attribute($variables['rows'][$num]['attributes']);
}
$variables['attributes']['class'][] = 'views-table';
$variables['attributes']['class'][] = 'views-view-table';
if (empty($variables['rows']) && !empty($options['empty_table'])) {
$build = $view->display_handler->renderArea('empty');
$variables['rows'][0]['columns'][0]['content'] = drupal_render($build);
$variables['rows'][0]['attributes'] = new Attribute(array('class' => 'odd'));
// Calculate the amounts of rows with output.
$variables['rows'][0]['columns'][0]['attributes'] = new Attribute(array(
'colspan' => count($variables['header']),
'class' => 'views-empty',
));
}
if (!empty($options['sticky'])) {
$variables['view']->element['#attached']['library'][] = array('system', 'drupal.tableheader');
$variables['attributes']['class'][] = "sticky-enabled";
}
$variables['attributes']['class'][] = 'cols-' . count($variables['header']);
// Add the caption to the list if set.
if (!empty($handler->options['caption'])) {
$variables['caption'] = Xss::filterAdmin($handler->options['caption']);
$variables['caption_needed'] = TRUE;
}
else {
$variables['caption'] = '';
$variables['caption_needed'] = FALSE;
}
$variables['summary'] = $handler->options['summary'];
$variables['description'] = $handler->options['description'];
$variables['caption_needed'] |= !empty($variables['summary']) || !empty($variables['description']);
// If the table has headers and it should react responsively to columns hidden
// with the classes represented by the constants RESPONSIVE_PRIORITY_MEDIUM
// and RESPONSIVE_PRIORITY_LOW, add the tableresponsive behaviors.
if (isset($variables['header']) && $responsive) {
$variables['view']->element['#attached']['library'][] = array('system', 'drupal.tableresponsive');
// Add 'responsive-enabled' class to the table to identify it for JS.
// This is needed to target tables constructed by this function.
$variables['attributes']['class'][] = 'responsive-enabled';
}
}
/**
* Prepares variables for views grid style templates.
*
* Default template: views-view-grid.html.twig.
*
* @param array $variables
* An associative array containing:
* - view: The view object.
* - rows: An array of row items. Each row is an array of content.
*/
function template_preprocess_views_view_grid(&$variables) {
$options = $variables['options'] = $variables['view']->style_plugin->options;
$horizontal = ($options['alignment'] === 'horizontal');
$variables['attributes']['class'] = array(
'views-view-grid',
$options['alignment'],
'cols-' . $options['columns'],
'clearfix',
);
$col = 0;
$row = 0;
$items = array();
$remainders = count($variables['rows']) % $options['columns'];
$num_rows = floor(count($variables['rows']) / $options['columns']);
// Iterate over each rendered views result row.
foreach ($variables['rows'] as $result_index => $item) {
// Add the item.
if ($horizontal) {
$items[$row]['content'][$col]['content'] = $item;
}
else {
$items[$col]['content'][$row]['content'] = $item;
}
// Create attributes for rows.
if (!$horizontal || ($horizontal && empty($items[$row]['attributes']))) {
$row_attributes = array('class' => array());
// Add default views row classes.
if ($options['row_class_default']) {
$row_attributes['class'][] = 'views-row';
$row_attributes['class'][] = 'row-' . ($row + 1);
if ($horizontal) {
$row_attributes['class'][] = 'clearfix';
}
}
// Add custom row classes.
$row_class = array_filter(explode(' ', $variables['view']->style_plugin->getCustomClass($result_index, 'row')));
if (!empty($row_class)) {
$row_attributes['class'] = array_merge($row_attributes['class'], $row_class);
}
// Add row attributes to the item.
if ($horizontal) {
$items[$row]['attributes'] = new Attribute($row_attributes);
}
else {
$items[$col]['content'][$row]['attributes'] = new Attribute($row_attributes);
}
}
// Create attributes for columns.
if ($horizontal || (!$horizontal && empty($items[$col]['attributes']))) {
$col_attributes = array('class' => array());
// Add default views column classes.
if ($options['col_class_default']) {
$col_attributes['class'][] = 'views-col';
$col_attributes['class'][] = 'col-' . ($col + 1);
if (!$horizontal) {
$col_attributes['class'][] = 'clearfix';
}
}
// Add custom column classes.
$col_class = array_filter(explode(' ', $variables['view']->style_plugin->getCustomClass($result_index, 'col')));
if (!empty($col_class)) {
$col_attributes['class'] = array_merge($col_attributes['class'], $col_class);
}
// Add automatic width for columns.
if ($options['automatic_width']) {
$col_attributes['style'] = 'width: ' . (100 / $options['columns']) . '%;';
}
// Add column attributes to the item.
if ($horizontal) {
$items[$row]['content'][$col]['attributes'] = new Attribute($col_attributes);
}
else {
$items[$col]['attributes'] = new Attribute($col_attributes);
}
}
// Increase, decrease or reset appropriate integers.
if ($horizontal) {
if ($col == 0 && $col != ($options['columns'] - 1)) {
$col++;
}
elseif ($col >= ($options['columns'] - 1)) {
$col = 0;
$row++;
}
else {
$col++;
}
}
else {
$row++;
if (!$remainders && $row == $num_rows) {
$row = 0;
$col++;
}
elseif ($remainders && $row == $num_rows + 1) {
$row = 0;
$col++;
$remainders--;
}
}
}
// Add items to the variables array.
$variables['items'] = $items;
}
/**
* Prepares variables for views unformatted rows templates.
*
* Default template: views-view-unformatted.html.twig.
*
* @param array $variables
* An associative array containing:
* - view: The view object.
* - rows: An array of row items. Each row is an array of content.
*/
function template_preprocess_views_view_unformatted(&$variables) {
$view = $variables['view'];
$rows = $variables['rows'];
$style = $view->style_plugin;
$options = $style->options;
$default_row_class = isset($options['default_row_class']) ? $options['default_row_class'] : FALSE;
$row_class_special = isset($options['row_class_special']) ? $options['row_class_special'] : FALSE;
// Set up striping values.
$count = 0;
$max = count($rows);
foreach ($rows as $id => $row) {
$variables['rows'][$id] = array();
$variables['rows'][$id]['content'] = $row;
$variables['rows'][$id]['attributes'] = array();
$count++;
if ($default_row_class) {
$variables['rows'][$id]['attributes']['class'][] = 'views-row';
$variables['rows'][$id]['attributes']['class'][] = 'views-row-' . $count;
}
if ($row_class_special) {
$variables['rows'][$id]['attributes']['class'][] = 'views-row-' . ($count % 2 ? 'odd' : 'even');
if ($count == 1) {
$variables['rows'][$id]['attributes']['class'][] = 'views-row-first';
}
if ($count == $max) {
$variables['rows'][$id]['attributes']['class'][] = 'views-row-last';
}
}
if ($row_class = $view->style_plugin->getRowClass($id)) {
$variables['rows'][$id]['attributes']['class'][] = $row_class;
}
$variables['rows'][$id]['attributes'] = new Attribute($variables['rows'][$id]['attributes']);
}
}
/**
* Prepares variables for Views HTML list templates.
*
* Default template: views-view-list.html.twig.
*
* @param array $variables
* An associative array containing:
* - view: A View object.
*/
function template_preprocess_views_view_list(&$variables) {
$handler = $variables['view']->style_plugin;
// Fetch classes from handler options.
$class = explode(' ', $handler->options['class']);
$class = array_map('drupal_clean_css_identifier', $class);
// Fetch wrapper classes from handler options.
$wrapper_class = explode(' ', $handler->options['wrapper_class']);
$wrapper_class = array_map('drupal_clean_css_identifier', $wrapper_class);
// Initialize a new attribute class for $wrapper_class.
if ($wrapper_class) {
$variables['attributes']['class'] = $wrapper_class;
}
// Initialize a new attribute class for $class.
$variables['list']['attributes'] = new Attribute(array('class' => $class));
$variables['list']['type'] = $handler->options['type'];
template_preprocess_views_view_unformatted($variables);
}
/**
* Prepares variables for RSS feed templates.
*
* Default template: views-view-rss.html.twig.
*
* @param array $variables
* An associative array containing:
* - view: A ViewExecutable object.
* - rows: The raw row data.
*/
function template_preprocess_views_view_rss(&$variables) {
global $base_url;
$view = $variables['view'];
$items = $variables['rows'];
$style = $view->style_plugin;
$config = config('system.site');
// The RSS 2.0 "spec" doesn't indicate HTML can be used in the description.
// We strip all HTML tags, but need to prevent double encoding from properly
// escaped source data (such as & becoming &amp;).
$variables['description'] = check_plain(decode_entities(strip_tags($style->getDescription())));
if ($view->display_handler->getOption('sitename_title')) {
$title = $config->get('name');
if ($slogan = $config->get('slogan')) {
$title .= ' - ' . $slogan;
}
}
else {
$title = $view->getTitle();
}
$variables['title'] = check_plain($title);
// Figure out which display which has a path we're using for this feed. If
// there isn't one, use the global $base_url
$link_display_id = $view->display_handler->getLinkDisplay();
if ($link_display_id && $display = $view->displayHandlers->get($link_display_id)) {
$path = $view->displayHandlers->get($link_display_id)->getPath();
}
if ($path) {
$path = $view->getUrl(NULL, $path);
$url_options = array('absolute' => TRUE);
if (!empty($view->exposed_raw_input)) {
$url_options['query'] = $view->exposed_raw_input;
}
// Compare the link to the default home page; if it's the default home page,
// just use $base_url.
if ($path == $config->get('page.front')) {
$path = '';