views.theme.inc 43.1 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\Xss;
9
use Drupal\Core\Language\Language;
10
use Drupal\Core\Template\Attribute;
11
use Drupal\views\ViewExecutable;
12

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

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

31
32
33
  $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
34

35
  // Basic classes.
36
  $variables['css_class'] = '';
37

38
39
40
41
42
  $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
43

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

50
  $empty = empty($view->result);
51
52
53
  $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
54

55
56
57
  $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
58

59
  $variables['pager']      = '';
merlinofchaos's avatar
merlinofchaos committed
60
61
62

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

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

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

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

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

  // If using AJAX, send identifying data about this view.
108
  if ($view->ajaxEnabled() && empty($view->is_attachment) && empty($view->live_preview)) {
merlinofchaos's avatar
merlinofchaos committed
109
110
111
112
    $settings = array(
      'views' => array(
        'ajax_path' => url('views/ajax'),
        'ajaxViews' => array(
113
          'views_dom_id:' . $variables['dom_id'] => array(
114
            'view_name' => $view->storage->id(),
merlinofchaos's avatar
merlinofchaos committed
115
116
            'view_display_id' => $view->current_display,
            'view_args' => check_plain(implode('/', $view->args)),
117
            'view_path' => check_plain(current_path()),
118
            'view_base_path' => $view->getPath(),
119
            'view_dom_id' => $variables['dom_id'],
merlinofchaos's avatar
merlinofchaos committed
120
121
            // To fit multiple views on a page, the programmer may have
            // overridden the display's pager_element.
122
            'pager_element' => isset($view->pager) ? $view->pager->getPagerId() : 0,
merlinofchaos's avatar
merlinofchaos committed
123
124
125
126
          ),
        ),
      ),
    );
127
128
    $view->element['#attached']['js'][] = array('type' => 'setting', 'data' => $settings);
    $view->element['#attached']['library'][] = array('views', 'views.ajax');
merlinofchaos's avatar
merlinofchaos committed
129
130
  }

131
  // If form fields were found in the view, reformat the view output as a form.
merlinofchaos's avatar
merlinofchaos committed
132
  if (views_view_has_form_elements($view)) {
133
    // Copy the rows so as not to modify them by reference when rendering.
134
    $rows = $variables['rows'];
135
    $rows = drupal_render($rows);
136
    $output = !empty($rows) ? $rows : $variables['empty'];
merlinofchaos's avatar
merlinofchaos committed
137
138
139
140
    $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) {
141
142
143
144
145
146
      $variables['header'] = '';
      $variables['exposed'] = '';
      $variables['pager'] = '';
      $variables['footer'] = '';
      $variables['more'] = '';
      $variables['feed_icon'] = '';
merlinofchaos's avatar
merlinofchaos committed
147
    }
148
    $variables['rows'] = $form;
merlinofchaos's avatar
merlinofchaos committed
149
150
151
152
  }
}

/**
153
154
155
156
 * Prepares variables for views fields templates.
 *
 * Default template: views-view-fields.html.twig.
 *
157
 * @param array $variables
158
159
160
161
162
163
164
165
166
167
168
169
 *   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
170
 */
171
172
function template_preprocess_views_view_fields(&$variables) {
  $view = $variables['view'];
merlinofchaos's avatar
merlinofchaos committed
173
174
175

  // Loop through the fields for this view.
  $previous_inline = FALSE;
176
  $variables['fields'] = array(); // ensure it's at least an empty array.
merlinofchaos's avatar
merlinofchaos committed
177
178
  foreach ($view->field as $id => $field) {
    // render this even if set to exclude so it can be used elsewhere.
179
    $field_output = $view->style_plugin->getField($view->row_index, $id);
180
    $empty = $field->isValueEmpty($field_output, $field->options['empty_zero']);
181
    if (empty($field->options['exclude']) && (!$empty || (empty($field->options['hide_empty']) && empty($variables['options']['hide_empty'])))) {
merlinofchaos's avatar
merlinofchaos committed
182
183
      $object = new stdClass();
      $object->handler = &$view->field[$id];
184
      $object->inline = !empty($variables['options']['inline'][$id]);
merlinofchaos's avatar
merlinofchaos committed
185

186
      $object->element_type = $object->handler->elementType(TRUE, !$variables['options']['default_field_elements'], $object->inline);
merlinofchaos's avatar
merlinofchaos committed
187
      if ($object->element_type) {
188
        $attributes = array();
merlinofchaos's avatar
merlinofchaos committed
189
        if ($object->handler->options['element_default_classes']) {
190
          $attributes['class'][] = 'field-content';
merlinofchaos's avatar
merlinofchaos committed
191
192
        }

193
        if ($classes = $object->handler->elementClasses($view->row_index)) {
194
          $attributes['class'][] = $classes;
merlinofchaos's avatar
merlinofchaos committed
195
        }
196
        $attributes = new Attribute($attributes);
merlinofchaos's avatar
merlinofchaos committed
197
198

        $pre = '<' . $object->element_type;
199
        $pre .= $attributes;
merlinofchaos's avatar
merlinofchaos committed
200
201
202
203
204
205
206
207
208
209
        $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;
210
211
      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
212
213
214
215
216
      }
      else {
        $object->raw = NULL; // make sure it exists to reduce NOTICE
      }

217
218
      if (!empty($variables['options']['separator']) && $previous_inline && $object->inline && $object->content) {
        $object->separator = filter_xss_admin($variables['options']['separator']);
merlinofchaos's avatar
merlinofchaos committed
219
220
221
222
223
      }

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

      $previous_inline = $object->inline;
224
      $object->inline_html = $object->handler->elementWrapperType(TRUE, TRUE);
225
      if ($object->inline_html === '' && $variables['options']['default_field_elements']) {
merlinofchaos's avatar
merlinofchaos committed
226
227
228
229
230
231
232
233
        $object->inline_html = $object->inline ? 'span' : 'div';
      }

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

      if ($object->inline_html) {
234
        $attributes = array();
merlinofchaos's avatar
merlinofchaos committed
235
        if ($object->handler->options['element_default_classes']) {
236
237
          $attributes['class'][] = 'views-field';
          $attributes['class'][] = 'views-field-' . $object->class;
merlinofchaos's avatar
merlinofchaos committed
238
239
        }

240
        if ($classes = $object->handler->elementWrapperClasses($view->row_index)) {
241
          $attributes['class'][] = $classes;
merlinofchaos's avatar
merlinofchaos committed
242
        }
243
        $attributes = new Attribute($attributes);
merlinofchaos's avatar
merlinofchaos committed
244
245

        $object->wrapper_prefix = '<' . $object->inline_html;
246
        $object->wrapper_prefix .= $attributes;
merlinofchaos's avatar
merlinofchaos committed
247
248
249
250
251
252
253
254
255
256
257
258
259
260
        $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 .= ': ';
        }

261
        $object->elementLabelType = $object->handler->elementLabelType(TRUE, !$variables['options']['default_field_elements']);
262
        if ($object->elementLabelType) {
263
          $attributes = array();
merlinofchaos's avatar
merlinofchaos committed
264
          if ($object->handler->options['element_default_classes']) {
265
266
            $attributes['class'][] = 'views-label';
            $attributes['class'][] = 'views-label-' . $object->class;
merlinofchaos's avatar
merlinofchaos committed
267
268
          }

269
          $element_label_class = $object->handler->elementLabelClasses($view->row_index);
merlinofchaos's avatar
merlinofchaos committed
270
          if ($element_label_class) {
271
            $attributes['class'][] = $element_label_class;
merlinofchaos's avatar
merlinofchaos committed
272
          }
273
          $attributes = new Attribute($attributes);
merlinofchaos's avatar
merlinofchaos committed
274

275
          $pre = '<' . $object->elementLabelType;
276
          $pre .= $attributes;
merlinofchaos's avatar
merlinofchaos committed
277
278
          $pre .= '>';

279
          $object->label_html = $pre . $object->label_html . '</' . $object->elementLabelType . '>';
merlinofchaos's avatar
merlinofchaos committed
280
281
282
        }
      }

283
      $variables['fields'][$id] = $object;
merlinofchaos's avatar
merlinofchaos committed
284
285
286
287
288
    }
  }

}

289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/**
* 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 = '';

307
  foreach ($fields as $field) {
308
309
310
311
312
313
314
315
316
317
318
319
320
321
    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
322
/**
323
324
325
326
 * Prepares variables for views single grouping templates.
 *
 * Default template: views-view-grouping.html.twig.
 *
327
 * @param array $variables
328
329
330
331
332
333
334
335
 *   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.
  */
336
337
function template_preprocess_views_view_grouping(&$variables) {
  $variables['content'] = $variables['view']->style_plugin->renderGroupingSets($variables['rows'], $variables['grouping_level']);
merlinofchaos's avatar
merlinofchaos committed
338
339
340
341
342
343
344
345
346
}

/**
 * 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
 */
347
348
function theme_views_view_field($variables) {
  return $variables['output'];
merlinofchaos's avatar
merlinofchaos committed
349
350
351
}

/**
352
 * Prepares variables for views field templates.
merlinofchaos's avatar
merlinofchaos committed
353
 *
354
355
 * Default template: views-view-field.html.twig.
 *
356
 * @param array $variables
357
358
359
360
361
 *   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
362
 */
363
364
function template_preprocess_views_view_field(&$variables) {
  $variables['output'] = $variables['field']->advancedRender($variables['row']);
merlinofchaos's avatar
merlinofchaos committed
365
366
367
}

/**
368
369
370
371
372
373
 * Prepares variables for views summary templates.
 *
 * The summary prints a single record from a row, with fields.
 *
 * Default template: views-view-summary.html.twig.
 *
374
 * @param array $variables
375
376
377
 *   An associative array containing:
 *   - view: A ViewExecutable object.
 *   - rows: The raw row data.
merlinofchaos's avatar
merlinofchaos committed
378
 */
379
380
function template_preprocess_views_view_summary(&$variables) {
  $view = $variables['view'];
merlinofchaos's avatar
merlinofchaos committed
381
382
383
384
385
386
387
388
389
  $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(
390
391
    url(current_path(), array('alias' => TRUE)), // force system path
    url(current_path()), // could be an alias
merlinofchaos's avatar
merlinofchaos committed
392
393
  ));

394
395
396
  // 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
397
398
  $row_args = array();

399
  foreach ($variables['rows'] as $id => $row) {
400
    $row_args[$id] = $argument->summaryArgument($row);
merlinofchaos's avatar
merlinofchaos committed
401
  }
402
  $argument->processSummaryArguments($row_args);
merlinofchaos's avatar
merlinofchaos committed
403

404
  foreach ($variables['rows'] as $id => $row) {
405
    $variables['rows'][$id]->attributes = array();
406
    $variables['rows'][$id]->link = $argument->summaryName($row);
merlinofchaos's avatar
merlinofchaos committed
407
408
409
410
411
412
413
    $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'];
    }
414
415
416
    $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])) {
417
      $variables['rows'][$id]->attributes['class'][] = 'active';
merlinofchaos's avatar
merlinofchaos committed
418
    }
419
    $variables['rows'][$id]->attributes = new Attribute($variables['rows'][$id]->attributes);
merlinofchaos's avatar
merlinofchaos committed
420
421
422
423
  }
}

/**
424
425
426
427
 * Prepares variables for unformatted summary view templates.
 *
 * Default template: views-view-summary-unformatted.html.twig.
 *
428
 * @param array $variables
429
430
431
432
433
434
 *   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
435
 */
436
437
function template_preprocess_views_view_summary_unformatted(&$variables) {
  $view = $variables['view'];
merlinofchaos's avatar
merlinofchaos committed
438
439
440
441
442
443
444
445
446
447
  $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(
448
449
450
451
    // Force system path.
    url(current_path(), array('alias' => TRUE)),
    // Could be an alias.
    url(current_path()),
merlinofchaos's avatar
merlinofchaos committed
452
453
  ));

454
455
456
  // 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
457
  $row_args = array();
458
  foreach ($variables['rows'] as $id => $row) {
459
    $row_args[$id] = $argument->summaryArgument($row);
merlinofchaos's avatar
merlinofchaos committed
460
  }
461
  $argument->processSummaryArguments($row_args);
merlinofchaos's avatar
merlinofchaos committed
462

463
  foreach ($variables['rows'] as $id => $row) {
464
    // Only false on first time.
merlinofchaos's avatar
merlinofchaos committed
465
    if ($count++) {
466
      $variables['rows'][$id]->separator = filter_xss_admin($variables['options']['separator']);
merlinofchaos's avatar
merlinofchaos committed
467
    }
468
    $variables['rows'][$id]->attributes = array();
469
    $variables['rows'][$id]->link = $argument->summaryName($row);
merlinofchaos's avatar
merlinofchaos committed
470
471
472
473
474
475
476
    $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'];
    }
477
478
479
    $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])) {
480
      $variables['rows'][$id]->attributes['class'][] = 'active';
merlinofchaos's avatar
merlinofchaos committed
481
    }
482
    $variables['rows'][$id]->attributes = new Attribute($variables['rows'][$id]->attributes);
merlinofchaos's avatar
merlinofchaos committed
483
484
485
486
  }
}

/**
487
488
489
490
 * Prepares variables for views table templates.
 *
 * Default template: views-view-table.html.twig.
 *
491
 * @param array $variables
492
493
494
 *   An associative array containing:
 *   - view: A ViewExecutable object.
 *   - rows: The raw row data.
merlinofchaos's avatar
merlinofchaos committed
495
 */
496
497
function template_preprocess_views_view_table(&$variables) {
  $view = $variables['view'];
merlinofchaos's avatar
merlinofchaos committed
498

499
  // We need the raw data for this grouping, which is passed in
500
  // as $variables['rows'].
merlinofchaos's avatar
merlinofchaos committed
501
  // However, the template also needs to use for the rendered fields.  We
502
  // therefore swap the raw data out to a new variable and reset $variables['rows']
merlinofchaos's avatar
merlinofchaos committed
503
504
  // so that it can get rebuilt.
  // Store rows so that they may be used by further preprocess functions.
505
506
507
  $result = $variables['result'] = $variables['rows'];
  $variables['rows'] = array();
  $variables['header'] = array();
merlinofchaos's avatar
merlinofchaos committed
508

509
510
  $options = $view->style_plugin->options;
  $handler = $view->style_plugin;
merlinofchaos's avatar
merlinofchaos committed
511
512
513

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

514
  $fields = &$view->field;
515
  $columns = $handler->sanitizeColumns($options['columns'], $fields);
merlinofchaos's avatar
merlinofchaos committed
516

517
518
  $active = !empty($handler->active) ? $handler->active : '';
  $order = !empty($handler->order) ? $handler->order : 'asc';
merlinofchaos's avatar
merlinofchaos committed
519

520
521
522
  // A boolean variable which stores whether the table has a responsive class.
  $responsive = FALSE;

523
  $query = tablesort_get_query_parameters();
merlinofchaos's avatar
merlinofchaos committed
524
525
526
527
  if (isset($view->exposed_raw_input)) {
    $query += $view->exposed_raw_input;
  }

528
529
  // A boolean to store whether the table's header has any labels.
  $has_header_labels = FALSE;
merlinofchaos's avatar
merlinofchaos committed
530
  foreach ($columns as $field => $column) {
531
532
    // Create a second variable so we can easily find what fields we have and
    // what the CSS classes should be.
533
    $variables['fields'][$field] = drupal_clean_css_identifier($field);
merlinofchaos's avatar
merlinofchaos committed
534
    if ($active == $field) {
535
      $variables['fields'][$field] .= ' active';
merlinofchaos's avatar
merlinofchaos committed
536
537
    }

538
    // Render the header labels.
merlinofchaos's avatar
merlinofchaos committed
539
540
    if ($field == $column && empty($fields[$field]->options['exclude'])) {
      $label = check_plain(!empty($fields[$field]) ? $fields[$field]->label() : '');
541
      if (empty($options['info'][$field]['sortable']) || !$fields[$field]->clickSortable()) {
542
        $variables['header'][$field]['content'] = $label;
merlinofchaos's avatar
merlinofchaos committed
543
544
545
546
547
548
549
550
551
552
      }
      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) {
553
554
555
556
557
          $tablesort_indicator = array(
            '#theme' => 'tablesort_indicator',
            '#style' => $initial,
          );
          $label .= drupal_render($tablesort_indicator);
merlinofchaos's avatar
merlinofchaos committed
558
559
560
561
562
563
564
565
566
        }

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

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

606
    // Add a CSS align class to each field if one was set.
merlinofchaos's avatar
merlinofchaos committed
607
    if (!empty($options['info'][$field]['align'])) {
608
      $variables['fields'][$field] .= ' ' . drupal_clean_css_identifier($options['info'][$field]['align']);
merlinofchaos's avatar
merlinofchaos committed
609
610
611
612
    }

    // Render each field into its appropriate column.
    foreach ($result as $num => $row) {
613
614
615
616
617
618
619
620
621
622

      // 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];

623
      // Add field classes.
624
      $column_reference['attributes'] = array();
merlinofchaos's avatar
merlinofchaos committed
625
      if ($fields[$field]->options['element_default_classes']) {
626
627
        $column_reference['attributes']['class'][] = 'views-field';
        $column_reference['attributes']['class'][] = 'views-field-' . $variables['fields'][$field];
merlinofchaos's avatar
merlinofchaos committed
628
629
      }

630
      if ($classes = $fields[$field]->elementClasses($num)) {
631
        $column_reference['attributes']['class'][] = $classes;
merlinofchaos's avatar
merlinofchaos committed
632
      }
633

634
635
      // Add responsive header classes.
      if (!empty($options['info'][$field]['responsive'])) {
636
        $column_reference['attributes']['class'][] = $options['info'][$field]['responsive'];
637
638
      }

639
      // Improves accessibility of complex tables.
640
      if (isset($variables['header'][$field]['attributes']['id'])) {
641
        $column_reference['attributes']['headers'] = array($variables['header'][$field]['attributes']['id']);
642
643
      }

644
      if (!empty($fields[$field])) {
645
        $field_output = $handler->getField($num, $field);
646
        $element_type = $fields[$field]->elementType(TRUE, TRUE);
merlinofchaos's avatar
merlinofchaos committed
647
648
649
650
        if ($element_type) {
          $field_output = '<' . $element_type . '>' . $field_output . '</' . $element_type . '>';
        }

651
        // Only bother with separators and stuff if the field shows up.
652
        if (!empty($field_output) && empty($column_reference['content'])) {
653
          // Place the field into the column, along with an optional separator.
654
          if (!empty($column_reference['content'])) {
655
            if (!empty($options['info'][$column]['separator'])) {
656
              $column_reference['content'] .= filter_xss_admin($options['info'][$column]['separator']);
657
            }
merlinofchaos's avatar
merlinofchaos committed
658
          }
659
          else {
660
            $column_reference['content'] = '';
661
          }
662
          $column_reference['content'] .= $field_output;
merlinofchaos's avatar
merlinofchaos committed
663
664
        }
      }
665
      $column_reference['attributes'] = new Attribute($column_reference['attributes']);
merlinofchaos's avatar
merlinofchaos committed
666
667
    }

668
669
    // Remove columns if the option is hide empty column is checked and the
    // field is not empty.
merlinofchaos's avatar
merlinofchaos committed
670
671
    if (!empty($options['info'][$field]['empty_column'])) {
      $empty = TRUE;
672
      foreach ($variables['rows'] as $columns) {
merlinofchaos's avatar
merlinofchaos committed
673
674
675
        $empty &= empty($columns[$column]);
      }
      if ($empty) {
676
        foreach ($variables['rows'] as &$column_items) {
merlinofchaos's avatar
merlinofchaos committed
677
          unset($column_items[$column]);
678
          unset($variables['header'][$column]);
merlinofchaos's avatar
merlinofchaos committed
679
680
681
682
683
684
        }
      }
    }
  }

  // Hide table header if all labels are empty.
685
  if (!$has_header_labels) {
686
    $variables['header'] = array();
merlinofchaos's avatar
merlinofchaos committed
687
688
689
  }

  $count = 0;
690
  foreach ($variables['rows'] as $num => $row) {
691
    $variables['rows'][$num]['attributes'] = array();
merlinofchaos's avatar
merlinofchaos committed
692
    if ($row_class_special) {
693
      $variables['rows'][$num]['attributes']['class'][] = ($count++ % 2 == 0) ? 'odd' : 'even';
694
      if ($num === 0) {
695
        $variables['rows'][$num]['attributes']['class'][] = 'views-row-first';
696
      }
697
      elseif ($num === (count($variables['rows']) - 1)) {
698
        $variables['rows'][$num]['attributes']['class'][] = 'views-row-last';
699
      }
merlinofchaos's avatar
merlinofchaos committed
700
    }
701
    if ($row_class = $handler->getRowClass($num)) {
702
      $variables['rows'][$num]['attributes']['class'][] = $row_class;
merlinofchaos's avatar
merlinofchaos committed
703
    }
704
    $variables['rows'][$num]['attributes'] = new Attribute($variables['rows'][$num]['attributes']);
merlinofchaos's avatar
merlinofchaos committed
705
706
  }

707
708
709
  $variables['attributes']['class'][] = 'views-table';
  $variables['attributes']['class'][] = 'views-view-table';
  if (empty($variables['rows']) && !empty($options['empty_table'])) {
710
    $build = $view->display_handler->renderArea('empty');
711
712
    $variables['rows'][0]['columns'][0]['content'] = drupal_render($build);
    $variables['rows'][0]['attributes'] = new Attribute(array('class' => 'odd'));
merlinofchaos's avatar
merlinofchaos committed
713
    // Calculate the amounts of rows with output.
714
    $variables['rows'][0]['columns'][0]['attributes'] = new Attribute(array(
715
      'colspan' => count($variables['header']),
716
717
      'class' => 'views-empty',
    ));
merlinofchaos's avatar
merlinofchaos committed
718
719
720
  }

  if (!empty($options['sticky'])) {
721
722
    $variables['view']->element['#attached']['library'][] = array('system', 'drupal.tableheader');
    $variables['attributes']['class'][] = "sticky-enabled";
merlinofchaos's avatar
merlinofchaos committed
723
  }
724
  $variables['attributes']['class'][] = 'cols-' . count($variables['header']);
merlinofchaos's avatar
merlinofchaos committed
725

726
727
  // Add the caption to the list if set.
  if (!empty($handler->options['caption'])) {
728
729
    $variables['caption'] = Xss::filterAdmin($handler->options['caption']);
    $variables['caption_needed'] = TRUE;
merlinofchaos's avatar
merlinofchaos committed
730
  }
731
  else {
732
733
    $variables['caption'] = '';
    $variables['caption_needed'] = FALSE;
734
735
  }

736
737
738
  $variables['summary'] = $handler->options['summary'];
  $variables['description'] = $handler->options['description'];
  $variables['caption_needed'] |= !empty($variables['summary']) || !empty($variables['description']);
739

740
741
742
  // 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.
743
744
  if (isset($variables['header']) && $responsive) {
    $variables['view']->element['#attached']['library'][] = array('system', 'drupal.tableresponsive');
745
746
    // Add 'responsive-enabled' class to the table to identify it for JS.
    // This is needed to target tables constructed by this function.
747
    $variables['attributes']['class'][] = 'responsive-enabled';
748
  }
merlinofchaos's avatar
merlinofchaos committed
749
750
751
}

/**
752
753
754
755
 * Prepares variables for views grid style templates.
 *
 * Default template: views-view-grid.html.twig.
 *
756
 * @param array $variables
757
758
759
 *   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
760
 */
761
function template_preprocess_views_view_grid(&$variables) {
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
  $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.
779
  foreach ($variables['rows'] as $result_index => $item) {
780
781
782
783
784
785
786
787

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

789
790
791
792
793
794
795
796
797
798
799
800
    // 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.
801
      $row_class = array_filter(explode(' ', $variables['view']->style_plugin->getCustomClass($result_index, 'row')));
802
803
804
805
806
807
808
809
810
      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
811
812
      }
    }
813
814
815
816
817
818
819
820
821
822

    // 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
823
824
        }
      }
825
      // Add custom column classes.
826
      $col_class = array_filter(explode(' ', $variables['view']->style_plugin->getCustomClass($result_index, 'col')));
827
828
829
830
831
832
833
834
835
836
837
838
839
840
      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
841
842
    }

843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
    // 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
858
859
860
861
862
863
864
865
866
867
868
869
      if (!$remainders && $row == $num_rows) {
        $row = 0;
        $col++;
      }
      elseif ($remainders && $row == $num_rows + 1) {
        $row = 0;
        $col++;
        $remainders--;
      }
    }
  }

870
871
  // Add items to the variables array.
  $variables['items'] = $items;
merlinofchaos's avatar
merlinofchaos committed
872
873
874
}

/**
875
876
877
878
 * Prepares variables for views unformatted rows templates.
 *
 * Default template: views-view-unformatted.html.twig.
 *
879
 * @param array $variables
880
881
882
 *   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
883
 */
884
885
886
function template_preprocess_views_view_unformatted(&$variables) {
  $view = $variables['view'];
  $rows = $variables['rows'];
merlinofchaos's avatar
merlinofchaos committed
887
888
889
890
891
892
893
894
895
  $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) {
896
897
898
    $variables['rows'][$id] = array();
    $variables['rows'][$id]['content'] = $row;
    $variables['rows'][$id]['attributes'] = array();
merlinofchaos's avatar
merlinofchaos committed
899
900
    $count++;
    if ($default_row_class) {
901
902
      $variables['rows'][$id]['attributes']['class'][] = 'views-row';
      $variables['rows'][$id]['attributes']['class'][] = 'views-row-' . $count;
merlinofchaos's avatar
merlinofchaos committed
903
904
    }
    if ($row_class_special) {
905
      $variables['rows'][$id]['attributes']['class'][] = 'views-row-' . ($count % 2 ? 'odd' : 'even');
merlinofchaos's avatar
merlinofchaos committed
906
      if ($count == 1) {
907
        $variables['rows'][$id]['attributes']['class'][] = 'views-row-first';
merlinofchaos's avatar
merlinofchaos committed
908
909
      }
      if ($count == $max) {
910
        $variables['rows'][$id]['attributes']['class'][] = 'views-row-last';
merlinofchaos's avatar
merlinofchaos committed
911
912
      }
    }
913
    if ($row_class = $view->style_plugin->getRowClass($id)) {
914
      $variables['rows'][$id]['attributes']['class'][] = $row_class;
merlinofchaos's avatar
merlinofchaos committed
915
    }
916
    $variables['rows'][$id]['attributes'] = new Attribute($variables['rows'][$id]['attributes']);
merlinofchaos's avatar
merlinofchaos committed
917
918
919
920
  }
}

/**
921
922
923
924
925
926
927
 * 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
928
 */
929
930
function template_preprocess_views_view_list(&$variables) {
  $handler  = $variables['view']->style_plugin;
merlinofchaos's avatar
merlinofchaos committed
931

932
  // Fetch classes from handler options.
merlinofchaos's avatar
merlinofchaos committed
933
  $class = explode(' ', $handler->options['class']);
934
  $class = array_map('drupal_clean_css_identifier', $class);
merlinofchaos's avatar
merlinofchaos committed
935

936
  // Fetch wrapper classes from handler options.
merlinofchaos's avatar
merlinofchaos committed
937
  $wrapper_class = explode(' ', $handler->options['wrapper_class']);
938
  $wrapper_class = array_map('drupal_clean_css_identifier', $wrapper_class);
merlinofchaos's avatar
merlinofchaos committed
939

940
941
  // Initialize a new attribute class for $wrapper_class.
  if ($wrapper_class) {
942
    $variables['attributes']['class'] = $wrapper_class;
merlinofchaos's avatar
merlinofchaos committed
943
944
  }

945
946
947
  // Initialize a new attribute class for $class.
  $variables['list']['attributes'] = new Attribute(array('class' => $class));
  $variables['list']['type'] = $handler->options['type'];
948

949
  template_preprocess_views_view_unformatted($variables);
merlinofchaos's avatar
merlinofchaos committed
950
951
952
}

/**
953
954
955
956
 * Prepares variables for RSS feed templates.
 *
 * Default template: views-view-rss.html.twig.
 *
957
 * @param array $variables