Problem/Motivation

When displaying a node with empty exceptiondays/seasonal days, the following message appears:

Warning: Undefined array key 0 in Drupal\office_hours\Plugin\Field\FieldType\OfficeHoursItemList->addOfficeHours() (line 229 of modules\contrib_jvo\office_hours\src\OfficeHoursFormatterTrait.php).

Steps to reproduce

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

CommentFileSizeAuthor
#3 office_hours-3439370.patch699 bytessarwan_verma

Comments

johnv created an issue. See original summary.

sarwan_verma’s picture

Assigned: Unassigned » sarwan_verma
sarwan_verma’s picture

Assigned: sarwan_verma » Unassigned
Status: Active » Needs review
StatusFileSize
new699 bytes

Hi @johnv,
I have fixed this issue "Warning: Undefined array key 0 in OfficeHoursItemList->addOfficeHours()" and also attached patch,

kindly review and verify.

johnv’s picture

Wow, you are fast :-)
Thanks!

I propose the following shorter code, using the new PHP8 '??' operator.

@@ -226,7 +227,7 @@ trait OfficeHoursFormatterTrait {
       // Get date of $day for the exception day lookup.
       $date = strtotime($day - 1 . ' day ' . $week . ' week midnight');
       if (isset($sorted_list[$date])) {
-        $exception_day = $sorted_list[$date][0]->day;
+        $exception_day = $sorted_list[$date][0]->day ?? NULL;
         if ($replace_exceptions || $day !== $exception_day) {
           // Add day data per day.
           // Only replace exceptions, not normal weekday,
johnv’s picture

Status: Needs review » Fixed

In the end, that piece of code contained more obsolete code lines. So I added the committec patch.

  • johnv committed d68af9f3 on 8.x-1.x
    Issue #3439370 by sarwan_verma, johnv: Warning: Undefined array key 0 in...

  • johnv committed d1f0dc9e on 8.x-1.x
    Issue #3439370: Warning: Undefined array key 0 in OfficeHoursItemList->...

Status: Fixed » Closed (fixed)

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