Last active
August 24, 2023 10:35
-
-
Save paulsheldrake/e11f35e7f963b4dc373fcb111d44f2cb to your computer and use it in GitHub Desktop.
Field level data for Views rows in Drupal 8.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{# | |
/** | |
* @file | |
* Theme override to display a view of unformatted rows. | |
* | |
* Available variables: | |
* - title: The title of this group of rows. May be empty. | |
* - rows: A list of the view's row items. | |
* - attributes: The row's HTML attributes. | |
* - content: The row's content. | |
* - fields: The individual fields for a row | |
* - view: The view object. | |
* - default_row_class: A flag indicating whether default classes should be | |
* used on rows. | |
* | |
* @see template_preprocess_views_view_unformatted() | |
*/ | |
#} | |
{% if title %} | |
<h3>{{ title }}</h3> | |
{% endif %} | |
{% for row in rows %} | |
{% set parity = cycle(['odd', 'even'], loop.index0) %} | |
{% set row_classes = [ | |
default_row_class ? 'views-row', | |
parity, | |
] | |
%} | |
{% if parity == 'odd' %} | |
<div{{ row.attributes.addClass(row_classes) }}> | |
<div class="content"> | |
{% if row.fields.field_ci_display_title.content %} | |
<h2>{{ row.fields.field_ci_display_title.content }}</h2> | |
{% endif %} | |
{{ row.fields.field_ci_body.content }} | |
</div> | |
<div class="media"> | |
{{ row.fields.field_ci_video.content }} | |
{{ row.fields.field_ci_image.content }} | |
</div> | |
</div> | |
{% elseif parity == 'even' %} | |
<div{{ row.attributes.addClass(row_classes) }}> | |
<div class="media"> | |
{{ row.fields.field_ci_video.content }} | |
{{ row.fields.field_ci_image.content }} | |
</div> | |
<div class="content"> | |
{% if row.fields.field_ci_display_title.content %} | |
<h2>{{ row.fields.field_ci_display_title.content }}</h2> | |
{% endif %} | |
{{ row.fields.field_ci_body.content }} | |
</div> | |
</div> | |
{% endif %} | |
{% endfor %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Implements template_preprocess_views_view_unformatted(). | |
* | |
* Add field data to row level template like D7. | |
* http://www.focal55.com/article/drupal-8-views-style-swagger | |
* | |
* This had a couple of bugs in it from the website so it's not an exact copy. | |
*/ | |
function whatever_preprocess_views_view_unformatted(&$variables) { | |
$view = $variables['view']; | |
$style = $view->style_plugin; | |
$options = $style->options; | |
$variables['default_row_class'] = !empty($options['default_row_class']); | |
foreach ($variables['rows'] as $id => $row_data) { | |
// Loop through the fields for this row. | |
$previous_inline = FALSE; | |
$variables['rows'][$id]['fields'] = array(); // ensure it's at least an empty array. | |
/** @var \Drupal\views\ResultRow $row */ | |
$row = $row_data['content']['#row']; | |
foreach ($view->field as $field_id => $field) { | |
// render this even if set to exclude so it can be used elsewhere. | |
$field_output = $view->style_plugin->getField($row->index, $field_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[$field_id]; | |
$object->inline = !empty($variables['options']['inline'][$field_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 = array(); | |
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 Drupal\Core\Template\Attribute($attributes); | |
} | |
$object->content = $field_output; | |
if (isset($view->field[$field_id]->field_alias) && isset($row->{$view->field[$field_id]->field_alias})) { | |
$object->raw = $row->{$view->field[$field_id]->field_alias}; | |
} | |
else { | |
$object->raw = NULL; // make sure it exists to reduce NOTICE | |
} | |
if (!empty($variables['options']['separator']) && $previous_inline && $object->inline && $object->content) { | |
$object->separator = Drupal\Component\Utility\Xss::filterAdmin($variables['options']['separator']); | |
} | |
$object->class = Drupal\Component\Utility\Html::cleanCssIdentifier($field_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 = array(); | |
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 Drupal\Core\Template\Attribute($attributes); | |
} | |
// Set up field label | |
$object->label = $view->field[$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 = array(); | |
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 Drupal\Core\Template\Attribute($attributes); | |
} | |
} | |
$variables['rows'][$id]['fields'][$field_id] = $object; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment