Skip to content

Commit cbed83d

Browse files
authored
Merge pull request #20 from psu-libraries/19-views-accordion-not-displaying-row-classes-and-are-showing-hidden-fields
19 views accordion not displaying row classes and are showing hidden fields
2 parents f51bd9b + cd870cb commit cbed83d

File tree

4 files changed

+82
-49
lines changed

4 files changed

+82
-49
lines changed

psulib_base_helper.module

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,16 @@ function psulib_base_helper_theme($existing, $type, $theme, $path): array {
2525
'options' => [],
2626
],
2727
],
28-
'views_psulib_base_helper_accordion_content_item' => [
28+
'views_view_fields__accordion_content' => [
29+
'base hook' => 'views_view_fields',
30+
'template' => 'views-view-fields--accordion-content',
31+
2932
'variables' => [
30-
'label' => '',
33+
'view' => NULL,
34+
'field' => NULL,
35+
'row' => NULL,
36+
'title_field' => 'title',
3137
'options' => [],
32-
'content' => [],
3338
],
3439
],
3540
];
@@ -47,7 +52,7 @@ function psulib_base_helper_preprocess_views_psulib_base_helper_card_grid(&$vari
4752
}
4853

4954
/**
50-
* Prepares variables for views table templates.
55+
* Prepares variables for views accordion template..
5156
*
5257
* Default template: views-view-table.html.twig.
5358
*
@@ -61,24 +66,26 @@ function template_preprocess_views_psulib_base_helper_accordion(&$variables) {
6166
$view = $variables['view'];
6267
$options = $view->style_plugin->options;
6368
$variables['options'] = $options;
69+
$variables['display_class'] = $view->display_handler->getOption('css_class');
6470
$items = [];
6571

72+
$renderer = \Drupal::service('renderer');
73+
6674
foreach ($view->result as $id => $row) {
67-
$item = [];
68-
foreach ($view->field as $field_id => $field) {
69-
if ($field_id == $options['title_field']) {
70-
$item['title'] = $view->style_plugin->getField($id, $field_id);
71-
}
72-
else {
73-
$item['content'][$field_id] = [
74-
'#theme' => 'views_psulib_base_helper_accordion_content_item',
75-
'#label' => $field->label(),
76-
'#options' => $field->options,
77-
'#content' => $view->style_plugin->getField($id, $field_id),
78-
];
79-
}
80-
}
81-
$items[] = $item;
75+
// Build the content array. We're extending the views_view_fields theme
76+
// function so we can remove the title field from the content area.
77+
$content = [
78+
'#theme' => 'views_view_fields__accordion_content',
79+
'#view' => $view,
80+
'#options' => $view->rowPlugin->options,
81+
'#title_field' => $options['title_field'],
82+
'#row' => $row,
83+
];
84+
85+
$items[] = [
86+
'title' => $view->style_plugin->getField($id, $options['title_field']),
87+
'content' => $renderer->render($content),
88+
];
8289
}
8390
$variables['items'] = $items;
8491

templates/views-psulib-base-helper-accordion-content-item.html.twig

Lines changed: 0 additions & 29 deletions
This file was deleted.

templates/views-psulib-base-helper-accordion.html.twig

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,7 @@
2424
title: '',
2525
default_item_tag: options.heading_level ?? 'h3',
2626
open_item_id: options.collapse_initial ? 0 : 1,
27-
items: items ?? []
27+
items: items ?? [],
28+
accordion_item_utility_classes: [options.row_class ?? ''],
29+
accordion_utility_classes: [display_class ?? ''],
2830
} %}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
{#
2+
/**
3+
* @file
4+
* Theme override to display all the fields in a row.
5+
*
6+
* Available variables:
7+
* - view: The view in use.
8+
* - fields: A list of fields, each one contains:
9+
* - content: The output of the field.
10+
* - raw: The raw data for the field, if it exists. This is NOT output safe.
11+
* - class: The safe class ID to use.
12+
* - handler: The Views field handler controlling this field.
13+
* - inline: Whether or not the field should be inline.
14+
* - wrapper_element: An HTML element for a wrapper.
15+
* - wrapper_attributes: List of attributes for wrapper element.
16+
* - separator: An optional separator that may appear before a field.
17+
* - label: The field's label text.
18+
* - label_element: An HTML element for a label wrapper.
19+
* - label_attributes: List of attributes for label wrapper.
20+
* - label_suffix: Colon after the label.
21+
* - element_type: An HTML element for the field content.
22+
* - element_attributes: List of attributes for HTML element for field content.
23+
* - has_label_colon: A boolean indicating whether to display a colon after
24+
* the label.
25+
* - element_type: An HTML element for the field content.
26+
* - element_attributes: List of attributes for HTML element for field content.
27+
* - row: The raw result from the query, with all data it fetched.
28+
*
29+
* @see template_preprocess_views_view_fields()
30+
*/
31+
#}
32+
33+
{% for id, field in fields| filter((v, k) => k != title_field) -%}
34+
{{ field.separator }}
35+
{%- if field.wrapper_element -%}
36+
<{{ field.wrapper_element }}{{ field.wrapper_attributes }}>
37+
{%- endif %}
38+
{%- if field.label -%}
39+
{%- if field.label_element -%}
40+
<{{ field.label_element }}{{ field.label_attributes }}>{{ field.label }}{{ field.label_suffix }}</{{ field.label_element }}>
41+
{%- else -%}
42+
{{ field.label }}{{ field.label_suffix }}
43+
{%- endif %}
44+
{%- endif %}
45+
{%- if field.element_type -%}
46+
<{{ field.element_type }}{{ field.element_attributes }}>{{ field.content }}</{{ field.element_type }}>
47+
{%- else -%}
48+
{{ field.content }}
49+
{%- endif %}
50+
{%- if field.wrapper_element -%}
51+
</{{ field.wrapper_element }}>
52+
{%- endif %}
53+
{%- endfor %}

0 commit comments

Comments
 (0)