views.theme.inc 42.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\Core\Template\Attribute;
9
use Drupal\views\ViewExecutable;
10

merlinofchaos's avatar
merlinofchaos committed
11 12 13 14 15 16 17 18 19 20
/**
 * Provide a full array of possible themes to try for a given hook.
 *
 * @param $hook
 *   The hook to use. This is the base theme/template name.
 * @param $view
 *   The view being rendered.
 * @param $display
 *   The display being rendered, if applicable.
 */
21
function _views_theme_functions($hook, ViewExecutable $view, $display = NULL) {
merlinofchaos's avatar
merlinofchaos committed
22 23 24
  $themes = array();

  if ($display) {
25
    $themes[] = $hook . '__' . $view->storage->id() . '__' . $display['id'];
26
    $themes[] = $hook . '__' . $display['id'];
27
    // Add theme suggestions for each single tag.
28
    foreach (drupal_explode_tags($view->storage->get('tag')) as $tag) {
merlinofchaos's avatar
merlinofchaos committed
29 30 31
      $themes[] = $hook . '__' . preg_replace('/[^a-z0-9]/', '_', strtolower($tag));
    }

32
    if ($display['id'] != $display['display_plugin']) {
33
      $themes[] = $hook . '__' . $view->storage->id() . '__' . $display['display_plugin'];
34
      $themes[] = $hook . '__' . $display['display_plugin'];
merlinofchaos's avatar
merlinofchaos committed
35 36
    }
  }
37
  $themes[] = $hook . '__' . $view->storage->id();
merlinofchaos's avatar
merlinofchaos committed
38 39 40 41 42 43 44 45 46 47 48 49
  $themes[] = $hook;
  return $themes;
}

/**
 * Preprocess the primary theme implementation for a view.
 */
function template_preprocess_views_view(&$vars) {
  global $base_path;

  $view = $vars['view'];

50 51 52 53 54
  $vars['rows'] = (!empty($view->result) || $view->style_plugin->even_empty()) ? $view->style_plugin->render($view->result) : '';
  // Force a render array so CSS/JS can be added.
  if (!is_array($vars['rows'])) {
    $vars['rows'] = array('#markup' => $vars['rows']);
  }
merlinofchaos's avatar
merlinofchaos committed
55

56 57
  $vars['css_name'] = drupal_clean_css_identifier($view->storage->id());
  $vars['id'] = $view->storage->id();
merlinofchaos's avatar
merlinofchaos committed
58 59 60
  $vars['display_id'] = $view->current_display;

  // Basic classes
61 62
  $vars['css_class'] = '';

63 64
  $vars['attributes']['class'] = array();
  $vars['attributes']['class'][] = 'view';
65 66
  $vars['attributes']['class'][] = 'view-' . drupal_clean_css_identifier($vars['id']);
  $vars['attributes']['class'][] = 'view-id-' . $vars['id'];
67
  $vars['attributes']['class'][] = 'view-display-id-' . $vars['display_id'];
merlinofchaos's avatar
merlinofchaos committed
68

69
  $css_class = $view->display_handler->getOption('css_class');
merlinofchaos's avatar
merlinofchaos committed
70 71
  if (!empty($css_class)) {
    $vars['css_class'] = preg_replace('/[^a-zA-Z0-9- ]/', '-', $css_class);
72
    $vars['attributes']['class'][] = $vars['css_class'];
merlinofchaos's avatar
merlinofchaos committed
73 74
  }

75
  $empty = empty($view->result);
76 77
  $vars['header'] = $view->display_handler->renderArea('header', $empty);
  $vars['footer'] = $view->display_handler->renderArea('footer', $empty);
78
  $vars['empty'] = $empty ? $view->display_handler->renderArea('empty', $empty) : FALSE;
merlinofchaos's avatar
merlinofchaos committed
79 80

  $vars['exposed']    = !empty($view->exposed_widgets) ? $view->exposed_widgets : '';
81
  $vars['more']       = $view->display_handler->renderMoreLink();
merlinofchaos's avatar
merlinofchaos committed
82 83 84 85 86 87
  $vars['feed_icon']  = !empty($view->feed_icon) ? $view->feed_icon : '';

  $vars['pager']      = '';

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

90
  if ($view->display_handler->renderPager()) {
merlinofchaos's avatar
merlinofchaos committed
91
    $exposed_input = isset($view->exposed_raw_input) ? $view->exposed_raw_input : NULL;
92
    $vars['pager'] = $view->renderPager($exposed_input);
merlinofchaos's avatar
merlinofchaos committed
93 94
  }

95 96 97 98 99 100 101 102 103 104 105 106
  if (!empty($view->attachment_before)) {
    $vars['attachment_before'] = $view->attachment_before;
  }
  else {
    $vars['attachment_before'] = array();
  }
  if (!empty($view->attachment_after)) {
    $vars['attachment_after'] = $view->attachment_after;
  }
  else {
    $vars['attachment_after'] = array();
  }
merlinofchaos's avatar
merlinofchaos committed
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128

  // Add contextual links to the view. We need to attach them to the dummy
  // $view_array variable, since contextual_preprocess() requires that they be
  // attached to an array (not an object) in order to process them. For our
  // purposes, it doesn't matter what we attach them to, since once they are
  // processed by contextual_preprocess() they will appear in the $title_suffix
  // variable (which we will then render in views-view.tpl.php).
  views_add_contextual_links($vars['view_array'], 'view', $view, $view->current_display);

  // Attachments are always updated with the outer view, never by themselves,
  // so they do not have dom ids.
  if (empty($view->is_attachment)) {
    // Our JavaScript needs to have some means to find the HTML belonging to this
    // view.
    //
    // It is true that the DIV wrapper has classes denoting the name of the view
    // and its display ID, but this is not enough to unequivocally match a view
    // with its HTML, because one view may appear several times on the page. So
    // we set up a hash with the current time, $dom_id, to issue a "unique" identifier for
    // each view. This identifier is written to both Drupal.settings and the DIV
    // wrapper.
    $vars['dom_id'] = $view->dom_id;
129
    $vars['attributes']['class'][] = 'view-dom-id-' . $vars['dom_id'];
merlinofchaos's avatar
merlinofchaos committed
130 131 132
  }

  // If using AJAX, send identifying data about this view.
133
  if ($view->ajaxEnabled() && empty($view->is_attachment) && empty($view->live_preview)) {
merlinofchaos's avatar
merlinofchaos committed
134 135 136 137 138
    $settings = array(
      'views' => array(
        'ajax_path' => url('views/ajax'),
        'ajaxViews' => array(
          'views_dom_id:' . $vars['dom_id'] => array(
139
            'view_name' => $view->storage->id(),
merlinofchaos's avatar
merlinofchaos committed
140 141
            'view_display_id' => $view->current_display,
            'view_args' => check_plain(implode('/', $view->args)),
142
            'view_path' => check_plain(current_path()),
143
            'view_base_path' => $view->getPath(),
merlinofchaos's avatar
merlinofchaos committed
144 145 146
            'view_dom_id' => $vars['dom_id'],
            // To fit multiple views on a page, the programmer may have
            // overridden the display's pager_element.
147
            'pager_element' => isset($view->pager) ? $view->pager->get_pager_id() : 0,
merlinofchaos's avatar
merlinofchaos committed
148 149 150 151
          ),
        ),
      ),
    );
152 153
    $view->element['#attached']['js'][] = array('type' => 'setting', 'data' => $settings);
    $view->element['#attached']['library'][] = array('views', 'views.ajax');
merlinofchaos's avatar
merlinofchaos committed
154 155 156 157
  }

  // If form fields were found in the View, reformat the View output as a form.
  if (views_view_has_form_elements($view)) {
158 159 160
    // Copy the rows so as not to modify them by reference when rendering.
    $rows = $vars['rows'];
    $rows = drupal_render($rows);
161
    $output = !empty($rows) ? $rows : $vars['empty'];
merlinofchaos's avatar
merlinofchaos committed
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
    $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) {
      $vars['header'] = '';
      $vars['exposed'] = '';
      $vars['pager'] = '';
      $vars['footer'] = '';
      $vars['more'] = '';
      $vars['feed_icon'] = '';
    }
    $vars['rows'] = $form;
  }
}

/**
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
 * Prepares variables for views fields templates.
 *
 * Default template: views-view-fields.html.twig.
 *
 * @param array $vars
 *   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
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
 */
function template_preprocess_views_view_fields(&$vars) {
  $view = $vars['view'];

  // Loop through the fields for this view.
  $previous_inline = FALSE;
  $vars['fields'] = array(); // ensure it's at least an empty array.
  foreach ($view->field as $id => $field) {
    // render this even if set to exclude so it can be used elsewhere.
    $field_output = $view->style_plugin->get_field($view->row_index, $id);
    $empty = $field->is_value_empty($field_output, $field->options['empty_zero']);
    if (empty($field->options['exclude']) && (!$empty || (empty($field->options['hide_empty']) && empty($vars['options']['hide_empty'])))) {
      $object = new stdClass();
      $object->handler = &$view->field[$id];
      $object->inline = !empty($vars['options']['inline'][$id]);

      $object->element_type = $object->handler->element_type(TRUE, !$vars['options']['default_field_elements'], $object->inline);
      if ($object->element_type) {
213
        $attributes = array();
merlinofchaos's avatar
merlinofchaos committed
214
        if ($object->handler->options['element_default_classes']) {
215
          $attributes['class'][] = 'field-content';
merlinofchaos's avatar
merlinofchaos committed
216 217 218
        }

        if ($classes = $object->handler->element_classes($view->row_index)) {
219
          $attributes['class'][] = $classes;
merlinofchaos's avatar
merlinofchaos committed
220
        }
221
        $attributes = new Attribute($attributes);
merlinofchaos's avatar
merlinofchaos committed
222 223

        $pre = '<' . $object->element_type;
224
        $pre .= $attributes;
merlinofchaos's avatar
merlinofchaos committed
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
        $field_output = $pre . '>' . $field_output . '</' . $object->element_type . '>';
      }

      // Protect ourself somewhat for backward compatibility. This will prevent
      // old templates from producing invalid HTML when no element type is selected.
      if (empty($object->element_type)) {
        $object->element_type = 'span';
      }

      $object->content = $field_output;
      if (isset($view->field[$id]->field_alias) && isset($vars['row']->{$view->field[$id]->field_alias})) {
        $object->raw = $vars['row']->{$view->field[$id]->field_alias};
      }
      else {
        $object->raw = NULL; // make sure it exists to reduce NOTICE
      }

      if (!empty($vars['options']['separator']) && $previous_inline && $object->inline && $object->content) {
        $object->separator = filter_xss_admin($vars['options']['separator']);
      }

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

      $previous_inline = $object->inline;
      $object->inline_html = $object->handler->element_wrapper_type(TRUE, TRUE);
      if ($object->inline_html === '' && $vars['options']['default_field_elements']) {
        $object->inline_html = $object->inline ? 'span' : 'div';
      }

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

      if ($object->inline_html) {
259
        $attributes = array();
merlinofchaos's avatar
merlinofchaos committed
260
        if ($object->handler->options['element_default_classes']) {
261 262
          $attributes['class'][] = 'views-field';
          $attributes['class'][] = 'views-field-' . $object->class;
merlinofchaos's avatar
merlinofchaos committed
263 264 265
        }

        if ($classes = $object->handler->element_wrapper_classes($view->row_index)) {
266
          $attributes['class'][] = $classes;
merlinofchaos's avatar
merlinofchaos committed
267
        }
268
        $attributes = new Attribute($attributes);
merlinofchaos's avatar
merlinofchaos committed
269 270

        $object->wrapper_prefix = '<' . $object->inline_html;
271
        $object->wrapper_prefix .= $attributes;
merlinofchaos's avatar
merlinofchaos committed
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
        $object->wrapper_prefix .= '>';
        $object->wrapper_suffix = '</' . $object->inline_html . '>';
      }

      // Set up the label for the value and the HTML to make it easier
      // on the template.
      $object->label = check_plain($view->field[$id]->label());
      $object->label_html = '';
      if ($object->label) {
        $object->label_html .= $object->label;
        if ($object->handler->options['element_label_colon']) {
          $object->label_html .= ': ';
        }

        $object->element_label_type = $object->handler->element_label_type(TRUE, !$vars['options']['default_field_elements']);
        if ($object->element_label_type) {
288
          $attributes = array();
merlinofchaos's avatar
merlinofchaos committed
289
          if ($object->handler->options['element_default_classes']) {
290 291
            $attributes['class'][] = 'views-label';
            $attributes['class'][] = 'views-label-' . $object->class;
merlinofchaos's avatar
merlinofchaos committed
292 293 294 295
          }

          $element_label_class = $object->handler->element_label_classes($view->row_index);
          if ($element_label_class) {
296
            $attributes['class'][] = $element_label_class;
merlinofchaos's avatar
merlinofchaos committed
297
          }
298
          $attributes = new Attribute($attributes);
merlinofchaos's avatar
merlinofchaos committed
299 300

          $pre = '<' . $object->element_label_type;
301
          $pre .= $attributes;
merlinofchaos's avatar
merlinofchaos committed
302 303 304 305 306 307 308 309 310 311 312 313
          $pre .= '>';

          $object->label_html = $pre . $object->label_html . '</' . $object->element_label_type . '>';
        }
      }

      $vars['fields'][$id] = $object;
    }
  }

}

314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
/**
* Returns HTML for multiple views fields.
*
* @param $variables
*   An associative array containing:
*   - fields: An array of field objects. Each field object contains:
*     - separator: A string that separates the fields.
*     - wrapper_suffix: A string added to the beginning of the fields.
*     - label_html: An HTML string that labels the fields.
*     - content: The fields.
*     - wrapper_suffix: A string added to the end of the fields.
*
* @see template_preprocess_views_view_fields()
*/
function theme_views_view_fields($variables) {
  $fields = $variables['fields'];
  $output = '';

  foreach ($fields as $id => $field) {
    if (!empty($field->separator)) {
      $output .= $field->separator;
    }

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

    $output .= $field->wrapper_suffix;
  }

  return $output;
}

merlinofchaos's avatar
merlinofchaos committed
347
/**
348 349 350 351 352 353 354 355 356 357 358 359 360
 * Prepares variables for views single grouping templates.
 *
 * Default template: views-view-grouping.html.twig.
 *
 * @param array $vars
 *   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.
  */
merlinofchaos's avatar
merlinofchaos committed
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
function template_preprocess_views_view_grouping(&$vars) {
  $vars['content'] = $vars['view']->style_plugin->render_grouping_sets($vars['rows'], $vars['grouping_level']);
}

/**
 * Display a single views field.
 *
 * Interesting bits of info:
 * $field->field_alias says what the raw value in $row will be. Reach it like
 * this: @code { $row->{$field->field_alias} @endcode
 */
function theme_views_view_field($vars) {
  $view = $vars['view'];
  $field = $vars['field'];
  $row = $vars['row'];
  return $vars['output'];
}

/**
380
 * Prepares variables for views field templates.
merlinofchaos's avatar
merlinofchaos committed
381
 *
382 383 384 385 386 387 388 389
 * Default template: views-view-field.html.twig.
 *
 * @param array $vars
 *   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
390 391 392 393 394 395
 */
function template_preprocess_views_view_field(&$vars) {
  $vars['output'] = $vars['field']->advanced_render($vars['row']);
}

/**
396 397 398 399 400 401 402 403 404 405
 * Prepares variables for views summary templates.
 *
 * The summary prints a single record from a row, with fields.
 *
 * Default template: views-view-summary.html.twig.
 *
 * @param array $vars
 *   An associative array containing:
 *   - view: A ViewExecutable object.
 *   - rows: The raw row data.
merlinofchaos's avatar
merlinofchaos committed
406 407
 */
function template_preprocess_views_view_summary(&$vars) {
408
  $view = $vars['view'];
merlinofchaos's avatar
merlinofchaos committed
409 410 411 412 413 414 415 416 417 418
  $argument = $view->argument[$view->build_info['summary_level']];
  $vars['row_classes'] = array();

  $url_options = array();

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

  $active_urls = drupal_map_assoc(array(
419 420
    url(current_path(), array('alias' => TRUE)), // force system path
    url(current_path()), // could be an alias
merlinofchaos's avatar
merlinofchaos committed
421 422
  ));

423 424 425
  // 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
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
  $row_args = array();

  foreach ($vars['rows'] as $id => $row) {
    $row_args[$id] = $argument->summary_argument($row);
  }
  $argument->process_summary_arguments($row_args);

  foreach ($vars['rows'] as $id => $row) {
    $vars['rows'][$id]->link = $argument->summary_name($row);
    $args = $view->args;
    $args[$argument->position] = $row_args[$id];

    $base_path = NULL;
    if (!empty($argument->options['summary_options']['base_path'])) {
      $base_path = $argument->options['summary_options']['base_path'];
    }
442
    $vars['rows'][$id]->url = url($view->getUrl($args, $base_path), $url_options);
merlinofchaos's avatar
merlinofchaos committed
443
    $vars['rows'][$id]->count = intval($row->{$argument->count_alias});
444
    $vars['row_classes'][$id] = array();
merlinofchaos's avatar
merlinofchaos committed
445
    if (isset($active_urls[$vars['rows'][$id]->url])) {
446
      $vars['row_classes'][$id]['class'][] = 'active';
merlinofchaos's avatar
merlinofchaos committed
447
    }
448
    $vars['row_classes'][$id] = new Attribute($vars['row_classes'][$id]);
merlinofchaos's avatar
merlinofchaos committed
449 450 451 452
  }
}

/**
453 454 455 456 457 458 459 460 461 462 463
 * Prepares variables for unformatted summary view templates.
 *
 * Default template: views-view-summary-unformatted.html.twig.
 *
 * @param array $vars
 *   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
464 465
 */
function template_preprocess_views_view_summary_unformatted(&$vars) {
466
  $view = $vars['view'];
merlinofchaos's avatar
merlinofchaos committed
467 468 469 470 471 472 473 474 475 476 477
  $argument = $view->argument[$view->build_info['summary_level']];
  $vars['row_classes'] = array();

  $url_options = array();

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

  $count = 0;
  $active_urls = drupal_map_assoc(array(
478 479 480 481
    // Force system path.
    url(current_path(), array('alias' => TRUE)),
    // Could be an alias.
    url(current_path()),
merlinofchaos's avatar
merlinofchaos committed
482 483
  ));

484 485 486
  // 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
487 488 489 490 491 492 493
  $row_args = array();
  foreach ($vars['rows'] as $id => $row) {
    $row_args[$id] = $argument->summary_argument($row);
  }
  $argument->process_summary_arguments($row_args);

  foreach ($vars['rows'] as $id => $row) {
494
    // Only false on first time.
merlinofchaos's avatar
merlinofchaos committed
495 496 497 498 499 500 501 502 503 504 505
    if ($count++) {
      $vars['rows'][$id]->separator = filter_xss_admin($vars['options']['separator']);
    }
    $vars['rows'][$id]->link = $argument->summary_name($row);
    $args = $view->args;
    $args[$argument->position] = $row_args[$id];

    $base_path = NULL;
    if (!empty($argument->options['summary_options']['base_path'])) {
      $base_path = $argument->options['summary_options']['base_path'];
    }
506
    $vars['rows'][$id]->url = url($view->getUrl($args, $base_path), $url_options);
merlinofchaos's avatar
merlinofchaos committed
507
    $vars['rows'][$id]->count = intval($row->{$argument->count_alias});
508
    $vars['row_classes'][$id] = array();
merlinofchaos's avatar
merlinofchaos committed
509
    if (isset($active_urls[$vars['rows'][$id]->url])) {
510
      $vars['row_classes'][$id]['class'][] = 'active';
merlinofchaos's avatar
merlinofchaos committed
511
    }
512
    $vars['row_classes'][$id] = new Attribute($vars['row_classes'][$id]);
merlinofchaos's avatar
merlinofchaos committed
513 514 515 516
  }
}

/**
517 518 519 520 521 522 523 524
 * Prepares variables for views table templates.
 *
 * Default template: views-view-table.html.twig.
 *
 * @param array $vars
 *   An associative array containing:
 *   - view: A ViewExecutable object.
 *   - rows: The raw row data.
merlinofchaos's avatar
merlinofchaos committed
525 526
 */
function template_preprocess_views_view_table(&$vars) {
527
  $view = $vars['view'];
merlinofchaos's avatar
merlinofchaos committed
528

529 530
  // We need the raw data for this grouping, which is passed in
  // as $vars['rows'].
merlinofchaos's avatar
merlinofchaos committed
531 532 533 534
  // However, the template also needs to use for the rendered fields.  We
  // therefore swap the raw data out to a new variable and reset $vars['rows']
  // so that it can get rebuilt.
  // Store rows so that they may be used by further preprocess functions.
535
  $result = $vars['result'] = $vars['rows'];
merlinofchaos's avatar
merlinofchaos committed
536 537 538 539
  $vars['rows'] = array();
  $vars['field_classes'] = array();
  $vars['header'] = array();

540 541
  $options = $view->style_plugin->options;
  $handler = $view->style_plugin;
merlinofchaos's avatar
merlinofchaos committed
542 543 544 545

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

546 547
  $fields = &$view->field;
  $columns = $handler->sanitize_columns($options['columns'], $fields);
merlinofchaos's avatar
merlinofchaos committed
548

549 550
  $active = !empty($handler->active) ? $handler->active : '';
  $order = !empty($handler->order) ? $handler->order : 'asc';
merlinofchaos's avatar
merlinofchaos committed
551

552 553 554
  // A boolean variable which stores whether the table has a responsive class.
  $responsive = FALSE;

555
  $query = tablesort_get_query_parameters();
merlinofchaos's avatar
merlinofchaos committed
556 557 558 559 560
  if (isset($view->exposed_raw_input)) {
    $query += $view->exposed_raw_input;
  }

  foreach ($columns as $field => $column) {
561 562
    // Create a second variable so we can easily find what fields we have and
    // what the CSS classes should be.
merlinofchaos's avatar
merlinofchaos committed
563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592
    $vars['fields'][$field] = drupal_clean_css_identifier($field);
    if ($active == $field) {
      $vars['fields'][$field] .= ' active';
    }

    // render the header labels
    if ($field == $column && empty($fields[$field]->options['exclude'])) {
      $label = check_plain(!empty($fields[$field]) ? $fields[$field]->label() : '');
      if (empty($options['info'][$field]['sortable']) || !$fields[$field]->click_sortable()) {
        $vars['header'][$field] = $label;
      }
      else {
        $initial = !empty($options['info'][$field]['default_sort_order']) ? $options['info'][$field]['default_sort_order'] : 'asc';

        if ($active == $field) {
          $initial = ($order == 'asc') ? 'desc' : 'asc';
        }

        $title = t('sort by @s', array('@s' => $label));
        if ($active == $field) {
          $label .= theme('tablesort_indicator', array('style' => $initial));
        }

        $query['order'] = $field;
        $query['sort'] = $initial;
        $link_options = array(
          'html' => TRUE,
          'attributes' => array('title' => $title),
          'query' => $query,
        );
593
        $vars['header'][$field] = l($label, current_path(), $link_options);
merlinofchaos's avatar
merlinofchaos committed
594 595 596
      }

      // Set up the header label class.
597
      $vars['header_classes'][$field] = array();
merlinofchaos's avatar
merlinofchaos committed
598
      if ($fields[$field]->options['element_default_classes']) {
599 600
        $vars['header_classes'][$field]['class'][] = 'views-field';
        $vars['header_classes'][$field]['class'][] = 'views-field-' . $vars['fields'][$field];
merlinofchaos's avatar
merlinofchaos committed
601
      }
602
      $vars['header_classes'][$field] = new Attribute($vars['header_classes'][$field]);
merlinofchaos's avatar
merlinofchaos committed
603 604
      $class = $fields[$field]->element_label_classes(0);
      if ($class) {
605
        $vars['header_classes'][$field]['class'][] = $class;
merlinofchaos's avatar
merlinofchaos committed
606
      }
607 608
      // Add responsive header classes.
      if (!empty($options['info'][$field]['responsive'])) {
609
        $vars['header_classes'][$field]['class'][] = $options['info'][$field]['responsive'];
610 611
        $responsive = TRUE;
      }
612
      // Add a CSS align class to each field if one was set.
merlinofchaos's avatar
merlinofchaos committed
613
      if (!empty($options['info'][$field]['align'])) {
614
        $vars['header_classes'][$field]['class'][] = drupal_clean_css_identifier($options['info'][$field]['align']);
merlinofchaos's avatar
merlinofchaos committed
615 616 617 618 619 620 621 622 623 624
      }
      // Add a header label wrapper if one was selected.
      if ($vars['header'][$field]) {
        $element_label_type = $fields[$field]->element_label_type(TRUE, TRUE);
        if ($element_label_type) {
          $vars['header'][$field] = '<' . $element_label_type . '>' . $vars['header'][$field] . '</' . $element_label_type . '>';
        }
      }
    }

625
    // Add a CSS align class to each field if one was set.
merlinofchaos's avatar
merlinofchaos committed
626 627 628 629 630 631
    if (!empty($options['info'][$field]['align'])) {
      $vars['fields'][$field] .= ' ' . drupal_clean_css_identifier($options['info'][$field]['align']);
    }

    // Render each field into its appropriate column.
    foreach ($result as $num => $row) {
632
      // Add field classes.
633
      $vars['field_classes'][$field][$num] = array();
merlinofchaos's avatar
merlinofchaos committed
634
      if ($fields[$field]->options['element_default_classes']) {
635 636
        $vars['field_classes'][$field][$num]['class'][] = 'views-field';
        $vars['field_classes'][$field][$num]['class'][] = 'views-field-' . $vars['fields'][$field];
merlinofchaos's avatar
merlinofchaos committed
637
      }
638
      $vars['field_classes'][$field][$num] = new Attribute($vars['field_classes'][$field][$num]);
merlinofchaos's avatar
merlinofchaos committed
639

640 641
      if ($classes = $fields[$field]->element_classes($num)) {
        $vars['field_classes'][$field][$num]['class'][] = $classes;
merlinofchaos's avatar
merlinofchaos committed
642
      }
643 644
      // Add responsive header classes.
      if (!empty($options['info'][$field]['responsive'])) {
645
        $vars['field_classes'][$field][$num]['class'][] = $options['info'][$field]['responsive'];
646 647
      }

merlinofchaos's avatar
merlinofchaos committed
648
      if (!empty($fields[$field]) && empty($fields[$field]->options['exclude'])) {
649
        $field_output = $handler->get_field($num, $field);
merlinofchaos's avatar
merlinofchaos committed
650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672
        $element_type = $fields[$field]->element_type(TRUE, TRUE);
        if ($element_type) {
          $field_output = '<' . $element_type . '>' . $field_output . '</' . $element_type . '>';
        }

        // Don't bother with separators and stuff if the field does not show up.
        if (empty($field_output) && !empty($vars['rows'][$num][$column])) {
          continue;
        }

        // Place the field into the column, along with an optional separator.
        if (!empty($vars['rows'][$num][$column])) {
          if (!empty($options['info'][$column]['separator'])) {
            $vars['rows'][$num][$column] .= filter_xss_admin($options['info'][$column]['separator']);
          }
        }
        else {
          $vars['rows'][$num][$column] = '';
        }
        $vars['rows'][$num][$column] .= $field_output;
      }
    }

673 674
    // Remove columns if the option is hide empty column is checked and the
    // field is not empty.
merlinofchaos's avatar
merlinofchaos committed
675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694
    if (!empty($options['info'][$field]['empty_column'])) {
      $empty = TRUE;
      foreach ($vars['rows'] as $num => $columns) {
        $empty &= empty($columns[$column]);
      }
      if ($empty) {
        foreach ($vars['rows'] as $num => &$column_items) {
          unset($column_items[$column]);
          unset($vars['header'][$column]);
        }
      }
    }
  }

  // Hide table header if all labels are empty.
  if (!array_filter($vars['header'])) {
    $vars['header'] = array();
  }

  $count = 0;
695
  $vars['row_classes'] = array();
merlinofchaos's avatar
merlinofchaos committed
696
  foreach ($vars['rows'] as $num => $row) {
697
    $vars['row_classes'][$num] = array();
merlinofchaos's avatar
merlinofchaos committed
698
    if ($row_class_special) {
699 700 701 702 703 704 705
      $vars['row_classes'][$num]['class'][] = ($count++ % 2 == 0) ? 'odd' : 'even';
      if ($num === 0) {
        $vars['row_classes'][$num]['class'][] = 'views-row-first';
      }
      elseif ($num === (count($vars['rows']) - 1)) {
        $vars['row_classes'][$num]['class'][] = 'views-row-last';
      }
merlinofchaos's avatar
merlinofchaos committed
706 707
    }
    if ($row_class = $handler->get_row_class($num)) {
708
      $vars['row_classes'][$num]['class'][] = $row_class;
merlinofchaos's avatar
merlinofchaos committed
709
    }
710
    $vars['row_classes'][$num] = new Attribute($vars['row_classes'][$num]);
merlinofchaos's avatar
merlinofchaos committed
711 712
  }

713
  $vars['attributes']['class'][] = 'views-table';
714
  $vars['attributes']['class'][] = 'views-view-table';
merlinofchaos's avatar
merlinofchaos committed
715
  if (empty($vars['rows']) && !empty($options['empty_table'])) {
716 717 718
    $build = $view->display_handler->renderArea('empty');
    $vars['rows'][0][0] = drupal_render($build);
    $vars['row_classes'][0] = new Attribute();
merlinofchaos's avatar
merlinofchaos committed
719
    // Calculate the amounts of rows with output.
720 721 722 723
    $vars['field_classes'][0][0] = new Attribute(array(
      'colspan' => count($vars['header']),
      'class' => 'views-empty',
    ));
merlinofchaos's avatar
merlinofchaos committed
724 725 726
  }

  if (!empty($options['sticky'])) {
727
    $vars['view']->element['#attached']['library'][] = array('system', 'drupal.tableheader');
728
    $vars['attributes']['class'][] = "sticky-enabled";
merlinofchaos's avatar
merlinofchaos committed
729
  }
730
  $vars['attributes']['class'][] = 'cols-' . count($vars['header']);
merlinofchaos's avatar
merlinofchaos committed
731 732

  if (!empty($handler->options['summary'])) {
733
    $vars['attributes']['summary'] = $handler->options['summary'];
merlinofchaos's avatar
merlinofchaos committed
734
  }
735 736 737 738
  // If the table has headers and it should react responsively to columns hidden
  // with the classes represented by the constants RESPONSIVE_PRIORITY_MEDIUM
  // and RESPONSIVE_PRIORITY_LOW, add the tableresponsive behaviors.
  if (count($vars['header']) && $responsive) {
739
    $vars['view']->element['#attached']['library'][] = array('system', 'drupal.tableresponsive');
740 741 742 743
    // Add 'responsive-enabled' class to the table to identify it for JS.
    // This is needed to target tables constructed by this function.
    $vars['attributes']['class'][] = 'responsive-enabled';
  }
merlinofchaos's avatar
merlinofchaos committed
744 745 746
}

/**
747 748 749 750 751 752 753 754
 * Prepares variables for views grid style templates.
 *
 * Default template: views-view-grid.html.twig.
 *
 * @param array $vars
 *   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
755 756
 */
function template_preprocess_views_view_grid(&$vars) {
757 758 759
  $view = $vars['view'];
  $options = $view->style_plugin->options;
  $handler = $view->style_plugin;
merlinofchaos's avatar
merlinofchaos committed
760 761 762
  $default_row_class = isset($options['default_row_class']) ? $options['default_row_class'] : TRUE;
  $row_class_special = isset($options['row_class_special']) ? $options['row_class_special'] : TRUE;

763
  $columns = $options['columns'];
764
  $vars['attributes']['class'][] = 'views-view-grid cols-' . $columns;
merlinofchaos's avatar
merlinofchaos committed
765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817

  $rows = array();
  $row_indexes = array();

  if ($options['alignment'] == 'horizontal') {
    $row = array();
    $col_count = 0;
    $row_count = 0;
    $count = 0;
    foreach ($vars['rows'] as $row_index => $item) {
      $count++;
      $row[] = $item;
      $row_indexes[$row_count][$col_count] = $row_index;
      $col_count++;
      if ($count % $columns == 0) {
        $rows[] = $row;
        $row = array();
        $col_count = 0;
        $row_count++;
      }
    }
    if ($row) {
      // Fill up the last line only if it's configured, but this is default.
      if (!empty($handler->options['fill_single_line']) && count($rows)) {
        for ($i = 0; $i < ($columns - $col_count); $i++) {
          $row[] = '';
        }
      }
      $rows[] = $row;
    }
  }
  else {
    $num_rows = floor(count($vars['rows']) / $columns);
    // The remainders are the 'odd' columns that are slightly longer.
    $remainders = count($vars['rows']) % $columns;
    $row = 0;
    $col = 0;
    foreach ($vars['rows'] as $count => $item) {
      $rows[$row][$col] = $item;
      $row_indexes[$row][$col] = $count;
      $row++;

      if (!$remainders && $row == $num_rows) {
        $row = 0;
        $col++;
      }
      elseif ($remainders && $row == $num_rows + 1) {
        $row = 0;
        $col++;
        $remainders--;
      }
    }
    for ($i = 0; $i < count($rows[0]); $i++) {
818
      // This should be a string so this is ok.
merlinofchaos's avatar
merlinofchaos committed
819 820 821 822 823 824
      if (!isset($rows[count($rows) - 1][$i])) {
        $rows[count($rows) - 1][$i] = '';
      }
    }
  }

825
  // Apply the row classes.
merlinofchaos's avatar
merlinofchaos committed
826 827 828
  foreach ($rows as $row_number => $row) {
    $row_classes = array();
    if ($default_row_class) {
829
      $row_classes['class'][] = 'row-' . ($row_number + 1);
merlinofchaos's avatar
merlinofchaos committed
830 831 832
    }
    if ($row_class_special) {
      if ($row_number == 0) {
833
        $row_classes['class'][] = 'row-first';
merlinofchaos's avatar
merlinofchaos committed
834 835
      }
      if (count($rows) == ($row_number + 1)) {
836
        $row_classes['class'][] = 'row-last';
merlinofchaos's avatar
merlinofchaos committed
837 838
      }
    }
839 840
    $row_classes = new Attribute($row_classes);

merlinofchaos's avatar
merlinofchaos committed
841
    foreach ($rows[$row_number] as $column_number => $item) {
842
      $vars['column_classes'][$row_number][$column_number] = array();
merlinofchaos's avatar
merlinofchaos committed
843
      if ($default_row_class) {
844
        $vars['column_classes'][$row_number][$column_number]['class'][] = 'col-' . ($column_number + 1);
merlinofchaos's avatar
merlinofchaos committed
845 846 847
      }
      if ($row_class_special) {
        if ($column_number == 0) {
848
          $vars['column_classes'][$row_number][$column_number]['class'][] = 'col-first';
merlinofchaos's avatar
merlinofchaos committed
849 850
        }
        elseif (count($rows[$row_number]) == ($column_number + 1)) {
851
          $vars['column_classes'][$row_number][$column_number]['class'][] = 'col-last';
merlinofchaos's avatar
merlinofchaos committed
852 853 854
        }
      }
      if (isset($row_indexes[$row_number][$column_number]) && $column_class = $view->style_plugin->get_row_class($row_indexes[$row_number][$column_number])) {
855
        $vars['column_classes'][$row_number][$column_number]['class'][] = $column_class;
merlinofchaos's avatar
merlinofchaos committed
856
      }
857
      $vars['column_classes'][$row_number][$column_number] = new Attribute($vars['column_classes'][$row_number][$column_number]);
merlinofchaos's avatar
merlinofchaos committed
858 859 860 861
    }
  }
  $vars['rows'] = $rows;
  if (!empty($handler->options['summary'])) {
862
    $vars['attributes']['summary'] = $handler->options['summary'];
863
  }
merlinofchaos's avatar
merlinofchaos committed
864 865 866
}

/**
867 868 869 870 871 872 873 874
 * Prepares variables for views unformatted rows templates.
 *
 * Default template: views-view-unformatted.html.twig.
 *
 * @param array $vars
 *   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
875 876 877 878 879 880 881
 */
function template_preprocess_views_view_unformatted(&$vars) {
  $view = $vars['view'];
  $rows = $vars['rows'];
  $style = $view->style_plugin;
  $options = $style->options;

882
  $vars['row_classes'] = array();
merlinofchaos's avatar
merlinofchaos committed
883 884 885 886 887 888 889
  $vars['classes'] = array();
  $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) {
890
    $vars['row_classes'][$id] = array();
merlinofchaos's avatar
merlinofchaos committed
891 892
    $count++;
    if ($default_row_class) {
893 894
      $vars['row_classes'][$id]['class'][] = 'views-row';
      $vars['row_classes'][$id]['class'][] = 'views-row-' . $count;
merlinofchaos's avatar
merlinofchaos committed
895 896
    }
    if ($row_class_special) {
897
      $vars['row_classes'][$id]['class'][] = 'views-row-' . ($count % 2 ? 'odd' : 'even');
merlinofchaos's avatar
merlinofchaos committed
898
      if ($count == 1) {
899
        $vars['row_classes'][$id]['class'][] = 'views-row-first';
merlinofchaos's avatar
merlinofchaos committed
900 901
      }
      if ($count == $max) {
902
        $vars['row_classes'][$id]['class'][] = 'views-row-last';
merlinofchaos's avatar
merlinofchaos committed
903 904 905 906
      }
    }

    if ($row_class = $view->style_plugin->get_row_class($id)) {
907
      $vars['row_classes'][$id]['class'][] = $row_class;
merlinofchaos's avatar
merlinofchaos committed
908
    }
909
    $vars['row_classes'][$id] = new Attribute($vars['row_classes'][$id]);
merlinofchaos's avatar
merlinofchaos committed
910 911 912 913
  }
}

/**
914 915 916 917 918 919 920
 * 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
921
 */
922 923
function template_preprocess_views_view_list(&$variables) {
  $handler  = $variables['view']->style_plugin;
merlinofchaos's avatar
merlinofchaos committed
924

925
  // Fetch classes from handler options.
merlinofchaos's avatar
merlinofchaos committed
926
  $class = explode(' ', $handler->options['class']);
927
  $class = array_map('drupal_clean_css_identifier', $class);
merlinofchaos's avatar
merlinofchaos committed
928

929
  // Fetch wrapper classes from handler options.
merlinofchaos's avatar
merlinofchaos committed
930
  $wrapper_class = explode(' ', $handler->options['wrapper_class']);
931
  $wrapper_class = array_map('drupal_clean_css_identifier', $wrapper_class);
merlinofchaos's avatar
merlinofchaos committed
932

933 934
  // Initialize a new attribute class for $wrapper_class.
  if ($wrapper_class) {
935
    $variables['wrapper_attributes'] = new Attribute(array('class' => $wrapper_class));
merlinofchaos's avatar
merlinofchaos committed
936 937
  }

938 939 940
  // Initialize a new attribute class for $class.
  $variables['list']['attributes'] = new Attribute(array('class' => $class));
  $variables['list']['type'] = $handler->options['type'];
941

942
  template_preprocess_views_view_unformatted($variables);
merlinofchaos's avatar
merlinofchaos committed
943 944 945
}

/**
946 947 948 949 950 951 952 953 954
 * Prepares variables for RSS feed templates.
 *
 * Default template: views-view-rss.html.twig.
 *
 * @param array $vars
 *   An associative array containing:
 *   - view: A ViewExecutable object.
 *   - rows: The raw row data.
*/
merlinofchaos's avatar
merlinofchaos committed
955 956 957
function template_preprocess_views_view_rss(&$vars) {
  global $base_url;

958 959
  $view = $vars['view'];
  $items = $vars['rows'];
merlinofchaos's avatar
merlinofchaos committed
960

961
  $style = $view->style_plugin;
merlinofchaos's avatar
merlinofchaos committed
962

963 964
  $config = config('system.site');

merlinofchaos's avatar
merlinofchaos committed
965 966 967 968 969
  // 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;).
  $vars['description'] = check_plain(decode_entities(strip_tags($style->get_description())));

970
  if ($view->display_handler->getOption('sitename_title')) {
971
    $title = $config->get('name');
972
    if ($slogan = $config->get('slogan')) {
merlinofchaos's avatar
merlinofchaos committed
973 974 975 976
      $title .= ' - ' . $slogan;
    }
  }
  else {
977
    $title = $view->getTitle();
merlinofchaos's avatar
merlinofchaos committed
978 979 980
  }
  $vars['title'] = check_plain($title);

981 982
  // Figure out which display which has a path we're using for this feed. If
  // there isn't one, use the global $base_url
983
  $link_display_id = $view->display_handler->getLinkDisplay();
984 985
  if ($link_display_id && $display = $view->displayHandlers->get($link_display_id)) {
    $path = $view->displayHandlers->get($link_display_id)->getPath();
merlinofchaos's avatar
merlinofchaos committed
986 987 988
  }

  if ($path) {
989
    $path = $view->getUrl(NULL, $path);
merlinofchaos's avatar
merlinofchaos committed
990 991 992 993 994
    $url_options = array('absolute' => TRUE);
    if (!empty($view->exposed_raw_input)) {
      $url_options['query'] = $view->exposed_raw_input;
    }

995 996
    // Compare the link to the default home page; if it's the default home page,
    // just use $base_url.
997
    if ($path == $config->get('page.front')) {
merlinofchaos's avatar
merlinofchaos committed
998 999 1000 1001 1002 1003
      $path = '';
    }

    $vars['link'] = check_url(url($path, $url_options));
  }

1004
  $vars['langcode'] = check_plain(language(LANGUAGE_TYPE_INTERFACE)->langcode);
1005
  $vars['namespaces'] = new Attribute($style->namespaces);
merlinofchaos's avatar
merlinofchaos committed
1006 1007 1008 1009 1010 1011
  $vars['items'] = $items;
  $vars['channel_elements'] = format_xml_elements($style->channel_elements);

  // During live preview we don't want to output the header since the contents
  // of the feed are being displayed inside a normal HTML page.
  if (empty($vars['view']->live_preview)) {
1012
    $vars['view']->getResponse()->headers->set('Content-Type', 'application/rss+xml; charset=utf-8');
merlinofchaos's avatar
merlinofchaos committed
1013 1014 1015 1016
  }
}

/**
1017 1018 1019 1020 1021 1022 1023
 * Prepares variables for views RSS item templates.
 *
 * Default template: views-view-row-rss.html.twig.
 *
 * @param array $vars
 *   An associative array containing:
 *   - row: The raw results rows.
merlinofchaos's avatar
merlinofchaos committed
1024 1025
 */
function template_preprocess_views_view_row_rss(&$vars) {
1026
  $item = $vars['row'];
merlinofchaos's avatar
merlinofchaos committed
1027 1028 1029 1030 1031 1032 1033 1034

  $vars['title'] = check_plain($item->title);
  $vars['link'] = check_url($item->link);
  $vars['description'] = check_plain($item->description);
  $vars['item_elements'] = empty($item->elements) ? '' : format_xml_elements($item->elements);
}

/**
1035 1036 1037 1038 1039 1040 1041
 * Prepares variables for views exposed form templates.
 *
 * Default template: views-exposed-form.html.twig.
 *
 * @param array $vars
 *   An associative array containing:
 *   - form: A render element representing the form.
merlinofchaos's avatar
merlinofchaos committed
1042 1043 1044 1045 1046
 */
function template_preprocess_views_exposed_form(&$vars) {
  $form = &$vars['form'];

  // Put all single checkboxes together in the last spot.
1047
  $checkboxes = array();
merlinofchaos's avatar
merlinofchaos committed
1048 1049

  if (!empty($form['q'])) {
1050
    $vars['q'] = $form['q'];
merlinofchaos's avatar
merlinofchaos committed
1051 1052 1053 1054 1055 1056
  }

  $vars['widgets'] = array();
  foreach ($form['#info'] as $id => $info) {
    // Set aside checkboxes.
    if (isset($form[$info['value']]['#type']) && $form[$info['value']]['#type'] == 'checkbox') {
1057
      $checkboxes[] = $form[$info['value']];
merlinofchaos's avatar
merlinofchaos committed
1058 1059
      continue;
    }
1060
    $widget = new stdClass();
merlinofchaos's avatar
merlinofchaos committed
1061
    // set up defaults so that there's always something there.
1062
    $widget->label = $widget->operator = $widget->widget = $widget->description = '';
merlinofchaos's avatar
merlinofchaos committed
1063 1064

    $widget->id = isset($form[$info['value']]['#id']) ? $form[$info['value']]['#id'] : '';
1065

merlinofchaos's avatar
merlinofchaos committed
1066 1067 1068
    if (!empty($info['label'])) {
      $widget->label = check_plain($info['label']);
    }
1069 1070
    if (!empty($info['operator']) && isset($form[$info['operator']])) {
      $widget->operator = $form[$info['operator']];
merlinofchaos's avatar
merlinofchaos committed
1071
    }
1072

1073
    $widget->widget = $form[$info['value']];
1074 1075 1076 1077 1078

    if (!empty($info['description'])) {
      $widget->description = check_plain($info['description']);
    }

merlinofchaos's avatar
merlinofchaos committed
1079
    $vars['widgets'][$id] = $widget;
1080 1081
    // Unset the widget, so that it doesn't get rendered twice.
    unset($form[$info['value']]);
merlinofchaos's avatar
merlinofchaos committed
1082 1083 1084
  }

  // Wrap up all the checkboxes we set aside into a widget.
1085
  if (!empty($checkboxes)) {
1086
    $widget = new stdClass();
merlinofchaos's avatar
merlinofchaos committed
1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113
    // set up defaults so that there's always something there.
    $widget->label = $widget->operator = $widget->widget = NULL;
    $widget->id = 'checkboxes';
    $widget->widget = $checkboxes;
    $vars['widgets']['checkboxes'] = $widget;
  }
}

/**
 * Theme function for a View with form elements: replace the placeholders.
 */
function theme_views_form_views_form($variables) {
  $form = $variables['form'];

  // Placeholders and their substitutions (usually rendered form elements).
  $search = array();
  $replace = array();

  // Add in substitutions provided by the form.
  foreach ($form['#substitutions']['#value'] as $substitution) {
    $field_name = $substitution['field_name'];
    $row_id = $substitution['row_id'];

    $search[] = $substitution['placeholder'];
    $replace[] = isset($form[$field_name][$row_id]) ? drupal_render($form[$field_name][$row_id]) : '';
  }
  // Add in substitutions from hook_views_form_substitutions().
1114
  $substitutions = Drupal::moduleHandler()->invokeAll('views_form_substitutions');
merlinofchaos's avatar
merlinofchaos committed
1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133
  foreach ($substitutions as $placeholder => $substitution) {
    $search[] = $placeholder;
    $replace[] = $substitution;
  }

  // Apply substitutions to the rendered output.
  $form['output']['#markup'] = str_replace($search, $replace, $form['output']['#markup']);

  // Render and add remaining form fields.
  return drupal_render_children($form);
}

function theme_views_mini_pager($vars) {
  global $pager_page_array, $pager_total;

  $tags = $vars['tags'];
  $element = $vars['element'];
  $parameters = $vars['parameters'];

1134
  // Current is the page we are currently paged to.
merlinofchaos's avatar
merlinofchaos committed
1135 1136 1137
  $pager_current = $pager_page_array[$element] + 1;
  // End of marker calculations.

1138 1139 1140 1141 1142
  $li_previous = array();
  if ($pager_total[$element] > 1 && $pager_page_array[$element] > 0) {
    $li_previous = array(
      '#theme' => 'pager_link__previous',
      '#text' => (isset($tags[1]) ? $tags[1] : t('‹‹')),
1143
      '#attributes' => array('title' => t('Go to previous page')),
1144 1145 1146 1147
      '#page_new' => pager_load_array($pager_page_array[$element] - 1, $element, $pager_page_array),
      '#element' => $element,
      '#interval' => 1,
      '#parameters' => $parameters,
merlinofchaos's avatar
merlinofchaos committed
1148
    );
1149
  }
merlinofchaos's avatar
merlinofchaos committed
1150

1151 1152 1153 1154 1155
  $li_next = array();
  if ($pager_page_array[$element] < ($pager_total[$element] - 1)) {
    $li_next = array(
      '#theme' => 'pager_link__next',
      '#text' => (isset($tags[3]) ? $tags[3] : t('››')),
1156
      '#attributes' => array('title' => t('Go to next page')),
1157 1158 1159 1160
      '#page_new' => pager_load_array($pager_page_array[$element] + 1, $element, $pager_page_array),
      '#element' => $element,
      '#interval' => 1,
      '#parameters' => $parameters,
merlinofchaos's avatar
merlinofchaos committed
1161 1162
    );
  }
1163 1164 1165 1166 1167 1168 1169

  $items[] = array(
    '#wrapper_attributes' => array('class' => array('pager-previous')),
  ) + $li_previous;

  $items[] = array(
    '#wrapper_attributes' => array('class' => array('pager-current')),
1170
    '#markup' => t('Page @current', array('@current' => $pager_current)),
1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184
  );

  $items[] = array(
    '#wrapper_attributes' => array('class' => array('pager-next')),
  ) + $li_next;

  return theme('item_list',
    array(
      'items' => $items,
      'title' => NULL,
      'type' => 'ul',
      'attributes' => array('class' => array('pager')),
    )
  );
merlinofchaos's avatar
merlinofchaos committed
1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201
}

/**
 * @defgroup views_templates Views template files
 * @{
 * All views templates can be overridden with a variety of names, using
 * the view, the display ID of the view, the display type of the view,
 * or some combination thereof.
 *
 * For each view, there will be a minimum of two templates used. The first
 * is used for all views: views-view.tpl.php.
 *
 * The second template is determined by the style selected for the view. Note
 * that certain aspects of the view can also change which style is used; for
 * example, arguments which provide a summary view might change the style to
 * one of the special summary styles.
 *