Problem/Motivation

The formatter shows an empty row: Closed, with no day label. This happens whenever:
- Number of days to show: Show only current day is enabled, and
- Group consecutive days with same hours into one set is enabled, and
- today is grouped with other days and
- today is not the last day of its group.

Each rendering logs these PHP warnings:

Warning: Undefined array key "day" in OfficeHoursItemListFormatter->formatComments()
Warning: Undefined array key "items" in OfficeHoursItemListFormatter->formatComments()
Warning: Undefined array key "label" in OfficeHoursThemeHooks->preprocessOfficeHours()
Deprecated function: mb_strlen(): Passing null to parameter #1 ($string)

With Monday–Friday 9:00–17:00, it is wrong Monday to Thursday and right on Friday. With the same hours every day, it is wrong six days a week.

Function groupDays() keeps a group under the key of its last day, with its first and last day in 'day' and 'endday'.
Function keepCurrentDay() looks today up by its own key:

$result[$day_number] = $office_hours[$day_number] ?? [];
if (!$result) {
  foreach ($office_hours as $key => $info) {
    // Because of grouped days, no direct read possible.

By then $result already holds the empty row, so !$result is never true and the loop meant for grouped days never runs. Even if it ran, the loop could not find a group that runs past Saturday into Sunday ('day' 6, 'endday' 0), as its
@todo says. That happens when the first day of the week is not Sunday.

Related: #3022029: Error in "Show only current day" depending on start of week, an earlier fix of 'Show only current day' depending on the first day of the week.

Steps to reproduce

1. Create an office_hours field with Monday–Friday 09:00–17:00.
2. Set its formatter to Show only current day, with Group consecutive days with same hours on.
3. View it on a Monday, Tuesday, Wednesday or Thursday: it shows Closed with no day, and warnings appear in the log.

With Group consecutive days off, or on a Friday, it shows the day and its hours.

Office Hours show day only when grouped bug

Proposed resolution

Look today up by its key. If it isn't there, return the row of the group that contains it. A new isDayInGroup() does this by counting the days from the first day of the group, so a group running into Sunday is found too. It leaves the
season and exception captions out.

The current day of a group is then shown with the label of the group (Monday - Friday: 9:00-17:00), as Show next open day already does.

A new kernel test, OfficeHoursCurrentDayKernelTest, covers:
- the same hours every day, with each first day of the week from Sunday to Saturday. This puts today at the first day of the group, its last day, in between, and in a group that runs into Sunday;
- a group of closed days around today.

Both tests fail on 8.x-1.x (06f122b). They pass with the change, and so do the existing Kernel and Unit tests.

Remaining tasks

- Review.
- Decide whether the current day should keep its own label (Tuesday) instead of the group's.

AI Disclosure

This came up while generating a demo for #3630716: Office Hours example module.
I validated the issue, but the analysis, recommendations, and patch need thorough review.

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

mortona2k created an issue. See original summary.

mortona2k’s picture

Issue summary: View changes

mortona2k’s picture

Status: Active » Needs review
mortona2k’s picture

Issue summary: View changes
mortona2k’s picture

The fix in place corrects the empty day when today is in a group.

But setting "show next open day" or "show only current day" implies showing only the next day, not the next group.

This screenshot shows the existing, fixed, and alternative displays.

Only local images are allowed.

___

I updated the Group consecutive days description to clarify that it groups items when show current/next day is set, and only the one day when unset.

We may want to expand on this in the future, to be able to show the current day hours in the current status settings, and something else beneath it, like the full schedule with groups.

mortona2k’s picture

Updated setting description:
Office hours current day grouped settings

johnv’s picture

Issue summary: View changes

  • johnv committed 3fd9dc0a on 8.x-1.x
    Issue #3630718: No day label for 'Show only current day' and 'Group...
johnv’s picture

Title: Show only current day shows an empty row when today is grouped with other days » No day label for 'Show only current day' and 'Group consecutive days'
Version: 8.x-1.x-dev » 8.x-1.29
Status: Needs review » Fixed

Thanks. Committed a shorter version.

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

mortona2k’s picture

Claude code review of committed change:

Bug: when today isn't stored under its own key, it returns the first grouped row without checking that today is in it. Both tests pass because each has only one multi-day group.

Test: Tue–Wed 8–12 and Thu–Mon 9–17, week starting Tuesday. On a Thursday it shows "Tuesday - Wednesday: 8:00-12:00".
johnv’s picture

Status: Fixed » Needs work

  • johnv committed eaf51614 on 8.x-1.x
    Issue #3630718: Wrong StatusTitle in FormatterTableSelectList
    

  • johnv committed 4b1b826c on 8.x-1.x
    Issue #3630718: Wrong StatusTitle in FormatterTableSelectList
    
johnv’s picture

Status: Needs work » Fixed

Please try again.
I added cloning of $items, to avoid severl formatters to influence eachother. That was already in place in other locations.

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

mortona2k’s picture

Claude found numerous issues including #12 is not fixed and #16 is not accurate, and there are a handful of code issues.

Hours Week starts Viewed on Shows Should show
Mon–Fri 9–17 (the original report) Monday Sat or Sun Monday - Friday: 9:00-17:00 Saturday - Sunday: Closed
Mon–Fri 9–17 (the original report) Saturday Mon–Thu Saturday - Sunday: Closed Monday - Friday: 9:00-17:00
Tue–Wed 8–12, Thu–Mon 9–17 (comment #12) Tuesday Thursday Tuesday - Wednesday: 8:00-12:00 Thursday - Monday: 9:00-17:00
johnv’s picture

Status: Fixed » Needs work
johnv’s picture

OK, AI sneaked in a requirement that was not in OP: First day of week is not Sunday.

Indeed, I noticed that while investigating the problem, and left it as a to-do.
We need a new ticket for that, since the initial problem is fixed.

  • johnv committed bad5d2c0 on 8.x-1.x
    Issue #3630718: Fix grouped days with firstday of week is not Sunday
    
johnv’s picture

It should now be fixed.

The only problem left seems to: 'Show only open days' when a week or season has no open days. Then, no caption 'Closed' is displayed.

  • johnv committed cafba2f9 on 8.x-1.x
    Issue #3630718: Factor out DayGrouper class
    

  • johnv committed a48cf50a on 8.x-1.x
    Issue #3630718: Factor out OfficeHoursRowsFormatter class
    

  • johnv committed 96d64782 on 8.x-1.x
    Issue #3630718: Unify calls to ListSorter
    
johnv’s picture

Status: Needs work » Fixed

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.