Skip to content

Commit 85399a9

Browse files
committed
refactor: expandable-list
1 parent c351823 commit 85399a9

2 files changed

Lines changed: 93 additions & 122 deletions

File tree

Modularity/source/php/Module/Posts/TemplateController/ExpandableListTemplate.php

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -54,24 +54,47 @@ public function __construct(\Modularity\Module\Posts\Posts $module)
5454
$this->data = $module->data;
5555
$this->fields = $module->fields;
5656

57-
$this->data['posts_list_column_titles'] = !empty($this->fields['posts_list_column_titles'])
58-
&& is_array($this->fields['posts_list_column_titles'])
59-
? $this->fields['posts_list_column_titles']
60-
: null;
57+
$this->data['accordionHeadings'] = $this->getAccordionHeadings();
6158

6259
$this->data['posts_hide_title_column'] = $this->fields['posts_hide_title_column'] ? true : false;
6360
$this->data['title_column_label'] = $this->fields['title_column_label'] ?? null;
6461
$this->data['allow_freetext_filtering'] = $this->fields['allow_freetext_filtering'] ?? null;
6562
$this->data['prepareAccordion'] = $this->prepareExpandableList();
6663
}
6764

65+
private function getTitleColumnLabel(): string
66+
{
67+
return $this->fields['title_column_label'] ?? __('Title', 'modularity');
68+
}
69+
70+
private function getAccordionHeadings(): array
71+
{
72+
$headings = [];
73+
74+
if (empty($this->fields['posts_hide_title_column'])) {
75+
$headings[] = $this->getTitleColumnLabel();
76+
}
77+
78+
if (!empty($this->fields['posts_list_column_titles']) && is_array($this->fields['posts_list_column_titles'])) {
79+
foreach ($this->fields['posts_list_column_titles'] as $column) {
80+
if (empty($column['column_header'])) {
81+
continue;
82+
}
83+
84+
$headings[] = $column['column_header'];
85+
}
86+
}
87+
88+
return $headings;
89+
}
90+
6891
/**
6992
* Get correct column values
7093
* @return array An array of column values for a column
7194
*/
7295
public function getColumnValues(): array
7396
{
74-
if (empty($this->data['posts_list_column_titles'])) {
97+
if (empty($this->data['accordionHeadings'])) {
7598
return [];
7699
}
77100

@@ -120,6 +143,11 @@ private function rewritePostContent($preparedPosts): array
120143
return $preparedPosts;
121144
}
122145

146+
private function shouldUsePostTitleForHeading(string $title): bool
147+
{
148+
return empty($this->fields['posts_hide_title_column']) && $title === $this->getTitleColumnLabel();
149+
}
150+
123151
/**
124152
* Prepare Data for accordion
125153
* @param array $items Array of posts
@@ -136,14 +164,14 @@ public function prepareExpandableList(): null|array
136164

137165
if (!empty($this->data['posts']) && is_array($this->data['posts'])) {
138166
foreach ($this->data['posts'] as $index => $item) {
139-
if ($this->hasColumnValues($columnValues) && $this->hasColumnTitles($this->data)) {
140-
foreach ($this->data['posts_list_column_titles'] as $colIndex => $column) {
141-
$sanitizedTitle = sanitize_title($column['column_header']);
167+
if ($this->hasColumnValues($columnValues) && $this->hasColumnTitles()) {
168+
foreach ($this->data['accordionHeadings'] as $colIndex => $title) {
169+
$usePostTitleInHeading = $this->shouldUsePostTitleForHeading($title);
170+
$sanitizedTitle = sanitize_title($title);
142171
if ($this->arrayDepth($columnValues) > 1) {
143-
$accordion[$index]['column_values'][$colIndex] =
144-
$columnValues[$index][$sanitizedTitle] ?? '';
172+
$accordion[$index]['headings'][$colIndex] = $usePostTitleInHeading ? $item->getTitle() : $columnValues[$index][$sanitizedTitle] ?? '';
145173
} else {
146-
$accordion[$index]['column_values'][$colIndex] = $columnValues[$sanitizedTitle] ?? '';
174+
$accordion[$index]['headings'][$colIndex] = $usePostTitleInHeading ? $item->getTitle() : $columnValues[$sanitizedTitle] ?? '';
147175
}
148176
}
149177
}
@@ -155,7 +183,7 @@ public function prepareExpandableList(): null|array
155183
}
156184
}
157185

158-
if ($accordion < 0) {
186+
if (empty($accordion)) {
159187
return null;
160188
}
161189

@@ -205,6 +233,6 @@ private function hasColumnValues(array $columnValues): bool
205233
*/
206234
private function hasColumnTitles(): bool
207235
{
208-
return !empty($this->data['posts_list_column_titles']) && is_array($this->data['posts_list_column_titles']);
236+
return !empty($this->data['accordionHeadings']) && is_array($this->data['accordionHeadings']);
209237
}
210238
}
Lines changed: 52 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -1,114 +1,57 @@
11
<div class="o-grid{{ !empty($stretch) ? ' o-grid--stretch' : '' }}">
2-
<div class="o-grid-12">
3-
@card([
4-
'heading' => false,
2+
@card([
3+
'heading' => false,
4+
'attributeList' => [
5+
'js-filter-container' => $ID,
6+
...(!$hideTitle && !empty($postTitle) ? ['aria-labelledby' => 'mod-posts-' . $ID . '-label'] : []),
7+
],
8+
'context' => 'module.posts.expandablelist'
9+
])
10+
@if ((!$hideTitle && !empty($postTitle)) || !empty($titleCTA))
11+
@element([
12+
'classList' => ['c-card__header']
13+
])
14+
@include('partials.post-title', ['variant' => 'h4', 'classList' => [], 'titleCTA' => $titleCTA ?? null])
15+
@endelement
16+
@endif
17+
@if (!isset($allow_freetext_filtering) || $allow_freetext_filtering)
18+
@element([
19+
'classList' => ['c-card__body'],
520
'attributeList' => [
6-
'js-filter-container' => $ID,
7-
...(!$hideTitle && !empty($postTitle) ? ['aria-labelledby' => 'mod-posts-' . $ID . '-label'] : []),
8-
],
9-
'context' => 'module.posts.expandablelist'
21+
'aria-label' => __('Search', 'municipio')
22+
]
1023
])
11-
@if ((!$hideTitle && !empty($postTitle)) || !empty($titleCTA))
12-
<div class="c-card__header">
13-
@include('partials.post-title', ['variant' => 'h4', 'classList' => [], 'titleCTA' => $titleCTA ?? null])
14-
</div>
15-
@endif
16-
<div>
17-
@if (!isset($allow_freetext_filtering) || $allow_freetext_filtering)
18-
<div class="c-card__body" aria-label="{{ __('Search', 'municipio') }}">
19-
@field([
20-
'type' => 'search',
21-
'name' => 'search',
22-
'label' => __('Search', 'municipio'),
23-
'hideLabel' => true,
24-
'attributeList' => [
25-
'js-filter-input' => $ID
26-
],
27-
'placeholder' => __('Search', 'municipio')
28-
])
29-
@endfield
30-
</div>
31-
@endif
32-
33-
@if (isset($posts_list_column_titles) && $posts_list_column_titles)
34-
35-
<header class="accordion-table__head">
36-
37-
@if (!$posts_hide_title_column)
38-
@typography([
39-
'element' => 'span',
40-
'classList' => ['accordion-table__head-column']
41-
])
42-
{{ isset($title_column_label) && !empty($title_column_label) && is_string($title_column_label) ? $title_column_label : __('Title', 'municipio') }}
43-
@endtypography
44-
@endif
45-
46-
@if (is_array($posts_list_column_titles) && !empty($posts_list_column_titles))
47-
@foreach ($posts_list_column_titles as $column)
48-
@typography([
49-
'element' => 'span',
50-
'classList' => ['accordion-table__head-column']
51-
])
52-
{{ $column['column_header'] }}
53-
@endtypography
54-
@endforeach
55-
<span class="accordion-table__head-column-icon"></span>
56-
@endif
57-
</header>
58-
@endif
59-
60-
61-
@if (count($prepareAccordion) > 0)
62-
@accordion([])
63-
@foreach ($prepareAccordion as $accordionItem)
64-
@if (isset($accordionItem['column_values']) &&
65-
is_array($accordionItem['column_values']) &&
66-
!empty($accordionItem['column_values']))
67-
@if ($posts_hide_title_column)
68-
@php $accordionItem['heading'] = []; @endphp
69-
@endif
70-
@accordion__item([
71-
'heading' => $accordionItem['column_values']
72-
? array_merge((array) $accordionItem['heading'], (array) $accordionItem['column_values'])
73-
: $accordionItem['heading'],
74-
'attributeList' => array_merge(
75-
['js-filter-item' => '','js-filter-data' => ''],
76-
$accordionItem['attributeList']
77-
),
78-
'classList' => array_merge(
79-
$accordionItem['classList'],
80-
['c-accordion-table', 'u-clearfix']
81-
)
82-
])
83-
{!! $accordionItem['content'] !!}
84-
@endaccordion__item
85-
@else
86-
@accordion__item([
87-
'heading' => $accordionItem['heading'],
88-
'classList' => $accordionItem['classList'],
89-
'attributeList' => array_merge(
90-
['js-filter-item' => '','js-filter-data' => ''],
91-
$accordionItem['attributeList'])
92-
])
93-
{!! $accordionItem['content'] !!}
94-
@endaccordion__item
95-
@endif
96-
@endforeach
97-
@endaccordion
98-
@else
99-
<section class="accordion-section">
100-
@typography([
101-
'element' => 'p'
102-
])
103-
{{ __('Nothing to display…', 'municipio') }}
104-
@endtypography
105-
</section>
106-
@endif
107-
108-
</div>
109-
110-
@endcard
111-
</div>
112-
</div>
24+
@field([
25+
'type' => 'search',
26+
'name' => 'search',
27+
'label' => __('Search', 'municipio'),
28+
'hideLabel' => true,
29+
'attributeList' => [
30+
'js-filter-input' => $ID
31+
],
32+
'placeholder' => __('Search', 'municipio')
33+
])
34+
@endfield
35+
@endelement
36+
@endif
37+
@accordion([
38+
'heading' => $accordionHeadings ?? []
39+
])
40+
@foreach($prepareAccordion as $accordionItem)
41+
@accordion__item([
42+
'heading' => $accordionItem['headings'],
43+
'attributeList' => array_merge(
44+
[
45+
'js-filter-item' => '',
46+
'js-filter-data' => ''
47+
],
48+
$accordionItem['attributeList']
49+
),
50+
])
51+
{!! $accordionItem['content'] !!}
52+
@endaccordion__item
53+
@endforeach
54+
@endaccordion
55+
@endcard
11356

11457
@include('partials.more')

0 commit comments

Comments
 (0)