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, and groupDays()/compressSlots() then change them;
  • getRows() replaces $this->sortedList with 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

  1. Add an office_hours field to a content type, with Exceptions enabled in the widget settings.
  2. 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").
  3. Create a view of that content type, showing fields, and add the hours field twice:
    1. formatter Table, Group consecutive days on;
    2. the default formatter, Replace weekday time slots with exception dates on.
  4. 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.

CommentFileSizeAuthor
#6 office_hours_3630495_render_twice.patch6.99 KBjohnv
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

Status: Active » Needs review
johnv’s picture

Issue summary: View changes
johnv’s picture

Status: Needs review » Needs work

I see the sortedlist is first rescued, then set back, in your patch.

I also see the following code around line 160:

    // Remove excessive exception days.
    $horizon = $settings['exceptions']['restrict_exceptions_to_num_days'];
    $this->keepExceptionDaysInHorizon($items, $horizon);
    // Refresh SortedList, now that items may have been removed.
    $this->sortedList = new OfficeHoursItemListSorter($items);

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.

public function getSortedItemList(int $start_time, ?int $horizon = 14): array {

    // Read the cache.
    if (isset($this->sortedItemList[$start_time])) {
      return $this->sortedItemList[$start_time];  // DELETE THIS LINE.
    }
johnv’s picture

StatusFileSize
new6.99 KB

Copilot gives another patch, by moving existing code. Please review that.

  • johnv committed 06f122b7 on 8.x-1.x
    Issue #3630495: Formatter bug when rendered twice on page
    
johnv’s picture

Version: 8.x-1.x-dev » 8.x-1.29
Status: Needs work » Fixed

I committed the last patch.

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.

johnv’s picture