views.theme.inc 41.9 KB
Newer Older
merlinofchaos's avatar
merlinofchaos committed
1
2
3
4
5
6
7
<?php

/**
 * @file
 * Preprocessors and helper functions to make theming easier.
 */

8
use Drupal\Component\Utility\String;
9
use Drupal\Component\Utility\Xss;
10
use Drupal\Core\Language\Language;
11
use Drupal\Core\Template\Attribute;
12
use Drupal\views\Form\ViewsForm;
13
use Drupal\views\ViewExecutable;
14

merlinofchaos's avatar
merlinofchaos committed
15
/**
16
17
18
19
 * Prepares variables for view templates.
 *
 * Default template: views-view.html.twig.
 *
20
 * @param array $variables
21
22
 *   An associative array containing:
 *   - view: The ViewExecutable object.
merlinofchaos's avatar
merlinofchaos committed
23
 */
24
25
function template_preprocess_views_view(&$variables) {
  $view = $variables['view'];
merlinofchaos's avatar
merlinofchaos committed
26

27
  $variables['rows'] = (!empty($view->result) || $view->style_plugin->evenEmpty()) ? $view->style_plugin->render($view->result) : array();
28
  // Force a render array so CSS/JS can be added.
29
30
  if (!is_array($variables['rows'])) {
    $variables['rows'] = array('#markup' => $variables['rows']);
31
  }
merlinofchaos's avatar
merlinofchaos committed
32

33
34
35
  $variables['css_name'] = drupal_clean_css_identifier($view->storage->id());
  $variables['id'] = $view->storage->id();
  $variables['display_id'] = $view->current_display;
merlinofchaos's avatar
merlinofchaos committed
36

37
  // Basic classes.
38
  $variables['css_class'] = '';
39

40
41
42
43
44
  $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'];
merlinofchaos's avatar
merlinofchaos committed
45

46
  $css_class = $view->display_handler->getOption('css_class');
merlinofchaos's avatar
merlinofchaos committed
47
  if (!empty($css_class)) {
48
49
    $variables['css_class'] = preg_replace('/[^a-zA-Z0-9- ]/', '-', $css_class);
    $variables['attributes']['class'][] = $variables['css_class'];
merlinofchaos's avatar
merlinofchaos committed
50
51
  }

52
  $empty = empty($view->result);
53
54
55
  $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;
merlinofchaos's avatar
merlinofchaos committed
56

57
58
59
  $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 : '';
merlinofchaos's avatar
merlinofchaos committed
60

61
  $variables['pager']      = '';
merlinofchaos's avatar
merlinofchaos committed
62
63
64

  // @todo: Figure out whether this belongs into views_ui_preprocess_views_view.
  // Render title for the admin preview.
65
  $variables['title'] = !empty($view->views_ui_context) ? Xss::filterAdmin($view->getTitle()) : '';
merlinofchaos's avatar
merlinofchaos committed
66

67
  if ($view->display_handler->renderPager()) {
merlinofchaos's avatar
merlinofchaos committed
68
    $exposed_input = isset($view->exposed_raw_input) ? $view->exposed_raw_input : NULL;
69
    $variables['pager'] = $view->renderPager($exposed_input);
merlinofchaos's avatar
merlinofchaos committed
70
71
  }

72
  if (!empty($view->attachment_before)) {
73
    $variables['attachment_before'] = $view->attachment_before;
74
75
  }
  else {
76
    $variables['attachment_before'] = array();
77
78
  }
  if (!empty($view->attachment_after)) {
79
    $variables['attachment_after'] = $view->attachment_after;
80
81
  }
  else {
82
    $variables['attachment_after'] = array();
83
  }
merlinofchaos's avatar
merlinofchaos committed
84
85
86
87
88
89

  // 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
90
  // variable (which we will then render in views-view.html.twig).
91
  views_add_contextual_links($variables['view_array'], 'view', $view, $view->current_display);
merlinofchaos's avatar
merlinofchaos committed
92
93
94
95

  // Attachments are always updated with the outer view, never by themselves,
  // so they do not have dom ids.
  if (empty($view->is_attachment)) {
96
97
    // Our JavaScript needs to have some means to find the HTML belonging to
    // this view.
merlinofchaos's avatar
merlinofchaos committed
98
99
100
101
    //
    // 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
102
103
    // 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
104
    // drupalSettings and the DIV wrapper.
105
106
    $variables['dom_id'] = $view->dom_id;
    $variables['attributes']['class'][] = 'view-dom-id-' . $variables['dom_id'];
merlinofchaos's avatar
merlinofchaos committed
107
108
  }

109
  // If form fields were found in the view, reformat the view output as a form.
110
  if ($view->hasFormElements()) {
111
    // Copy the rows so as not to modify them by reference when rendering.
112
    $rows = $variables['rows'];
113
114
115
116
117
118
119
120
121
    // Only render row output if there are rows. Otherwise, render the empty
    // region.
    if (!empty($rows)) {
      $output = drupal_render($rows);
    }
    else {
      $empty = $variables['empty'];
      $output = drupal_render($empty);
    }
122
123
124
125

    $container = \Drupal::getContainer();
    $form_object = new ViewsForm($container->get('controller_resolver'), $container->get('url_generator'), $container->get('request'), $view->storage->id(), $view->current_display);
    $form = \Drupal::formBuilder()->getForm($form_object, $view, $output);
merlinofchaos's avatar
merlinofchaos committed
126
127
128
    // 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) {
129
130
131
132
133
134
      $variables['header'] = '';
      $variables['exposed'] = '';
      $variables['pager'] = '';
      $variables['footer'] = '';
      $variables['more'] = '';
      $variables['feed_icon'] = '';
merlinofchaos's avatar
merlinofchaos committed
135
    }
136
    $variables['rows'] = $form;
merlinofchaos's avatar
merlinofchaos committed
137
138
139
140
  }
}

/**
141
142
143
144
 * Prepares variables for views fields templates.
 *
 * Default template: views-view-fields.html.twig.
 *
145
 * @param array $variables
146
147
148
149
150
151
152
153
154
155
156
157
 *   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.
merlinofchaos's avatar
merlinofchaos committed
158
 */
159
160
function template_preprocess_views_view_fields(&$variables) {
  $view = $variables['view'];
merlinofchaos's avatar
merlinofchaos committed
161
162
163

  // Loop through the fields for this view.
  $previous_inline = FALSE;
164
  $variables['fields'] = array(); // ensure it's at least an empty array.
merlinofchaos's avatar
merlinofchaos committed
165
166
  foreach ($view->field as $id => $field) {
    // render this even if set to exclude so it can be used elsewhere.
167
    $field_output = $view->style_plugin->getField($view->row_index, $id);
168
    $empty = $field->isValueEmpty($field_output, $field->options['empty_zero']);
169
    if (empty($field->options['exclude']) && (!$empty || (empty($field->options['hide_empty']) && empty($variables['options']['hide_empty'])))) {
merlinofchaos's avatar
merlinofchaos committed
170
      $object = new stdClass();
171
      $object->handler = $view->field[$id];
172
      $object->inline = !empty($variables['options']['inline'][$id]);
merlinofchaos's avatar
merlinofchaos committed
173

174
      $object->element_type = $object->handler->elementType(TRUE, !$variables['options']['default_field_elements'], $object->inline);
merlinofchaos's avatar
merlinofchaos committed
175
      if ($object->element_type) {
176
        $attributes = array();
merlinofchaos's avatar
merlinofchaos committed
177
        if ($object->handler->options['element_default_classes']) {
178
          $attributes['class'][] = 'field-content';
merlinofchaos's avatar
merlinofchaos committed
179
180
        }

181
        if ($classes = $object->handler->elementClasses($view->row_index)) {
182
          $attributes['class'][] = $classes;
merlinofchaos's avatar
merlinofchaos committed
183
        }
184
        $attributes = new Attribute($attributes);
merlinofchaos's avatar
merlinofchaos committed
185
186

        $pre = '<' . $object->element_type;
187
        $pre .= $attributes;
merlinofchaos's avatar
merlinofchaos committed
188
189
190
191
192
193
194
195
196
197
        $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;
198
199
      if (isset($view->field[$id]->field_alias) && isset($variables['row']->{$view->field[$id]->field_alias})) {
        $object->raw = $variables['row']->{$view->field[$id]->field_alias};
merlinofchaos's avatar
merlinofchaos committed
200
201
202
203
204
      }
      else {
        $object->raw = NULL; // make sure it exists to reduce NOTICE
      }

205
      if (!empty($variables['options']['separator']) && $previous_inline && $object->inline && $object->content) {
206
        $object->separator = Xss::filterAdmin($variables['options']['separator']);
merlinofchaos's avatar
merlinofchaos committed
207
208
209
210
211
      }

      $object->class = drupal_clean_css_identifier($id);

      $previous_inline = $object->inline;
212
      $object->inline_html = $object->handler->elementWrapperType(TRUE, TRUE);
213
      if ($object->inline_html === '' && $variables['options']['default_field_elements']) {
merlinofchaos's avatar
merlinofchaos committed
214
215
216
217
218
219
220
221
        $object->inline_html = $object->inline ? 'span' : 'div';
      }

      // Set up the wrapper HTML.
      $object->wrapper_prefix = '';
      $object->wrapper_suffix = '';

      if ($object->inline_html) {
222
        $attributes = array();
merlinofchaos's avatar
merlinofchaos committed
223
        if ($object->handler->options['element_default_classes']) {
224
225
          $attributes['class'][] = 'views-field';
          $attributes['class'][] = 'views-field-' . $object->class;
merlinofchaos's avatar
merlinofchaos committed
226
227
        }

228
        if ($classes = $object->handler->elementWrapperClasses($view->row_index)) {
229
          $attributes['class'][] = $classes;
merlinofchaos's avatar
merlinofchaos committed
230
        }
231
        $attributes = new Attribute($attributes);
merlinofchaos's avatar
merlinofchaos committed
232
233

        $object->wrapper_prefix = '<' . $object->inline_html;
234
        $object->wrapper_prefix .= $attributes;
merlinofchaos's avatar
merlinofchaos committed
235
236
237
238
239
240
        $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.
241
      $object->label = String::checkPlain($view->field[$id]->label());
merlinofchaos's avatar
merlinofchaos committed
242
243
244
245
246
247
248
      $object->label_html = '';
      if ($object->label) {
        $object->label_html .= $object->label;
        if ($object->handler->options['element_label_colon']) {
          $object->label_html .= ': ';
        }

249
        $object->elementLabelType = $object->handler->elementLabelType(TRUE, !$variables['options']['default_field_elements']);
250
        if ($object->elementLabelType) {
251
          $attributes = array();
merlinofchaos's avatar
merlinofchaos committed
252
          if ($object->handler->options['element_default_classes']) {
253
254
            $attributes['class'][] = 'views-label';
            $attributes['class'][] = 'views-label-' . $object->class;
merlinofchaos's avatar
merlinofchaos committed
255
256
          }

257
          $element_label_class = $object->handler->elementLabelClasses($view->row_index);
merlinofchaos's avatar
merlinofchaos committed
258
          if ($element_label_class) {
259
            $attributes['class'][] = $element_label_class;
merlinofchaos's avatar
merlinofchaos committed
260
          }
261
          $attributes = new Attribute($attributes);
merlinofchaos's avatar
merlinofchaos committed
262

263
          $pre = '<' . $object->elementLabelType;
264
          $pre .= $attributes;
merlinofchaos's avatar
merlinofchaos committed
265
266
          $pre .= '>';

267
          $object->label_html = $pre . $object->label_html . '</' . $object->elementLabelType . '>';
merlinofchaos's avatar
merlinofchaos committed
268
269
270
        }
      }

271
      $variables['fields'][$id] = $object;
merlinofchaos's avatar
merlinofchaos committed
272
273
274
275
276
    }
  }

}

277
/**
278
279
280
281
282
283
284
285
286
287
288
289
290
 * 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()
 */
291
292
293
294
function theme_views_view_fields($variables) {
  $fields = $variables['fields'];
  $output = '';

295
  foreach ($fields as $field) {
296
297
298
299
300
301
302
303
304
305
306
307
308
309
    if (!empty($field->separator)) {
      $output .= $field->separator;
    }

    $output .= $field->wrapper_prefix;
    $output .= $field->label_html;
    $output .= $field->content;

    $output .= $field->wrapper_suffix;
  }

  return $output;
}

merlinofchaos's avatar
merlinofchaos committed
310
/**
311
312
313
314
 * Prepares variables for views single grouping templates.
 *
 * Default template: views-view-grouping.html.twig.
 *
315
 * @param array $variables
316
317
318
319
320
321
322
 *   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.
323
 */
324
325
function template_preprocess_views_view_grouping(&$variables) {
  $variables['content'] = $variables['view']->style_plugin->renderGroupingSets($variables['rows'], $variables['grouping_level']);
merlinofchaos's avatar
merlinofchaos committed
326
327
328
329
330
331
332
333
334
}

/**
 * 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
 */
335
336
function theme_views_view_field($variables) {
  return $variables['output'];
merlinofchaos's avatar
merlinofchaos committed
337
338
339
}

/**
340
 * Prepares variables for views field templates.
merlinofchaos's avatar
merlinofchaos committed
341
 *
342
343
 * Default template: views-view-field.html.twig.
 *
344
 * @param array $variables
345
346
347
348
349
 *   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.
merlinofchaos's avatar
merlinofchaos committed
350
 */
351
352
function template_preprocess_views_view_field(&$variables) {
  $variables['output'] = $variables['field']->advancedRender($variables['row']);
merlinofchaos's avatar
merlinofchaos committed
353
354
355
}

/**
356
357
358
359
360
361
 * Prepares variables for views summary templates.
 *
 * The summary prints a single record from a row, with fields.
 *
 * Default template: views-view-summary.html.twig.
 *
362
 * @param array $variables
363
364
365
 *   An associative array containing:
 *   - view: A ViewExecutable object.
 *   - rows: The raw row data.
merlinofchaos's avatar
merlinofchaos committed
366
 */
367
368
function template_preprocess_views_view_summary(&$variables) {
  $view = $variables['view'];
merlinofchaos's avatar
merlinofchaos committed
369
370
371
372
373
374
375
376
  $argument = $view->argument[$view->build_info['summary_level']];

  $url_options = array();

  if (!empty($view->exposed_raw_input)) {
    $url_options['query'] = $view->exposed_raw_input;
  }

377
  $active_urls = array(
378
379
    url(current_path(), array('alias' => TRUE)), // force system path
    url(current_path()), // could be an alias
380
381
  );
  $active_urls = array_combine($active_urls, $active_urls);
merlinofchaos's avatar
merlinofchaos committed
382

383
384
385
  // 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.
merlinofchaos's avatar
merlinofchaos committed
386
387
  $row_args = array();

388
  foreach ($variables['rows'] as $id => $row) {
389
    $row_args[$id] = $argument->summaryArgument($row);
merlinofchaos's avatar
merlinofchaos committed
390
  }
391
  $argument->processSummaryArguments($row_args);
merlinofchaos's avatar
merlinofchaos committed
392

393
  foreach ($variables['rows'] as $id => $row) {
394
    $variables['rows'][$id]->attributes = array();
395
    $variables['rows'][$id]->link = $argument->summaryName($row);
merlinofchaos's avatar
merlinofchaos committed
396
397
398
399
400
401
402
    $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'];
    }
403
404
405
    $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])) {
406
      $variables['rows'][$id]->attributes['class'][] = 'active';
merlinofchaos's avatar
merlinofchaos committed
407
    }
408
    $variables['rows'][$id]->attributes = new Attribute($variables['rows'][$id]->attributes);
merlinofchaos's avatar
merlinofchaos committed
409
410
411
412
  }
}

/**
413
414
415
416
 * Prepares variables for unformatted summary view templates.
 *
 * Default template: views-view-summary-unformatted.html.twig.
 *
417
 * @param array $variables
418
419
420
421
422
423
 *   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.
merlinofchaos's avatar
merlinofchaos committed
424
 */
425
426
function template_preprocess_views_view_summary_unformatted(&$variables) {
  $view = $variables['view'];
merlinofchaos's avatar
merlinofchaos committed
427
428
429
430
431
432
433
434
435
  $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;
436
  $active_urls = array(
437
438
439
440
    // Force system path.
    url(current_path(), array('alias' => TRUE)),
    // Could be an alias.
    url(current_path()),
441
442
  );
  $active_urls = array_combine($active_urls, $active_urls);
merlinofchaos's avatar
merlinofchaos committed
443

444
445
446
  // 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.
merlinofchaos's avatar
merlinofchaos committed
447
  $row_args = array();
448
  foreach ($variables['rows'] as $id => $row) {
449
    $row_args[$id] = $argument->summaryArgument($row);
merlinofchaos's avatar
merlinofchaos committed
450
  }
451
  $argument->processSummaryArguments($row_args);
merlinofchaos's avatar
merlinofchaos committed
452

453
  foreach ($variables['rows'] as $id => $row) {
454
    // Only false on first time.
merlinofchaos's avatar
merlinofchaos committed
455
    if ($count++) {
456
      $variables['rows'][$id]->separator = Xss::filterAdmin($variables['options']['separator']);
merlinofchaos's avatar
merlinofchaos committed
457
    }
458
    $variables['rows'][$id]->attributes = array();
459
    $variables['rows'][$id]->link = $argument->summaryName($row);
merlinofchaos's avatar
merlinofchaos committed
460
461
462
463
464
465
466
    $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'];
    }
467
468
469
    $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])) {
470
      $variables['rows'][$id]->attributes['class'][] = 'active';
merlinofchaos's avatar
merlinofchaos committed
471
    }
472
    $variables['rows'][$id]->attributes = new Attribute($variables['rows'][$id]->attributes);
merlinofchaos's avatar
merlinofchaos committed
473
474
475
476
  }
}

/**
477
478
479
480
 * Prepares variables for views table templates.
 *
 * Default template: views-view-table.html.twig.
 *
481
 * @param array $variables
482
483
484
 *   An associative array containing:
 *   - view: A ViewExecutable object.
 *   - rows: The raw row data.
merlinofchaos's avatar
merlinofchaos committed
485
 */
486
487
function template_preprocess_views_view_table(&$variables) {
  $view = $variables['view'];
merlinofchaos's avatar
merlinofchaos committed
488

489
  // We need the raw data for this grouping, which is passed in
490
  // as $variables['rows'].
merlinofchaos's avatar
merlinofchaos committed
491
  // However, the template also needs to use for the rendered fields.  We
492
  // therefore swap the raw data out to a new variable and reset $variables['rows']
merlinofchaos's avatar
merlinofchaos committed
493
494
  // so that it can get rebuilt.
  // Store rows so that they may be used by further preprocess functions.
495
496
497
  $result = $variables['result'] = $variables['rows'];
  $variables['rows'] = array();
  $variables['header'] = array();
merlinofchaos's avatar
merlinofchaos committed
498

499
500
  $options = $view->style_plugin->options;
  $handler = $view->style_plugin;
merlinofchaos's avatar
merlinofchaos committed
501
502
503

  $row_class_special = isset($options['row_class_special']) ? $options['row_class_special'] : TRUE;

504
  $fields = &$view->field;
505
  $columns = $handler->sanitizeColumns($options['columns'], $fields);
merlinofchaos's avatar
merlinofchaos committed
506

507
508
  $active = !empty($handler->active) ? $handler->active : '';
  $order = !empty($handler->order) ? $handler->order : 'asc';
merlinofchaos's avatar
merlinofchaos committed
509

510
511
512
  // A boolean variable which stores whether the table has a responsive class.
  $responsive = FALSE;

513
  $query = tablesort_get_query_parameters();
merlinofchaos's avatar
merlinofchaos committed
514
515
516
517
  if (isset($view->exposed_raw_input)) {
    $query += $view->exposed_raw_input;
  }

518
519
  // A boolean to store whether the table's header has any labels.
  $has_header_labels = FALSE;
merlinofchaos's avatar
merlinofchaos committed
520
  foreach ($columns as $field => $column) {
521
522
    // Create a second variable so we can easily find what fields we have and
    // what the CSS classes should be.
523
    $variables['fields'][$field] = drupal_clean_css_identifier($field);
merlinofchaos's avatar
merlinofchaos committed
524
    if ($active == $field) {
525
      $variables['fields'][$field] .= ' active';
merlinofchaos's avatar
merlinofchaos committed
526
527
    }

528
    // Render the header labels.
merlinofchaos's avatar
merlinofchaos committed
529
    if ($field == $column && empty($fields[$field]->options['exclude'])) {
530
      $label = String::checkPlain(!empty($fields[$field]) ? $fields[$field]->label() : '');
531
      if (empty($options['info'][$field]['sortable']) || !$fields[$field]->clickSortable()) {
532
        $variables['header'][$field]['content'] = $label;
merlinofchaos's avatar
merlinofchaos committed
533
534
535
536
537
538
539
540
541
542
      }
      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) {
543
544
545
546
547
          $tablesort_indicator = array(
            '#theme' => 'tablesort_indicator',
            '#style' => $initial,
          );
          $label .= drupal_render($tablesort_indicator);
merlinofchaos's avatar
merlinofchaos committed
548
549
550
551
552
553
554
555
556
        }

        $query['order'] = $field;
        $query['sort'] = $initial;
        $link_options = array(
          'html' => TRUE,
          'attributes' => array('title' => $title),
          'query' => $query,
        );
557
        $variables['header'][$field]['content'] = l($label, current_path(), $link_options);
merlinofchaos's avatar
merlinofchaos committed
558
559
560
      }

      // Set up the header label class.
561
      $variables['header'][$field]['attributes'] = array();
merlinofchaos's avatar
merlinofchaos committed
562
      if ($fields[$field]->options['element_default_classes']) {
563
564
        $variables['header'][$field]['attributes']['class'][] = 'views-field';
        $variables['header'][$field]['attributes']['class'][] = 'views-field-' . $variables['fields'][$field];
merlinofchaos's avatar
merlinofchaos committed
565
      }
566
      $class = $fields[$field]->elementLabelClasses(0);
merlinofchaos's avatar
merlinofchaos committed
567
      if ($class) {
568
        $variables['header'][$field]['attributes']['class'][] = $class;
merlinofchaos's avatar
merlinofchaos committed
569
      }
570
571
      // Add responsive header classes.
      if (!empty($options['info'][$field]['responsive'])) {
572
        $variables['header'][$field]['attributes']['class'][] = $options['info'][$field]['responsive'];
573
574
        $responsive = TRUE;
      }
575
      // Add a CSS align class to each field if one was set.
merlinofchaos's avatar
merlinofchaos committed
576
      if (!empty($options['info'][$field]['align'])) {
577
        $variables['header'][$field]['attributes']['class'][] = drupal_clean_css_identifier($options['info'][$field]['align']);
merlinofchaos's avatar
merlinofchaos committed
578
579
      }
      // Add a header label wrapper if one was selected.
580
      if ($variables['header'][$field]['content']) {
581
        $element_label_type = $fields[$field]->elementLabelType(TRUE, TRUE);
merlinofchaos's avatar
merlinofchaos committed
582
        if ($element_label_type) {
583
          $variables['header'][$field]['content'] = '<' . $element_label_type . '>' . $variables['header'][$field]['content'] . '</' . $element_label_type . '>';
merlinofchaos's avatar
merlinofchaos committed
584
        }
585
        // Improves accessibility of complex tables.
586
        $variables['header'][$field]['attributes']['id'] = drupal_html_id('view-' . $field . '-table-column');
merlinofchaos's avatar
merlinofchaos committed
587
      }
588
589
590
591
592
593
      // 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']);
merlinofchaos's avatar
merlinofchaos committed
594
595
    }

596
    // Add a CSS align class to each field if one was set.
merlinofchaos's avatar
merlinofchaos committed
597
    if (!empty($options['info'][$field]['align'])) {
598
      $variables['fields'][$field] .= ' ' . drupal_clean_css_identifier($options['info'][$field]['align']);
merlinofchaos's avatar
merlinofchaos committed
599
600
601
602
    }

    // Render each field into its appropriate column.
    foreach ($result as $num => $row) {
603
604
605
606
607
608
609
610
611
612

      // Skip building the attributes and content if the field is to be excluded
      // from the display.
      if (!empty($fields[$field]->options['exclude'])) {
        continue;
      }

      // Reference to the column in the loop to make the code easier to read.
      $column_reference =& $variables['rows'][$num]['columns'][$column];

613
      // Add field classes.
614
      $column_reference['attributes'] = array();
merlinofchaos's avatar
merlinofchaos committed
615
      if ($fields[$field]->options['element_default_classes']) {
616
617
        $column_reference['attributes']['class'][] = 'views-field';
        $column_reference['attributes']['class'][] = 'views-field-' . $variables['fields'][$field];
merlinofchaos's avatar
merlinofchaos committed
618
619
      }

620
      if ($classes = $fields[$field]->elementClasses($num)) {
621
        $column_reference['attributes']['class'][] = $classes;
merlinofchaos's avatar
merlinofchaos committed
622
      }
623

624
625
      // Add responsive header classes.
      if (!empty($options['info'][$field]['responsive'])) {
626
        $column_reference['attributes']['class'][] = $options['info'][$field]['responsive'];
627
628
      }

629
      // Improves accessibility of complex tables.
630
      if (isset($variables['header'][$field]['attributes']['id'])) {
631
        $column_reference['attributes']['headers'] = array($variables['header'][$field]['attributes']['id']);
632
633
      }

634
      if (!empty($fields[$field])) {
635
        $field_output = $handler->getField($num, $field);
636
        $element_type = $fields[$field]->elementType(TRUE, TRUE);
merlinofchaos's avatar
merlinofchaos committed
637
638
639
640
        if ($element_type) {
          $field_output = '<' . $element_type . '>' . $field_output . '</' . $element_type . '>';
        }

641
        // Only bother with separators and stuff if the field shows up.
642
        if (!empty($field_output) && empty($column_reference['content'])) {
643
          // Place the field into the column, along with an optional separator.
644
          if (!empty($column_reference['content'])) {
645
            if (!empty($options['info'][$column]['separator'])) {
646
              $column_reference['content'] .= Xss::filterAdmin($options['info'][$column]['separator']);
647
            }
merlinofchaos's avatar
merlinofchaos committed
648
          }
649
          else {
650
            $column_reference['content'] = '';
651
          }
652
          $column_reference['content'] .= $field_output;
merlinofchaos's avatar
merlinofchaos committed
653
654
        }
      }
655
      $column_reference['attributes'] = new Attribute($column_reference['attributes']);
merlinofchaos's avatar
merlinofchaos committed
656
657
    }

658
659
    // Remove columns if the option is hide empty column is checked and the
    // field is not empty.
merlinofchaos's avatar
merlinofchaos committed
660
661
    if (!empty($options['info'][$field]['empty_column'])) {
      $empty = TRUE;
662
      foreach ($variables['rows'] as $columns) {
merlinofchaos's avatar
merlinofchaos committed
663
664
665
        $empty &= empty($columns[$column]);
      }
      if ($empty) {
666
        foreach ($variables['rows'] as &$column_items) {
merlinofchaos's avatar
merlinofchaos committed
667
          unset($column_items[$column]);
668
          unset($variables['header'][$column]);
merlinofchaos's avatar
merlinofchaos committed
669
670
671
672
673
674
        }
      }
    }
  }

  // Hide table header if all labels are empty.
675
  if (!$has_header_labels) {
676
    $variables['header'] = array();
merlinofchaos's avatar
merlinofchaos committed
677
678
679
  }

  $count = 0;
680
  foreach ($variables['rows'] as $num => $row) {
681
    $variables['rows'][$num]['attributes'] = array();
682
    if ($row_class = $handler->getRowClass($num)) {
683
      $variables['rows'][$num]['attributes']['class'][] = $row_class;
merlinofchaos's avatar
merlinofchaos committed
684
    }
685
    $variables['rows'][$num]['attributes'] = new Attribute($variables['rows'][$num]['attributes']);
merlinofchaos's avatar
merlinofchaos committed
686
687
  }

688
689
690
  $variables['attributes']['class'][] = 'views-table';
  $variables['attributes']['class'][] = 'views-view-table';
  if (empty($variables['rows']) && !empty($options['empty_table'])) {
691
    $build = $view->display_handler->renderArea('empty');
692
693
    $variables['rows'][0]['columns'][0]['content'] = drupal_render($build);
    $variables['rows'][0]['attributes'] = new Attribute(array('class' => 'odd'));
merlinofchaos's avatar
merlinofchaos committed
694
    // Calculate the amounts of rows with output.
695
    $variables['rows'][0]['columns'][0]['attributes'] = new Attribute(array(
696
      'colspan' => count($variables['header']),
697
698
      'class' => 'views-empty',
    ));
merlinofchaos's avatar
merlinofchaos committed
699
700
701
  }

  if (!empty($options['sticky'])) {
702
    $variables['view']->element['#attached']['library'][] = 'core/drupal.tableheader';
703
    $variables['attributes']['class'][] = "sticky-enabled";
merlinofchaos's avatar
merlinofchaos committed
704
  }
705
  $variables['attributes']['class'][] = 'cols-' . count($variables['header']);
merlinofchaos's avatar
merlinofchaos committed
706

707
708
  // Add the caption to the list if set.
  if (!empty($handler->options['caption'])) {
709
710
    $variables['caption'] = Xss::filterAdmin($handler->options['caption']);
    $variables['caption_needed'] = TRUE;
merlinofchaos's avatar
merlinofchaos committed
711
  }
712
  else {
713
714
    $variables['caption'] = '';
    $variables['caption_needed'] = FALSE;
715
716
  }

717
718
719
  $variables['summary'] = $handler->options['summary'];
  $variables['description'] = $handler->options['description'];
  $variables['caption_needed'] |= !empty($variables['summary']) || !empty($variables['description']);
720

721
722
723
  // 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.
724
  if (isset($variables['header']) && $responsive) {
725
    $variables['view']->element['#attached']['library'][] = 'core/drupal.tableresponsive';
726
727
    // Add 'responsive-enabled' class to the table to identify it for JS.
    // This is needed to target tables constructed by this function.
728
    $variables['attributes']['class'][] = 'responsive-enabled';
729
  }
merlinofchaos's avatar
merlinofchaos committed
730
731
732
}

/**
733
734
735
736
 * Prepares variables for views grid style templates.
 *
 * Default template: views-view-grid.html.twig.
 *
737
 * @param array $variables
738
739
740
 *   An associative array containing:
 *   - view: The view object.
 *   - rows: An array of row items. Each row is an array of content.
merlinofchaos's avatar
merlinofchaos committed
741
 */
742
function template_preprocess_views_view_grid(&$variables) {
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
  $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.
760
  foreach ($variables['rows'] as $result_index => $item) {
761
762
763
764
765
766
767
768

    // Add the item.
    if ($horizontal) {
      $items[$row]['content'][$col]['content'] = $item;
    }
    else {
      $items[$col]['content'][$row]['content'] = $item;
    }
merlinofchaos's avatar
merlinofchaos committed
769

770
771
772
773
774
775
776
777
778
779
780
781
    // 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.
782
      $row_class = array_filter(explode(' ', $variables['view']->style_plugin->getCustomClass($result_index, 'row')));
783
784
785
786
787
788
789
790
791
      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);
merlinofchaos's avatar
merlinofchaos committed
792
793
      }
    }
794
795
796
797
798
799
800
801
802
803

    // 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';
merlinofchaos's avatar
merlinofchaos committed
804
805
        }
      }
806
      // Add custom column classes.
807
      $col_class = array_filter(explode(' ', $variables['view']->style_plugin->getCustomClass($result_index, 'col')));
808
809
810
811
812
813
814
815
816
817
818
819
820
821
      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);
      }
merlinofchaos's avatar
merlinofchaos committed
822
823
    }

824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
    // 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++;
merlinofchaos's avatar
merlinofchaos committed
839
840
841
842
843
844
845
846
847
848
849
850
      if (!$remainders && $row == $num_rows) {
        $row = 0;
        $col++;
      }
      elseif ($remainders && $row == $num_rows + 1) {
        $row = 0;
        $col++;
        $remainders--;
      }
    }
  }

851
852
  // Add items to the variables array.
  $variables['items'] = $items;
merlinofchaos's avatar
merlinofchaos committed
853
854
855
}

/**
856
857
858
859
 * Prepares variables for views unformatted rows templates.
 *
 * Default template: views-view-unformatted.html.twig.
 *
860
 * @param array $variables
861
862
863
 *   An associative array containing:
 *   - view: The view object.
 *   - rows: An array of row items. Each row is an array of content.
merlinofchaos's avatar
merlinofchaos committed
864
 */
865
866
867
function template_preprocess_views_view_unformatted(&$variables) {
  $view = $variables['view'];
  $rows = $variables['rows'];
merlinofchaos's avatar
merlinofchaos committed
868
869
870
871
872
873
874
875
876
  $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) {
877
878
879
    $variables['rows'][$id] = array();
    $variables['rows'][$id]['content'] = $row;
    $variables['rows'][$id]['attributes'] = array();
merlinofchaos's avatar
merlinofchaos committed
880
881
    $count++;
    if ($default_row_class) {
882
      $variables['rows'][$id]['attributes']['class'][] = 'views-row';
merlinofchaos's avatar
merlinofchaos committed
883
    }
884
    if ($row_class = $view->style_plugin->getRowClass($id)) {
885
      $variables['rows'][$id]['attributes']['class'][] = $row_class;
merlinofchaos's avatar
merlinofchaos committed
886
    }
887
    $variables['rows'][$id]['attributes'] = new Attribute($variables['rows'][$id]['attributes']);
merlinofchaos's avatar
merlinofchaos committed
888
889
890
891
  }
}

/**
892
893
894
895
896
897
898
 * 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.
merlinofchaos's avatar
merlinofchaos committed
899
 */
900
901
function template_preprocess_views_view_list(&$variables) {
  $handler  = $variables['view']->style_plugin;
merlinofchaos's avatar
merlinofchaos committed
902

903
  // Fetch classes from handler options.
merlinofchaos's avatar
merlinofchaos committed
904
  $class = explode(' ', $handler->options['class']);
905
  $class = array_map('drupal_clean_css_identifier', $class);
merlinofchaos's avatar
merlinofchaos committed
906

907
  // Fetch wrapper classes from handler options.
merlinofchaos's avatar
merlinofchaos committed
908
  $wrapper_class = explode(' ', $handler->options['wrapper_class']);
909
  $wrapper_class = array_map('drupal_clean_css_identifier', $wrapper_class);
merlinofchaos's avatar
merlinofchaos committed
910

911
912
  // Initialize a new attribute class for $wrapper_class.
  if ($wrapper_class) {
913
    $variables['attributes']['class'] = $wrapper_class;
merlinofchaos's avatar
merlinofchaos committed
914
915
  }

916
917
918
  // Initialize a new attribute class for $class.
  $variables['list']['attributes'] = new Attribute(array('class' => $class));
  $variables['list']['type'] = $handler->options['type'];
919

920
  template_preprocess_views_view_unformatted($variables);
merlinofchaos's avatar
merlinofchaos committed
921
922
923
}

/**
924
925
926
927
 * Prepares variables for RSS feed templates.
 *
 * Default template: views-view-rss.html.twig.
 *
928
 * @param array $variables
929
930
931
 *   An associative array containing:
 *   - view: A ViewExecutable object.
 *   - rows: The raw row data.
932
 */
933
function template_preprocess_views_view_rss(&$variables) {
934
  $view  = $variables['view'];
935
  $items = $variables['rows'];
936
  $style = $view->style_plugin;
merlinofchaos's avatar
merlinofchaos committed
937

938
  $config = \Drupal::config('system.site');
939

merlinofchaos's avatar
merlinofchaos committed
940
941
942
  // 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 &amp becoming &amp;amp;).
943
  $variables['description'] = String::checkPlain(decode_entities(strip_tags($style->getDescription())));
merlinofchaos's avatar
merlinofchaos committed
944

945
  if ($view->display_handler->getOption('sitename_title')) {
946
    $title = $config->get('name');
947