function template_preprocess_views_view_fields

Prepares variables for views fields templates.

Default template: views-view-fields.html.twig.

Parameters

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.

File

drupal/core/modules/views/views.theme.inc, line 203
Preprocessors and helper functions to make theming easier.

Code

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
      ->getField($view->row_index, $id);
    $empty = $field
      ->isValueEmpty($field_output, $field->options['empty_zero']);
    if (empty($field->options['exclude']) && (!$empty || empty($field->options['hide_empty']) && empty($vars['options']['hide_empty']))) {
      $object = new stdClass();
      $object->handler =& $view->field[$id];
      $object->inline = !empty($vars['options']['inline'][$id]);
      $object->element_type = $object->handler
        ->elementType(TRUE, !$vars['options']['default_field_elements'], $object->inline);
      if ($object->element_type) {
        $attributes = array();
        if ($object->handler->options['element_default_classes']) {
          $attributes['class'][] = 'field-content';
        }
        if ($classes = $object->handler
          ->elementClasses($view->row_index)) {
          $attributes['class'][] = $classes;
        }
        $attributes = new Attribute($attributes);
        $pre = '<' . $object->element_type;
        $pre .= $attributes;
        $field_output = $pre . '>' . $field_output . '</' . $object->element_type . '>';
      }

      // Protect ourself somewhat for backward compatibility. This will prevent
      // old templates from producing invalid HTML when no element type is selected.
      if (empty($object->element_type)) {
        $object->element_type = 'span';
      }
      $object->content = $field_output;
      if (isset($view->field[$id]->field_alias) && isset($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
        ->elementWrapperType(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) {
        $attributes = array();
        if ($object->handler->options['element_default_classes']) {
          $attributes['class'][] = 'views-field';
          $attributes['class'][] = 'views-field-' . $object->class;
        }
        if ($classes = $object->handler
          ->elementWrapperClasses($view->row_index)) {
          $attributes['class'][] = $classes;
        }
        $attributes = new Attribute($attributes);
        $object->wrapper_prefix = '<' . $object->inline_html;
        $object->wrapper_prefix .= $attributes;
        $object->wrapper_prefix .= '>';
        $object->wrapper_suffix = '</' . $object->inline_html . '>';
      }

      // Set up the label for the value and the HTML to make it easier
      // on the template.
      $object->label = check_plain($view->field[$id]
        ->label());
      $object->label_html = '';
      if ($object->label) {
        $object->label_html .= $object->label;
        if ($object->handler->options['element_label_colon']) {
          $object->label_html .= ': ';
        }
        $object->elementLabelType = $object->handler
          ->elementLabelType(TRUE, !$vars['options']['default_field_elements']);
        if ($object->elementLabelType) {
          $attributes = array();
          if ($object->handler->options['element_default_classes']) {
            $attributes['class'][] = 'views-label';
            $attributes['class'][] = 'views-label-' . $object->class;
          }
          $element_label_class = $object->handler
            ->elementLabelClasses($view->row_index);
          if ($element_label_class) {
            $attributes['class'][] = $element_label_class;
          }
          $attributes = new Attribute($attributes);
          $pre = '<' . $object->elementLabelType;
          $pre .= $attributes;
          $pre .= '>';
          $object->label_html = $pre . $object->label_html . '</' . $object->elementLabelType . '>';
        }
      }
      $vars['fields'][$id] = $object;
    }
  }
}