Error addOfficeHours Argument #4 ($replace_exceptions) must be of type bool

Created on 6 May 2025, 9 days ago

Problem/Motivation

The Office Hours module is throwing a TypeError when formatting office hours data because the code is not properly handling null values for the $replace_exceptions parameter. This occurs in the OfficeHoursItemListFormatter::addOfficeHours() method, which expects a boolean value but is receiving null in some cases.

Steps to reproduce

1. Install and enable the Office Hours module
2. Create content that uses the Office Hours field
3. View the content with certain configuration settings where the 'replace_exceptions' setting is not defined
4. The page will display an error:
"TypeError: Drupal\office_hours\OfficeHoursItemListFormatter::addOfficeHours(): Argument #4 ($replace_exceptions) must be of type bool, null given, called in /var/www/html/htdocs/modules/contrib/office_hours/src/OfficeHoursItemListFormatter.php on line 125"

Proposed resolution

Add null coalescing operators to provide default values for potentially null settings:
1. Set a default value of FALSE for $replace_exceptions when it's null
2. Set a default value of 0 for $horizon when it's null
3. Cast $replace_exceptions to boolean when passing it to addOfficeHours() method

Remaining tasks

1. Apply and review the patch
2. Test the fix with various Office Hours field configurations
3. Consider adding unit tests to verify the fix

User interface changes

None. This is a bug fix that does not affect the user interface.

API changes

None. The fix maintains backward compatibility by providing default values for parameters that might be null.

Data model changes

None. This fix only addresses parameter handling in the formatter and does not change any data structures or storage.

🐛 Bug report
Status

Active

Version

1.28

Component

Code

Created by

🇳🇱Netherlands Remco Hoeneveld

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

Comments & Activities

  • Issue created by @Remco Hoeneveld
  • 🇳🇱Netherlands johnv
  • 🇳🇱Netherlands johnv

    This should not be possible.
    I do not understand how you have empty 'exceptions' settings.
    But in any case, the 'defaultSettings' are there to prevent such cases.

    Attached patch should fix the problem, not the symptom. The current code is too cautious.
    Pleas test and report back.

Production build 0.71.5 2024