Warning in hook_tokens()

Created on 30 November 2022, about 2 years ago
Updated 10 May 2023, over 1 year ago

Problem/Motivation

As of PHP 8.1 I found when rendering a page that used office hours and Metatag/Schema.org Metatag to add office hours to structured data, I started to see these warnings in logs:

Message Warning: Undefined array key "" in office_hours_tokens() (line 45 of /var/www/html/docroot/modules/contrib/office_hours/office_hours.tokens.inc)

Not all days were set in the office hours on that node but all days were being referred to as tokens in Metatag/Schema.org Metatag, [node:field_opening_hours:0:day], [node:field_opening_hours:1:day], [node:field_opening_hours:2:day], [node:field_opening_hours:3:day], [node:field_opening_hours:4:day], [node:field_opening_hours:5:day], [node:field_opening_hours:6:day]. This led to Metatag/Schema.org Metatag requesting an index of the office hours that did not exist via the token hook.

Steps to reproduce

Create a node with office hours and do not fill all days.
Add Metatag/Schema.org Metatag entries for that node type and use all seven indexes. Or more if you'd like, as that would also lead to these warnings.
Navigate to node.

Proposed resolution

Check field index is not null before accessing in token replacement.

Remaining tasks

I shall follow this issue with a patch/issue fork shortly.

🐛 Bug report
Status

Fixed

Version

1.7

Component

Code

Created by

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024