Problem/Motivation
I encountered this while creating demos for the module, with a view that renders the field multiple times.
When one office_hours field is rendered by more than one formatter in the same request, the formatters after the first can show wrong rows: days merged, time slots repeated, open days shown as Closed. It happens when a formatter uses Replace weekday time slots with exception dates and an exception date falls in the coming seven days, wherever one entity's field is formatted twice: a view that adds the field twice with different formatters, a view listing the same entity in several rows, a block plus the node display, Layout Builder.
OfficeHoursItemListFormatter::getRows() clones the item list before changing it, but:
- with Replace weekday time slots,
addOfficeHours()puts the items of the sorted list into the rows as they are. Those are the items of the field itself, andgroupDays()/compressSlots()then change them; getRows()replaces$this->sortedListwith a sorter over its cloned, grouped items and leaves it there. The formatter object is kept on the item list, so the next call reads the changed items.
Follow-up to #3629915: In formatter, weekdays are not always replaced by Exceptions.
Steps to reproduce
- Add an office_hours field to a content type, with Exceptions enabled in the widget settings.
- Create a node with Monday–Friday 09:00–17:00, and a closed exception two days from today, with a comment (e.g. "Closed for inventory").
- Create a view of that content type, showing fields, and add the hours field twice:
- formatter Table, Group consecutive days on;
- the default formatter, Replace weekday time slots with exception dates on.
- The second one reads:
Monday - Friday: 9:00 am - 5:00 pm, 9:00 am - 5:00 pm, 9:00 am - 5:00 pm, 9:00 am - 5:00 pm, 9:00 am - 5:00 pm Tuesday: Closed Wednesday: Closed for inventory Thursday: Closed Friday: Closed
Rendered on its own, it shows Monday, Tuesday, Thursday and Friday 9:00 am - 5:00 pm and Wednesday closed.
Proposed resolution
Copy the items that replace a weekday, and restore the sorted list of the field at the end of getRows(). Both are needed: either one alone leaves at least one of the new tests failing.
New kernel test OfficeHoursRepeatedRowsKernelTest: formats the same field with replace on and then grouped, and with replace on twice, and compares the rows with a field formatted once. On 8.x-1.x (b53e3de) both tests fail; with the change they pass, as do the existing Kernel and Unit tests.
Remaining tasks
Review the merge request.
User interface changes
None.
API changes
None.
Data model changes
None.
AI disclosure
AI was used to diagnose and fix the issue.
| Comment | File | Size | Author |
|---|---|---|---|
| #6 | office_hours_3630495_render_twice.patch | 6.99 KB | johnv |
Issue fork office_hours-3630495
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
Comment #3
mortona2k commentedComment #4
johnvComment #5
johnvI see the sortedlist is first rescued, then set back, in your patch.
I also see the following code around line 160:
Apparently,
I assume a copy is made, here, not reusing a singleton. If using a singleton, we might remove the singleton, at cost of some reprocessing.
Please test if removing the following also results in better rendering:
It should be replaced with comments that we explicitely do not want to reuse a cache, for later maintenance.
Comment #6
johnvCopilot gives another patch, by moving existing code. Please review that.
Comment #8
johnvI committed the last patch.
Comment #11
johnv