DateTimeComputed tries to compute from an empty value

Created on 4 May 2019, over 5 years ago
Updated 29 August 2024, 3 months ago

📌 Remove the try/catch block from DateTimeComputed::getValue() Needs work 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 📌 Remove the try/catch block from DateTimeComputed::getValue() Needs work re-throws it.

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.

🐛 Bug report
Status

Needs work

Version

11.0 🔥

Component
Datetime 

Last updated about 7 hours ago

Created by

🇬🇧United Kingdom joachim

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

  • Needs issue summary update

    Issue summaries save everyone time if they are kept up-to-date. See Update issue summary task instructions.

Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

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

  • 🇬🇧United Kingdom alexpott 🇪🇺🌍
    +++ b/core/modules/datetime/src/DateTimeComputed.php
    @@ -46,8 +46,7 @@ public function getValue() {
    +    if (empty($value) || !is_string($value)) {
           return NULL;
    

    I 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.

  • 🇮🇳India TanujJain-TJ

    updated patch after addressing points from #21, still needs tests tho.

  • Status changed to Needs review over 1 year ago
  • 🇮🇳India TanujJain-TJ

    unrelated failure, retest is green. please review.

  • Status changed to Needs work over 1 year ago
  • 🇺🇸United States smustgrave

    @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.

  • 🇪🇸Spain Carlitus

    The #22 patch resolves for me the problem.

    I have a paragraph with somo required fields.

    Without de patch when i upload a imagen i get an ajax error and the image is not uploaded.

    And with the patch it works well:

  • 🇬🇧United Kingdom millnut

    Confirming 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

  • 🇦🇺Australia 2pha

    I 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.

  • 🇫🇷France Grimreaper France 🇫🇷

    Hi,

    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 🐛 On Paris timezone, impossible to set a date to 2023/03/31 02:00:00 Active .

    If this may help to have reproducible steps.

  • 🇪🇸Spain fjgarlin

    We had exactly the same situation described in #25 and the patch in #22 fixed the issue.

  • 🇪🇸Spain fjgarlin

    Full backtrace in case it helps for writing tests

    TypeError: DateTime::createFromFormat(): Argument #2 ($datetime) must be of type string, array given in DateTime::createFromFormat() (line 251 of /app/web/core/lib/Drupal/Component/Datetime/DateTimePlus.php).
    
    #0 /app/web/core/lib/Drupal/Component/Datetime/DateTimePlus.php(251): DateTime::createFromFormat()
    #1 /app/web/core/modules/datetime/src/DateTimeComputed.php(57): Drupal\Component\Datetime\DateTimePlus::createFromFormat()
    #2 /app/web/core/lib/Drupal/Core/Field/FieldItemBase.php(154): Drupal\datetime\DateTimeComputed->getValue()
    #3 /app/web/core/modules/datetime_range/src/Plugin/Field/FieldWidget/DateRangeWidgetBase.php(33): Drupal\Core\Field\FieldItemBase->__get()
    #4 /app/web/core/modules/datetime_range/src/Plugin/Field/FieldWidget/DateRangeDefaultWidget.php(59): Drupal\datetime_range\Plugin\Field\FieldWidget\DateRangeWidgetBase->formElement()
    #5 /app/web/core/lib/Drupal/Core/Field/WidgetBase.php(459): Drupal\datetime_range\Plugin\Field\FieldWidget\DateRangeDefaultWidget->formElement()
    #6 /app/web/core/lib/Drupal/Core/Field/WidgetBase.php(219): Drupal\Core\Field\WidgetBase->formSingleElement()
    #7 /app/web/core/lib/Drupal/Core/Field/WidgetBase.php(120): Drupal\Core\Field\WidgetBase->formMultipleElements()
    #8 /app/web/core/lib/Drupal/Core/Entity/Entity/EntityFormDisplay.php(186): Drupal\Core\Field\WidgetBase->form()
    #9 /app/web/modules/contrib/paragraphs/src/Plugin/Field/FieldWidget/ParagraphsWidget.php(807): Drupal\Core\Entity\Entity\EntityFormDisplay->buildForm()
    #10 /app/web/core/lib/Drupal/Core/Field/WidgetBase.php(459): Drupal\paragraphs\Plugin\Field\FieldWidget\ParagraphsWidget->formElement()
    #11 /app/web/modules/contrib/paragraphs/src/Plugin/Field/FieldWidget/ParagraphsWidget.php(1353): Drupal\Core\Field\WidgetBase->formSingleElement()
    #12 /app/web/core/lib/Drupal/Core/Field/WidgetBase.php(120): Drupal\paragraphs\Plugin\Field\FieldWidget\ParagraphsWidget->formMultipleElements()
    #13 /app/web/modules/contrib/paragraphs/src/Plugin/Field/FieldWidget/ParagraphsWidget.php(1475): Drupal\Core\Field\WidgetBase->form()
    #14 /app/web/core/lib/Drupal/Core/Entity/Entity/EntityFormDisplay.php(186): Drupal\paragraphs\Plugin\Field\FieldWidget\ParagraphsWidget->form()
    #15 /app/web/core/lib/Drupal/Core/Entity/ContentEntityForm.php(121): Drupal\Core\Entity\Entity\EntityFormDisplay->buildForm()
    #16 /app/web/core/modules/node/src/NodeForm.php(127): Drupal\Core\Entity\ContentEntityForm->form()
    #17 /app/web/core/lib/Drupal/Core/Entity/EntityForm.php(107): Drupal\node\NodeForm->form()
    #18 [internal function]: Drupal\Core\Entity\EntityForm->buildForm()
    #19 /app/web/core/lib/Drupal/Core/Form/FormBuilder.php(536): call_user_func_array()
    #20 /app/web/core/lib/Drupal/Core/Form/FormBuilder.php(375): Drupal\Core\Form\FormBuilder->retrieveForm()
    #21 /app/web/core/lib/Drupal/Core/Form/FormBuilder.php(633): Drupal\Core\Form\FormBuilder->rebuildForm()
    #22 /app/web/core/lib/Drupal/Core/Form/FormBuilder.php(325): Drupal\Core\Form\FormBuilder->processForm()
    #23 /app/web/core/lib/Drupal/Core/Controller/FormController.php(73): Drupal\Core\Form\FormBuilder->buildForm()
    #24 [internal function]: Drupal\Core\Controller\FormController->getContentResult()
    #25 /app/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(123): call_user_func_array()
    #26 /app/web/core/lib/Drupal/Core/Render/Renderer.php(627): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}()
    #27 /app/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(124): Drupal\Core\Render\Renderer->executeInRenderContext()
    #28 /app/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(97): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext()
    #29 /app/vendor/symfony/http-kernel/HttpKernel.php(181): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}()
    #30 /app/vendor/symfony/http-kernel/HttpKernel.php(76): Symfony\Component\HttpKernel\HttpKernel->handleRaw()
    #31 /app/web/core/lib/Drupal/Core/StackMiddleware/Session.php(58): Symfony\Component\HttpKernel\HttpKernel->handle()
    #32 /app/web/core/lib/Drupal/Core/StackMiddleware/KernelPreHandle.php(48): Drupal\Core\StackMiddleware\Session->handle()
    #33 /app/web/core/lib/Drupal/Core/StackMiddleware/ContentLength.php(28): Drupal\Core\StackMiddleware\KernelPreHandle->handle()
    #34 /app/web/core/modules/big_pipe/src/StackMiddleware/ContentLength.php(32): Drupal\Core\StackMiddleware\ContentLength->handle()
    #35 /app/web/core/modules/page_cache/src/StackMiddleware/PageCache.php(106): Drupal\big_pipe\StackMiddleware\ContentLength->handle()
    #36 /app/web/core/modules/page_cache/src/StackMiddleware/PageCache.php(85): Drupal\page_cache\StackMiddleware\PageCache->pass()
    #37 /app/web/core/lib/Drupal/Core/StackMiddleware/ReverseProxyMiddleware.php(48): Drupal\page_cache\StackMiddleware\PageCache->handle()
    #38 /app/web/core/lib/Drupal/Core/StackMiddleware/NegotiationMiddleware.php(51): Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle()
    #39 /app/web/core/lib/Drupal/Core/StackMiddleware/AjaxPageState.php(36): Drupal\Core\StackMiddleware\NegotiationMiddleware->handle()
    #40 /app/web/core/lib/Drupal/Core/StackMiddleware/StackedHttpKernel.php(51): Drupal\Core\StackMiddleware\AjaxPageState->handle()
    #41 /app/web/core/lib/Drupal/Core/DrupalKernel.php(704): Drupal\Core\StackMiddleware\StackedHttpKernel->handle()
    #42 /app/web/index.php(19): Drupal\Core\DrupalKernel->handle()
    #43 {main}
    
  • 🇪🇸Spain fjgarlin

    I'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 DateTimeComputed yet. Maybe some guidance here would help move the issue forward.

  • Pipeline finished with Failed
    7 months ago
    Total: 989s
    #150325
  • I am having the same issue on Drupal 10.3.2

Production build 0.71.5 2024