function template_preprocess_views_view_fields in Drupal 9
Same name and namespace in other branches
- 8 core/modules/views/views.theme.inc \template_preprocess_views_view_fields()
Prepares variables for views fields templates.
Default template: views-view-fields.html.twig.
Parameters
array $variables: An associative array containing:
- view: The view object.
- options: An array of options. Each option contains:
- inline: An array that contains the fields that are to be displayed inline.
- default_field_elements: If default field wrapper elements are to be provided.
- hide_empty: Whether the field is to be hidden if empty.
- element_default_classes: If the default classes are to be added.
- separator: A string to be placed between inline fields to keep them visually distinct.
- row: An array containing information about the current row.
File
- core/
modules/ views/ views.theme.inc, line 98 - Preprocessors and helper functions to make theming easier.
Code
function template_preprocess_views_view_fields(&$variables) {
$view = $variables['view'];
// Loop through the fields for this view.
$previous_inline = FALSE;
// Ensure it's at least an empty array.
$variables['fields'] = [];
/** @var \Drupal\views\ResultRow $row */
$row = $variables['row'];
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($row->index, $id);
$empty = $field
->isValueEmpty($field_output, $field->options['empty_zero']);
if (empty($field->options['exclude']) && (!$empty || empty($field->options['hide_empty']) && empty($variables['options']['hide_empty']))) {
$object = new stdClass();
$object->handler = $view->field[$id];
$object->inline = !empty($variables['options']['inline'][$id]);
// Set up default value of the flag that indicates whether to display a
// colon after the label.
$object->has_label_colon = FALSE;
$object->element_type = $object->handler
->elementType(TRUE, !$variables['options']['default_field_elements'], $object->inline);
if ($object->element_type) {
$attributes = [];
if ($object->handler->options['element_default_classes']) {
$attributes['class'][] = 'field-content';
}
if ($classes = $object->handler
->elementClasses($row->index)) {
$attributes['class'][] = $classes;
}
$object->element_attributes = new Attribute($attributes);
}
$object->content = $field_output;
if (isset($view->field[$id]->field_alias) && isset($row->{$view->field[$id]->field_alias})) {
$object->raw = $row->{$view->field[$id]->field_alias};
}
else {
// Make sure it exists to reduce NOTICE.
$object->raw = NULL;
}
if (!empty($variables['options']['separator']) && $previous_inline && $object->inline && $object->content) {
$object->separator = [
'#markup' => $variables['options']['separator'],
];
}
$object->class = Html::cleanCssIdentifier($id);
$previous_inline = $object->inline;
// Set up field wrapper element.
$object->wrapper_element = $object->handler
->elementWrapperType(TRUE, TRUE);
if ($object->wrapper_element === '' && $variables['options']['default_field_elements']) {
$object->wrapper_element = $object->inline ? 'span' : 'div';
}
// Set up field wrapper attributes if field wrapper was set.
if ($object->wrapper_element) {
$attributes = [];
if ($object->handler->options['element_default_classes']) {
$attributes['class'][] = 'views-field';
$attributes['class'][] = 'views-field-' . $object->class;
}
if ($classes = $object->handler
->elementWrapperClasses($row->index)) {
$attributes['class'][] = $classes;
}
$object->wrapper_attributes = new Attribute($attributes);
}
// Set up field label
$object->label = $view->field[$id]
->label();
// Set up field label wrapper and its attributes.
if ($object->label) {
// Add a colon in a label suffix.
if ($object->handler->options['element_label_colon']) {
$object->label_suffix = ': ';
$object->has_label_colon = TRUE;
}
// Set up label HTML element.
$object->label_element = $object->handler
->elementLabelType(TRUE, !$variables['options']['default_field_elements']);
// Set up label attributes.
if ($object->label_element) {
$attributes = [];
if ($object->handler->options['element_default_classes']) {
$attributes['class'][] = 'views-label';
$attributes['class'][] = 'views-label-' . $object->class;
}
// Set up field label.
$element_label_class = $object->handler
->elementLabelClasses($row->index);
if ($element_label_class) {
$attributes['class'][] = $element_label_class;
}
$object->label_attributes = new Attribute($attributes);
}
}
$variables['fields'][$id] = $object;
}
}
}