DateTimeComputed: Fatal error when Ajax call in form. (strlen() on array)

Created on 3 October 2022, over 1 year ago
Updated 6 June 2024, 12 days ago

Problem/Motivation

When a form launches an AJAX call (like in the yoast_seo module), then the form_state is build by the request. (core/lib/Drupal/Core/Form/EventSubscriber/FormAjaxSubscriber.php r:103)
Therefor, the DateTime fields have their values as an array and not a string (array containing date and time elements), like in the form names: field_date[0][value][date] and field_date[0][value][time].
Result: in the DateTimeComputed.php (r:47) =>

$value = $item->{($this->definition->getSetting('date source'))};

the value is now an array, instead of a string, which on line 58 -> 251, throws an error:

TypeError: DateTime::createFromFormat(): Argument #2 ($datetime) must be of type string, array given in DateTime::createFromFormat() (line 251 of /var/app/web/core/lib/Drupal/Component/Datetime/DateTimePlus.php)

Steps to reproduce

  1. Install Drupal core with the Standard profile.
  2. Install and enable the Paragraphs module.
  3. Create a Test paragraph type with a required date field (Date type: Date only).
  4. Add an entity-reference revisions (ERR) field to the page content type, referencing the Test paragraph type (number of values: unlimited).
  5. Create a page node. Leave the date field empty. Click the "Add Test" button.

Proposed resolution

I'll share a patch in comments, but it probably can be handled better.
As we don't know which key is being handled (date/time) I just check if the value is an array, and if so just use the first element instead.
feel free to further search for a cleaner solution.

Remaining tasks

  1. Find steps to reproduce without using contributed modules.
  2. Decide on the right approach. (See the patch in Comment #2 and the one on 🐛 DateTime::createFromFormat() expects parameter 2 to be string Closed: duplicate .)
  3. Add tests.
🐛 Bug report
Status

Active

Version

11.0 🔥

Component
Datetime 

Last updated about 19 hours ago

Created by

🇧🇪Belgium Tim Lammar

Live updates comments and jobs are added and updated live.
  • PHP 8.1

    The issue particularly affects sites running on PHP version 8.1.0 or later.

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.69.0 2024