Problem/Motivation
#3025642: Remove the try/catch block from DateTimeComputed::getValue() revealed this bug.
If a date field is empty, trying to get the date computed property on it causes DateTimeComputed to try to create a date object, which causes DrupalDateTime to throw an exception. Currently that exception is caught, but the patch at #3025642: Remove the try/catch block from DateTimeComputed::getValue() re-throws it.
Steps to reproduce
- Install the latest version of Drupal 11.x using the standard profile
- Navigate to admin/structure/types/manage/article/fields and add a date field to the Article content type
- Create a node of the article content type
- Save the node with the date field empty and try to get the date computed property
Proposed resolution
It doesn't make sense to attempt to create a date if we know there's no source value anyway, so DateTimeComputed should return early in that case.
Remaining tasks
- Fix any pipeline errors
- Make sure test coverage is adequate
- Review the code
User interface changes
N/A
Introduced terminology
N/A
API changes
TBD
Data model changes
N/A
Release notes snippet
TBD
| Comment | File | Size | Author |
|---|---|---|---|
| #36 | 3052608_1.patch | 453 bytes | nehajyoti |
| #36 | 3052608.patch | 453 bytes | nehajyoti |
| #25 | Captura desde 2023-04-26 13-58-42.png | 87.48 KB | carlitus |
| #25 | Captura desde 2023-04-26 13-48-20.png | 139.93 KB | carlitus |
| #22 | interdiff_16-22.txt | 461 bytes | tanuj. |
Issue fork drupal-3052608
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
joachim commentedComment #3
jian he commentedThe patch solved the issue.
Comment #5
yogeshmpawarAll looks good so setting back to RTBC! Tests failure is not related (see #3055648: Frequent random fail in \Drupal\Tests\media_library\FunctionalJavascript\MediaLibraryTest).
Comment #6
wim leersSorry, but this still needs an explicit test.
Comment #8
matthiasm11 commentedWhen using date range items,
$valuecan be an array containing empty values:Since
createFromFormat()expects a string value, we should not only check if$valueis empty (NULLor an empty string), but also if it's a string before callingcreateFromFormat().Steps to reproduce:
Warning: DateTime::createFromFormat() expects parameter 2 to be string, array given in /var/www/web/core/lib/Drupal/Component/Datetime/DateTimePlus.php.Comment #9
hardik_patel_12 commentedComment #11
kishor_kolekar commentedI've re-rolled patch for 9.1
Comment #16
tunprogI've re-rolled patch for 9.3
Comment #17
bvoynickStill needs tests
Comment #19
benjifisherWe need to clarify the scope of this issue.
The issue description has not changed since this issue was created. The bug described there does not show up unless you apply one of the patches for #3025642: Remove the try/catch block from DateTimeComputed::getValue().
The first patch on this issue, in #2, tests
empty($value). In the current 10.1.x, there is a very similar test of$value === NULL. So it may be that the original bug described here was already fixed in #3240181: \Drupal\datetime\DateTimeComputed::getValue() causes deprecations on PHP 8.1. I am adding that as a related issue.Comment #8 describes steps to reproduce that involve the Paragraphs module. The patch in that comment, and the later patches, all test
empty($value) || !is_string($value). That looks like it is fixing a different problem, and that problem may be in the Paragraphs module, not in Drupal core.Is there a bug here that can be reproduced with just Drupal core? I tried to do something like the steps to reproduce in #8. I added a Datetime field to a content type, allowing unlimited values. If I make the field required and try to add a second instance while the first is empty, then I get a form validation error. If the field is not required, then I can add as many empty ones as I like, and they are clearly being added with AJAX calls. It looks as though
$valueisNULLin these cases.If the original problem has been fixed, then let's close this issue as outdated. If we can reproduce a bug with just Drupal core, then let's add steps to reproduce in the issue summary. If the problem is actually with Paragraphs, then let's open an issue for that module.
Comment #20
bvoynickI am seeing something similar come up in some instances involving AJAX and form rebuilding, which I think is probably the motivation for #16. The scope does indeed lie beyond instances of a NULL value - rather, the problem is an array value. So far I am not able to reproduce in Core alone without contrib. However, I think limit_validation_errors may be hiding this problem in Core-only AJAX cases I have tried testing.
The value getting set to an array does lie in Core. More details are in this issue: https://www.drupal.org/project/drupal/issues/3313231 There is a fatal on array values even without #3025642 - and while I think Paragraphs is related, I don't think it's to blame. As noted in that ticket, there seems to be an issue with inconsistent override of $form_state in Datetime and DateList elements. I think it's likely that element classes issue together with a perfect storm of AJAX, a heavy hand in rebuilding such as in Paragraph widgets, and contexts where limit_validation_errors does not happen to prevent this from coming up.
I am not certain whether this is best addressed...
Comment #21
alexpottI think this should be
if (!is_string($value) && $value !== '') {if $value = '0'; then empty($value) will return TRUE.
We really need some tests here. And a reason why the value is not a NULL or string at this point.
Comment #22
tanuj. commentedupdated patch after addressing points from #21, still needs tests tho.
Comment #23
tanuj. commentedunrelated failure, retest is green. please review.
Comment #24
smustgrave commented@Tanuj thank you for the interest.
This was previously tagged for tests which weren't added.
Issue summary should be updated to highlight proposed solution.
Please do some basic checks before putting in review.
Thanks.
Comment #25
carlitus commentedThe #22 patch resolves for me the problem.
I have a paragraph with some required fields.
Without de patch when I upload a image I get an ajax error and the image is not uploaded.

And with the patch it works well:

Comment #27
millnut commentedConfirming this patch 16 fixes my issue in Drupal 9.5.x, with the minimal test case below.
- Creating new content
- Required Date field in a paragraph with no default value set
- Using media library for an image field
Results in an AJAX error and the media library popup not opening
Comment #28
2phaI hit this too...
I have...
A profile with a required date field with no default value.
This profile is required to be filled on user creation (so shows on the "add user" page).
All is ok until I add a password policy (which maybe forces a form rebuild?)
When adding a user and I tab away from the password field, ajax is fired and I get the "DateTime::createFromFormat(): Argument #2 ($datetime) must be of type string" error.
The $time variable used as the second parameter sent to createFromFormat is an array in this case.
If I set a value on the required date field before trying to set the password, I don't get the error and the $time variable is a string.
Patch #22 worked for me
Comment #29
grimreaperHi,
I am not sure if this is related, but I had the same problem when I had a value set in my daterange field. And when trying to isolate the problem, in my case I ended up with a simple date field for a very specific datetime #3410541: On Paris timezone, impossible to set a date to 2023/03/31 02:00:00.
If this may help to have reproducible steps.
Comment #30
fjgarlin commentedWe had exactly the same situation described in #25 and the patch in #22 fixed the issue.
Comment #31
fjgarlin commentedComment #32
fjgarlin commentedFull backtrace in case it helps for writing tests
Comment #34
fjgarlin commentedI've moved #22 to an MR here https://git.drupalcode.org/project/drupal/-/merge_requests/7585/diffs
So we can continue development and testing there.
I don't think that there are tests that were checking the NULL case before, so I'm not sure where to best place the tests. In fact, I don't see any tests for
DateTimeComputedyet. Maybe some guidance here would help move the issue forward.Comment #35
jawadharaba98 commentedI am having the same issue on Drupal 10.3.2
Comment #36
nehajyoti commentedLatest Patch to fix this issue (occured while drupal core upgrade from 10.3.* to 10.4.3)
Comment #37
rob230 commentedI get this in Drupal 10.4 - just have any AJAX request (such as an "add another value") in a multiple paragraphs field, and it will fail with this error if there is a date field that you haven't populated yet.
The patch #36 fixes it.
This appears to be the same issue as #3313231: DateTimeComputed: Fatal error when Ajax call in form. (strlen() on array) but the fix in this issue makes more sense to me.
Comment #39
riyas_nr commentedAdded test coverage for passing non-string values to
DateTimeComputed::getValue(). Moving to NR.Comment #41
tame4tex commentedI was able to reproduce the bug in core in a FunctionalJavascript test using
NestedEntityTestFormfrom thefield_testmodule. This test has been committed to the MR of #3313231: DateTimeComputed: Fatal error when Ajax call in form. (strlen() on array). Also see comment #21 on that issue.I have also updated that issue with an alternative solution that removes the need to modify
DateTimeComputed::getValue.This issue could possibly be closed as a duplicate of #3313231: DateTimeComputed: Fatal error when Ajax call in form. (strlen() on array). But I guess it begs the question, is it right for
DateTimeComputed::getValueto expect the value to either be a string or NULL? I propose it is and stricter typing should be applied by throwing a helpful exception. If that is the case, then this issue, if not closed as a duplicate of #3313231: DateTimeComputed: Fatal error when Ajax call in form. (strlen() on array), should be closed as a duplicate of #3025642: Remove the try/catch block from DateTimeComputed::getValue() or #3213054: DateTimeComputed is suppressing date errors silently.Comment #42
tame4tex commentedComment #43
smustgrave commentedThink this one needs an issue summary first.
Comment #44
ironnuts commentedAdded the IS template and created steps to reproduce (more steps and more detail would be useful).
Comment #47
herved commentedRebased the MR
The project I work on needed this exactly for the scenario described in the other issue - nested paragraphs and empty required date field then click add more - #3313231: DateTimeComputed: Fatal error when Ajax call in form. (strlen() on array) I can confirm it also fixes the issue.
Comment #49
liam morlandI have corrected the conditional that it ensures
$valueis a non-empty string. This fix seems to be working.There is a
try/catchblock, but that would catch an exception, not a type error.