Skip to content

Commit 6560463

Browse files
committed
4.2.0 - getGroupedRanges()
1 parent 11d7a67 commit 6560463

File tree

3 files changed

+58
-1
lines changed

3 files changed

+58
-1
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Release Notes for Store Hours
22

3+
## 4.2.0 - 2024-07-28
4+
- Added `craft\storehours\data\FieldData::getGroupedRanges()`.
5+
36
## 4.1.0 - 2024-07-28
47
- Added the “Start Day” field setting. ([#43](https://github.com/craftcms/store-hours/issues/43))
58
- Added `craft\storehours\data\FieldData::getIsOpen()`. ([#37](https://github.com/craftcms/store-hours/issues/37))

README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,36 @@ To only show certain days of the week (e.g only Monday through Friday), use the
115115
{% endfor %}
116116
```
117117

118+
### Showing Grouped Ranges
119+
120+
You can show consolidated list of hours, such as:
121+
122+
> **11am** – **6pm** Mon – Fri
123+
> **11am** – **5pm** Sat – Sun
124+
125+
To do that, use the `getGroupedRanges()` field method, which is similar to `getRange()`, except the resulting days are grouped by their time slots.
126+
127+
```twig
128+
<ul>
129+
{% for group in entry.<FieldHandle>.getGroupedRanges(1) %}
130+
{% set first = group|first %}
131+
{% set last = group|last %}
132+
{% if first.open and first.close %}
133+
<li>
134+
<strong>
135+
{{ first.open|date('g:sa')|replace(':00', '') }}&hairsp;–&hairsp;{{ first.close|date('g:sa')|replace(':00', '') }}
136+
</strong>
137+
{% if first != last %}
138+
{{ first.getName('medium') }}&hairsp;–&hairsp;{{ last.getName('medium') }}
139+
{% else %}
140+
{{ first.getName('medium') }}
141+
{% endif %}
142+
{% endif %}
143+
</li>
144+
{% endfor %}
145+
</ul>
146+
```
147+
118148
### Changing the Week Start Day
119149

120150
You can use the `getRange()` field method to return the full list of days with a different week start day. For example, if you want Monday to be the first day of the week, do this:

src/data/FieldData.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,31 @@ public function getRange(int $start, int $end = null): array
198198
array_slice($data, 0, $end + 1)
199199
);
200200
}
201-
201+
202+
/**
203+
* Returns a range of the days, grouped by consecutive days that have the same time slot values.
204+
*
205+
* @param int $start The first day to return
206+
* @param int|null $end The last day to return. If null, it will be whatever day comes before `$start`.
207+
* @return DayData[][]
208+
*/
209+
public function getGroupedRanges(int $start, int $end = null): array
210+
{
211+
$range = $this->getRange($start, $end);
212+
$groups = [];
213+
$lastSlotKey = null;
214+
foreach ($range as $day) {
215+
/** @phpstan-ignore-next-line */
216+
$slotKey = implode('-', array_map(fn(?DateTime $slot) => $slot ? $this->minuteOfDay($slot) : 'null', (array)$day));
217+
if ($slotKey !== $lastSlotKey) {
218+
$groups[] = [];
219+
$lastSlotKey = $slotKey;
220+
}
221+
$groups[count($groups) - 1][] = $day;
222+
}
223+
return $groups;
224+
}
225+
202226
/**
203227
* Returns whether any day has any time slots filled in.
204228
*

0 commit comments

Comments
 (0)