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.
Comments
Comment #3
jurgenhaasProvided a fix in the MR.
Comment #4
mandclu commentedCould 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.
Comment #5
jurgenhaas@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 foris_numeric($items[$delta]->value)as the function requires a numeric argument.Comment #6
ramprog commentedJurgen, the patch works for us (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 being 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.
Comment #7
hfernandes commentedI was able to reproduce this issue with the following steps:
Smart Date | Inline range with timezoneApplying the provided patch, the error goes away.
Comment #8
snap_x@jurgenhaas thanks for the fix. It solves the issue for me.
Comment #9
mandclu commentedComment #11
mandclu commentedThanks for the work here. Merged into the 4.3.x branch.
Comment #12
mandclu commented