- Issue created by @rondog469
- π³π±Netherlands johnv
I guess thisis the same request as β¨ Season Formatter: update weekly timetable depending on the season Closed: duplicate , 'replace weekdays with exception and season hours'.
Indeed, this needs to be implemented. - πΊπΈUnited States rondog469
Yes that sounds similar. I ended up doing a hook_preprocess_office_hours and modified the twig template. Basically the gist was in the preprocess, check if an exception exists. If it does, find which day the exception is for and add an exception property to that day of the weeks render array. In the twig file I check if that property exists and if it does display that instead of the normal time slot
{% if item.slots['#markup'] is not empty %} <span class="office-hours__item-slots"> {% if item.exception %} {{ item.exception.slots }} {% else %} {{ item.slots }} {% endif %} </span> {% endif %}
- π³π±Netherlands johnv
Can you share the hook_preprocess_office_hours code? for the day calculation.
I plan to put all changes in the Formatter code, adding a setting.
SO you bot want to change the current weekday, AND keep the exception day below the weekdays?
- πΊπΈUnited States rondog469
Sure, but as I said before I dont think this is super flexible for all cases. I wrote this for our specific needs. It could be a good starting point though.
/** * Implements hook_preprocess_office_hours(). * * A normal week with no exceptions is 7 items long, Sunday-Saturday. * If there is an exception this array will be longer. Let's figure out * which day of the week needs to be overridden. See template for integration. * * @TODO Array length might not be reliable especially if a day of the week is * consistently closed. Perhaps search for the exception label instead. */ function HOOK_preprocess_office_hours(&$vars) { $hourLength = count($vars['office_hours']); $keys = array_keys($vars['office_hours']); if ($hourLength > 7) { for ($i = 8; $i < $hourLength; $i++) { $key = $keys[$i]; $dayOverrideKey = date('w', $vars['office_hours'][$key]['day']); $vars['items'][$dayOverrideKey]['exception'] = $vars['items'][$i]; } } }
That code outputs something like this. You can see I have an exception for Tuesday july 4th
^ array:9 [βΌ 0 => array:4 [βΌ "label" => "Sunday: " "slots" => array:2 [βΆ] "comments" => array:2 [βΆ] "suffix" => "<br />" ] 1 => array:4 [βΌ "label" => "Monday: " "slots" => array:2 [βΆ] "comments" => array:2 [βΆ] "suffix" => "<br />" ] 2 => array:5 [βΌ "label" => "Tuesday: " "slots" => array:2 [βΆ] "comments" => array:2 [βΆ] "suffix" => "<br />" "exception" => array:4 [βΌ "label" => "Tuesday, July 4, 2023: " "slots" => array:2 [βΌ "#type" => "markup" "#markup" => "10:00 am-3:00 pm" ] "comments" => array:2 [βΌ "#type" => "markup" "#markup" => "Closing early for July 4th" ] "suffix" => "<br />" ] ] 3 => array:4 [βΌ "label" => "Wednesday: " "slots" => array:2 [βΆ] "comments" => array:2 [βΆ] "suffix" => "<br />" ] 4 => array:4 [βΌ "label" => "Thursday: " "slots" => array:2 [βΆ] "comments" => array:2 [βΆ] "suffix" => "<br />" ] 5 => array:4 [βΌ "label" => "Friday: " "slots" => array:2 [βΆ] "comments" => array:2 [βΆ] "suffix" => "<br />" ] 6 => array:4 [βΌ "label" => "Saturday: " "slots" => array:2 [βΆ] "comments" => array:2 [βΆ] "suffix" => "<br />" ] 7 => array:4 [βΌ "label" => "Exception hours" "slots" => array:2 [βΆ] "comments" => array:2 [βΆ] "suffix" => "<br />" ] 8 => array:4 [βΌ "label" => "Tuesday, July 4, 2023: " "slots" => array:2 [βΆ] "comments" => array:2 [βΌ "#type" => "markup" "#markup" => "Closing early for July 4th" ] "suffix" => "<br />" ] ]
Here is our modified twig template. Aside from showing the exception hours I also change the current day label to read as TODAY
{% set classes = "office-hours office-hours-status--" ~ (is_open ? "open" : "closed") %} <div{{ attributes.addClass(classes) }} id="{{ id }}"> {% set today = 'now'|date('w') %} {% for key, item in items %} {% set currentClass = '' %} {% if today == key %} {% set currentClass = 'current' %} {% endif %} <div class="office-hours__item {{ currentClass }}"> {% if item.label %} <span class="office-hours__item-label" style="width: {{ label_length * 0.60 }}em;"> {% if today == key %} Today: {% else %} {{ item.label }} {% endif %} </span> {% endif %} {% if item.slots['#markup'] is not empty %} <span class="office-hours__item-slots"> {% if item.exception %} {{ item.exception.slots }} {% else %} {{ item.slots }} {% endif %} </span> {% endif %} {% if item.comments['#markup'] is not empty %} <span class="office-hours__item-comments">{{ item.comments }}</span> {% endif %} {% if item.exception.comments['#markup'] is not empty %} <span class="office-hours__item-comments">{{ item.exception.comments }}</span> {% endif %} <span>{{ item_separator | raw }}</span> </div> {% endfor %} </div>
- π³π±Netherlands johnv
FYI, π Add Exception Day support for 'Current day' formatter Fixed is now committed.
Adding it to the complete formatter was a bridge too far, atm.
All was handled in php, not theming/twig. - Status changed to Needs work
about 1 year ago 2:03pm 3 November 2023 - First commit to issue fork.
- π¦πΊAustralia geoffreyr
@johnv Thank you for your patch! I've set up an issue fork at https://git.drupalcode.org/issue/office_hours-3370722/-/tree/3370722-rep... that applies the patch, fixes some typing issues I found, and adjusts some of the iterator logic as it wasn't replacing the first default slot of days with an exception.
My use case is displaying today's opening hours for a list of venues, so it's pretty narrow compared to the needs of everyone that uses this module. I reckon that we'll want to make replacements optional so it applies to some formatters and not others. Either way, we'll need some more eyes on it to see if the behaviour that we're seeing is what we want. Also, tests. I need to write some tests.
- π³π±Netherlands johnv
Indeed, this feature might be optional.
for your use case, "displaying today's opening hours for a list of venues", you might check the formatter setting to display 'current' or 'next' open day. That already contains this functionality since it queries getCurrent().
- π¦πΊAustralia geoffreyr
Yes, the View that I'm using this on is set to display only the current day.
I've just pushed an update that adds a replace_exceptions key in exceptions config that determines whether to run the exceptions replacement logic. Hopefully this helps, let me know if there's anything else you'd like me to take a look at.
- π³π±Netherlands johnv
On another note: I see you made a correction here: ' week midnight UTC'.
would you be so kind to grep the source code for 'midnight', and check if there are more errors?
Obviously, you are in another time zone then I am. Not sure how to handle it. - π¦πΊAustralia geoffreyr
I've checked your patch and there aren't any further instances that need updating.
The reason that I needed to add UTC string to the 1st param of DrupalDateTime constructor is because I found that the dates used to index
$exceptions
wouldn't match those for the DrupalDateTime unless I specifically added it; there was always an offset equivalent to the difference between my timezone and UTC. - π³π±Netherlands johnv
I mean other instances in the office_hours module, not in the patch.
So, I can test other midnights by just changing the time zone in my user profile, or the site settings? - First commit to issue fork.
- π³π±Netherlands johnv
@larowlan, I see you are working on this, thanks.
ITMT, please review$sorted_days = $this->sortedList->getSortedItemList($today);
This is new code, needed for the proper support of seasons and exceptions.
I think it can be used for this purpose, too.
You only need to select the proper days for the current weekd display, and rename the date to the weekday in the key. - π¦πΊAustralia larowlan π¦πΊπ.au GMT+10
Full disclaimer I'm working on a project with geoffreyr and just rerolled his MR because it no longer applied
- π³π±Netherlands johnv
Sorry for pulling the ground under your feet.
But you now have a π Better OfficeHoursItemListSorter.php Fixed .
This feature must use that prepared, sorted list, keyed with date. - Status changed to Fixed
10 months ago 7:50am 28 February 2024 - π³π±Netherlands johnv
Please check latest dev version. Thanks a lot for you contributions.
Automatically closed - issue fixed for 2 weeks with no activity.