InvalidArgumentException: The timestamp must be numeric. in Drupal\Component\Datetime\DateTimePlus::createFromTimestamp

Created on 21 February 2025, 6 months ago

Problem/Motivation

We have a smart_date field with unlimited cardinality and when we use the entity in a complex IEF (inline entity form), we get an error when adding another item to the widget:

#0 /var/www/html/web/modules/contrib/smart_date/src/Plugin/Field/FieldWidget/SmartDateWidgetBase.php(142): Drupal\Component\Datetime\DateTimePlus::createFromTimestamp(Array, '')
#1 /var/www/html/web/modules/contrib/smart_date/src/Plugin/Field/FieldWidget/SmartDateDefaultWidget.php(110): Drupal\smart_date\Plugin\Field\FieldWidget\SmartDateWidgetBase->formElement(Object(Drupal\smart_date\Plugin\Field\FieldType\SmartDateFieldItemList), 0, Array, Array, Object(Drupal\Core\Form\FormState))
#2 /var/www/html/web/modules/contrib/smart_date/src/Plugin/Field/FieldWidget/SmartDateInlineWidget.php(35): Drupal\smart_date\Plugin\Field\FieldWidget\SmartDateDefaultWidget->formElement(Object(Drupal\smart_date\Plugin\Field\FieldType\SmartDateFieldItemList), 0, Array, Array, Object(Drupal\Core\Form\FormState))
#3 /var/www/html/web/core/lib/Drupal/Core/Field/WidgetBase.php(459): Drupal\smart_date\Plugin\Field\FieldWidget\SmartDateInlineWidget->formElement(Object(Drupal\smart_date\Plugin\Field\FieldType\SmartDateFieldItemList), 0, Array, Array, Object(Drupal\Core\Form\FormState))
#4 /var/www/html/web/modules/contrib/smart_date/src/Plugin/Field/FieldWidget/SmartDateWidgetBase.php(562): Drupal\Core\Field\WidgetBase->formSingleElement(Object(Drupal\smart_date\Plugin\Field\FieldType\SmartDateFieldItemList), 0, Array, Array, Object(Drupal\Core\Form\FormState))
#5 /var/www/html/web/core/lib/Drupal/Core/Field/WidgetBase.php(120): Drupal\smart_date\Plugin\Field\FieldWidget\SmartDateWidgetBase->formMultipleElements(Object(Drupal\smart_date\Plugin\Field\FieldType\SmartDateFieldItemList), Array, Object(Drupal\Core\Form\FormState))
#6 /var/www/html/web/core/lib/Drupal/Core/Entity/Entity/EntityFormDisplay.php(197): Drupal\Core\Field\WidgetBase->form(Object(Drupal\smart_date\Plugin\Field\FieldType\SmartDateFieldItemList), Array, Object(Drupal\Core\Form\FormState))
#7 /var/www/html/web/modules/contrib/inline_entity_form/src/Form/EntityInlineForm.php(186): Drupal\Core\Entity\Entity\EntityFormDisplay->buildForm(Object(Drupal\fedms_content\Entity\Session), Array, Object(Drupal\Core\Form\FormState))
#8 /var/www/html/web/modules/contrib/inline_entity_form/src/Element/InlineEntityForm.php(149): Drupal\inline_entity_form\Form\EntityInlineForm->entityForm(Array, Object(Drupal\Core\Form\FormState))
#9 [internal function]: Drupal\inline_entity_form\Element\InlineEntityForm::processEntityForm(Array, Object(Drupal\Core\Form\FormState), Array)
#10 /var/www/html/web/core/lib/Drupal/Core/Form/FormBuilder.php(1010): call_user_func_array(Array, Array)
#11 /var/www/html/web/core/lib/Drupal/Core/Form/FormBuilder.php(1073): Drupal\Core\Form\FormBuilder->doBuildForm('group_session_e...', Array, Object(Drupal\Core\Form\FormState))
#12 /var/www/html/web/core/lib/Drupal/Core/Form/FormBuilder.php(1073): Drupal\Core\Form\FormBuilder->doBuildForm('group_session_e...', Array, Object(Drupal\Core\Form\FormState))
#13 /var/www/html/web/core/lib/Drupal/Core/Form/FormBuilder.php(1073): Drupal\Core\Form\FormBuilder->doBuildForm('group_session_e...', Array, Object(Drupal\Core\Form\FormState))
#14 /var/www/html/web/core/lib/Drupal/Core/Form/FormBuilder.php(1073): Drupal\Core\Form\FormBuilder->doBuildForm('group_session_e...', Array, Object(Drupal\Core\Form\FormState))
#15 /var/www/html/web/core/lib/Drupal/Core/Form/FormBuilder.php(1073): Drupal\Core\Form\FormBuilder->doBuildForm('group_session_e...', Array, Object(Drupal\Core\Form\FormState))
#16 /var/www/html/web/core/lib/Drupal/Core/Form/FormBuilder.php(1073): Drupal\Core\Form\FormBuilder->doBuildForm('group_session_e...', Array, Object(Drupal\Core\Form\FormState))
#17 /var/www/html/web/core/lib/Drupal/Core/Form/FormBuilder.php(444): Drupal\Core\Form\FormBuilder->doBuildForm('group_session_e...', Array, Object(Drupal\Core\Form\FormState))
#18 /var/www/html/web/core/lib/Drupal/Core/Form/FormBuilder.php(625): Drupal\Core\Form\FormBuilder->rebuildForm('group_session_e...', Object(Drupal\Core\Form\FormState), Array)
#19 /var/www/html/web/core/lib/Drupal/Core/Form/FormBuilder.php(321): Drupal\Core\Form\FormBuilder->processForm('group_session_e...', Array, Object(Drupal\Core\Form\FormState))
#20 /var/www/html/web/core/lib/Drupal/Core/Controller/FormController.php(73): Drupal\Core\Form\FormBuilder->buildForm(Object(Drupal\group\Entity\Form\GroupForm), Object(Drupal\Core\Form\FormState))
#21 /var/www/html/web/core/modules/layout_builder/src/Controller/LayoutBuilderHtmlEntityFormController.php(39): Drupal\Core\Controller\FormController->getContentResult(Object(Symfony\Component\HttpFoundation\Request), Object(Drupal\Core\Routing\RouteMatch))
#22 [internal function]: Drupal\layout_builder\Controller\LayoutBuilderHtmlEntityFormController->getContentResult(Object(Symfony\Component\HttpFoundation\Request), Object(Drupal\Core\Routing\RouteMatch))
#23 /var/www/html/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(123): call_user_func_array(Array, Array)
#24 /var/www/html/web/core/lib/Drupal/Core/Render/Renderer.php(593): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}()
#25 /var/www/html/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(121): Drupal\Core\Render\Renderer->executeInRenderContext(Object(Drupal\Core\Render\RenderContext), Object(Closure))
#26 /var/www/html/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(97): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(Array, Array)
#27 /var/www/html/vendor/symfony/http-kernel/HttpKernel.php(183): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}()
#28 /var/www/html/vendor/symfony/http-kernel/HttpKernel.php(76): Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object(Symfony\Component\HttpFoundation\Request), 1)
#29 /var/www/html/web/core/lib/Drupal/Core/StackMiddleware/Session.php(53): Symfony\Component\HttpKernel\HttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#30 /var/www/html/web/core/lib/Drupal/Core/StackMiddleware/KernelPreHandle.php(48): Drupal\Core\StackMiddleware\Session->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#31 /var/www/html/web/core/lib/Drupal/Core/StackMiddleware/ContentLength.php(28): Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#32 /var/www/html/web/core/modules/big_pipe/src/StackMiddleware/ContentLength.php(32): Drupal\Core\StackMiddleware\ContentLength->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#33 /var/www/html/web/core/modules/ban/src/BanMiddleware.php(50): Drupal\big_pipe\StackMiddleware\ContentLength->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#34 /var/www/html/web/core/lib/Drupal/Core/StackMiddleware/ReverseProxyMiddleware.php(48): Drupal\ban\BanMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#35 /var/www/html/web/core/lib/Drupal/Core/StackMiddleware/NegotiationMiddleware.php(51): Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#36 /var/www/html/web/core/lib/Drupal/Core/StackMiddleware/AjaxPageState.php(36): Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#37 /var/www/html/web/core/lib/Drupal/Core/StackMiddleware/StackedHttpKernel.php(51): Drupal\Core\StackMiddleware\AjaxPageState->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#38 /var/www/html/web/core/lib/Drupal/Core/DrupalKernel.php(709): Drupal\Core\StackMiddleware\StackedHttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#39 /var/www/html/web/index.php(19): Drupal\Core\DrupalKernel->handle(Object(Symfony\Component\HttpFoundation\Request))
#40 {main}

From that stack trace, it looks like $items[$delta]->value in \Drupal\smart_date\Plugin\Field\FieldWidget\SmartDateWidgetBase::formElement is an array.

Proposed resolution

The code already checks if the property is not empty. It should also check if it contains a numeric value.

🐛 Bug report
Status

Active

Version

4.2

Component

Code

Created by

🇩🇪Germany jurgenhaas Gottmadingen

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

Merge Requests

Comments & Activities

  • Issue created by @jurgenhaas
  • 🇩🇪Germany jurgenhaas Gottmadingen

    Provided a fix in the MR.

  • Pipeline finished with Success
    6 months ago
    Total: 150s
    #430642
  • 🇨🇦Canada mandclu

    Could we get some steps to reproduce on this? If it would be possible to anticipate and handle what might be such an array, it might be worth the effort.

  • 🇩🇪Germany jurgenhaas Gottmadingen

    @mandclu I've tried to reproduce this is a vanilla D11 site with no luck so far. I don't know what makes the difference on the customer site where we experience that issue.

    However, as the code block already check for !empty($items[$delta]->value) to then use that value as an integer for a function call, it would certainly not hurt to also check for is_numeric($items[$delta]->value) as the function requires a numeric argument.

  • Status changed to Needs review 3 months ago
  • 🇳🇱Netherlands ramprog

    Jurgen, the patch works for use (D10 Smart date 4.2.3)

    Our situation.
    On a training node we have training edition paragraphs which contain a smart date field.
    I add the start date and tick the box whole day , and leave the end date and time empty

    If i then click on add another paragraph, the current data is beeing validated and that is when the error occurs.
    Validation error on empty fields and a 500 error from ajax call due to the issue described here

    By applying the patch the error goes away.

  • 🇧🇷Brazil hfernandes

    I was able to reproduce this issue with the following steps:

    1. Install paragraphs module and create a paragraph type with the smart_date field
    2. In the paragraph form display, select Smart Date | Inline range with timezone
    3. Install the media module and enable any media field into CT. e.g Basic page CT
    4. Now add at the Basic page the paragraph field created at 1 step
    5. Try to create a Basic page, selecting the data into the paragraph, but without selecting a time.
    6. Now jump into media field and click into "Add Media" btn
    7. You'll receive erros into console and drupal message.


    Applying the provided patch, the error goes away.

Production build 0.71.5 2024