Exception thrown when combined with the 'condition_field' module

Created on 11 July 2023, over 1 year ago

Problem/Motivation

When this module is combined with the Condition Field module to add the Block Scheduler as a visibility condition to custom blocks, the following exception is thrown:

InvalidArgumentException: The timestamp must be numeric. in Drupal\Component\Datetime\DateTimePlus::createFromTimestamp() (line 201 of core/lib/Drupal/Component/Datetime/DateTimePlus.php).

This seems to happen because the Block Schedule module stores the 'Expiry' condition as a 'DrupalDateTime' object instead of a 'int' timestamp. This is the stored data structure in the database:

a:1:{s:6:"expiry";a:3:{s:5:"start";O:35:"Drupal\Core\Datetime\DrupalDateTime":11:{s:25:"*formatTranslationCache";N;s:15:"*inputTimeRaw";s:0:"";s:20:"*inputTimeAdjusted";s:0:"";s:19:"*inputTimeZoneRaw";s:0:"";s:24:"*inputTimeZoneAdjusted";s:0:"";s:17:"*inputFormatRaw";s:0:"";s:22:"*inputFormatAdjusted";s:0:"";s:11:"*langcode";s:2:"en";s:9:"*errors";a:0:{}s:17:"*dateTimeObject";O:8:"DateTime":3:{s:4:"date";s:26:"2023-07-10 12:30:00.000000";s:13:"timezone_type";i:3;s:8:"timezone";s:16:"America/New_York";}s:20:"*stringTranslation";N;}s:3:"end";O:35:"Drupal\Core\Datetime\DrupalDateTime":11:{s:25:"*formatTranslationCache";N;s:15:"*inputTimeRaw";s:0:"";s:20:"*inputTimeAdjusted";s:0:"";s:19:"*inputTimeZoneRaw";s:0:"";s:24:"*inputTimeZoneAdjusted";s:0:"";s:17:"*inputFormatRaw";s:0:"";s:22:"*inputFormatAdjusted";s:0:"";s:11:"*langcode";s:2:"en";s:9:"*errors";a:0:{}s:17:"*dateTimeObject";O:8:"DateTime":3:{s:4:"date";s:26:"2023-07-11 12:30:00.000000";s:13:"timezone_type";i:3;s:8:"timezone";s:16:"America/New_York";}s:20:"*stringTranslation";N;}s:6:"negate";s:1:"0";}}

Steps to reproduce

  • Install and enable the Block Scheduler module.
  • Install and enable the Condition Field module.
  • Create a custom block type.
  • Add a 'Condition plugin field' to the custom block type.
  • Enable the 'Expiry' plugin in the 'Condition plugin field' configuration.
  • Save the settings.
  • Create a block of the newly custom block type created.
  • Add an expiry date. The block should be saved properly.
  • Edit the block.
  • The exception InvalidArgumentException is thrown.

Proposed resolution

Catch the InvalidArgumentException on the 'buildConfigurationForm' method of the 'Expiry' class and directly return the stored object in case it already is a 'DrupalDateTime' instance.

🐛 Bug report
Status

Active

Version

2.0

Component

Code

Created by

🇪🇸Spain MiguelArber

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

Comments & Activities

Production build 0.71.5 2024