datetime date views filter: DateTime object not set

Created on 27 April 2018, almost 7 years ago
Updated 30 January 2023, about 2 years ago

Problem/Motivation

Using datetime views filter and filtering with an invalid date, there is no validation and fatal error occured.

To reproduce:

  • Add a datetime field in an entity bundle.
  • Create a view to list this entities and add a filter on this datetime field
  • Search for a date with wrong format or for example day in french: lun 2018-04-27. The following error occur:
    <em class="placeholder">Exception</em>: DateTime object not set. in <em class="placeholder">Drupal\Component\Datetime\DateTimePlus-&gt;__call()</em> (line <em class="placeholder">355</em> of <em class="placeholder">core/lib/Drupal/Component/Datetime/DateTimePlus.php</em>). <pre class="backtrace">Drupal\datetime\Plugin\views\filter\Date-&gt;opSimple(&#039;sesame_quota_option__field_sesame_opt_date.field_sesame_opt_date_value&#039;) (Line: 314)
    Drupal\views\Plugin\views\filter\NumericFilter-&gt;query() (Line: 1370)
    Drupal\views\ViewExecutable-&gt;_build(&#039;filter&#039;) (Line: 1259)
    Drupal\views\ViewExecutable-&gt;build() (Line: 390)
    Drupal\views\Plugin\views\display\PathPluginBase-&gt;execute() (Line: 180)
    Drupal\views\Plugin\views\display\Page-&gt;execute() (Line: 1627)
    Drupal\views\ViewExecutable-&gt;executeDisplay(&#039;page_1&#039;, Array) (Line: 77)
    Drupal\views\Element\View::preRenderViewElement(Array)
    call_user_func(Array, Array) (Line: 378)
    Drupal\Core\Render\Renderer-&gt;doRender(Array, ) (Line: 195)
    Drupal\Core\Render\Renderer-&gt;render(Array, ) (Line: 226)
    Drupal\Core\Render\MainContent\HtmlRenderer-&gt;Drupal\Core\Render\MainContent\{closure}() (Line: 582)
    Drupal\Core\Render\Renderer-&gt;executeInRenderContext(Object, Object) (Line: 227)
    Drupal\Core\Render\MainContent\HtmlRenderer-&gt;prepare(Array, Object, Object) (Line: 117)
    Drupal\Core\Render\MainContent\HtmlRenderer-&gt;renderResponse(Array, Object, Object) (Line: 90)
    Drupal\Core\EventSubscriber\MainContentViewSubscriber-&gt;onViewRenderArray(Object, &#039;kernel.view&#039;, Object) (Line: 76)
    Drupal\webprofiler\EventDispatcher\TraceableEventDispatcher-&gt;dispatch(&#039;kernel.view&#039;, Object) (Line: 156)
    Symfony\Component\HttpKernel\HttpKernel-&gt;handleRaw(Object, 1) (Line: 68)
    Symfony\Component\HttpKernel\HttpKernel-&gt;handle(Object, 1, 1) (Line: 57)
    Drupal\Core\StackMiddleware\Session-&gt;handle(Object, 1, 1) (Line: 47)
    Drupal\Core\StackMiddleware\KernelPreHandle-&gt;handle(Object, 1, 1) (Line: 99)
    Drupal\page_cache\StackMiddleware\PageCache-&gt;pass(Object, 1, 1) (Line: 78)
    Drupal\page_cache\StackMiddleware\PageCache-&gt;handle(Object, 1, 1) (Line: 47)
    Drupal\Core\StackMiddleware\ReverseProxyMiddleware-&gt;handle(Object, 1, 1) (Line: 38)
    Drupal\webprofiler\StackMiddleware\WebprofilerMiddleware-&gt;handle(Object, 1, 1) (Line: 50)
    Drupal\Core\StackMiddleware\NegotiationMiddleware-&gt;handle(Object, 1, 1) (Line: 23)
    Stack\StackedHttpKernel-&gt;handle(Object, 1, 1) (Line: 664)
    Drupal\Core\DrupalKernel-&gt;handle(Object) (Line: 19)
  • Search for the same date with day in english works: mon 2018-04-27. Same for date only: 2018-04-27

Proposed resolution

Mimic the date filter validation from views module.

Remaining tasks

  1. Add tests
  2. Review
🐛 Bug report
Status

Needs work

Version

9.5

Component
Datetime 

Last updated 9 days ago

Created by

🇫🇷France goz

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.

  • Usability

    Makes Drupal easier to use. Preferred over UX, D7UX, etc.

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.

  • The Needs Review Queue Bot tested this issue. It either no longer applies to Drupal core, or fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".

    Apart from a re-roll or rebase, this issue may need more work to address feedback in the issue or MR comments. To progress an issue, incorporate this feedback as part of the process of updating the issue. This helps other contributors to know what is outstanding.

    Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.

  • Status changed to Needs review almost 2 years ago
  • 🇮🇳India arunkumark Coimbatore

    I also faced the same issue with the Date time object on the Views exposed filter having the wrong Date format. The issue is due to the Views Token filter module. After the proper token update it works fine.

    But, expected to show a valid error or warning rather than a page break. The patch was not applied with the latest version of Drupal core 10.1.x. Rerolled the patch and resolved the page break.

  • Status changed to Needs work almost 2 years ago
  • The Needs Review Queue Bot tested this issue. It fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".

    This does not mean that the patch needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.

    Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.

  • 🇮🇳India Akram Khan Cuttack, Odisha

    added updated patch try to fix CCF #45. modifies the validateExposed() function to use a local variable $form_element to hold the field element that needs to be set with the form error message. It also changes the if-else block to use the null coalesce operator instead of an if-else block for brevity.

  • Status changed to Needs review almost 2 years ago
  • 🇮🇳India Akram Khan Cuttack, Odisha
  • Status changed to Needs work almost 2 years ago
  • 🇺🇸United States smustgrave

    Also look at the tags please. Was previously tagged for test which are still needed.

  • 🇺🇸United States edwardsay

    Patch works, but not in case the form element in the exposed form is placed inside a container or other wrapper. I added support for this case. Also, I faced an issue with the "reset" button while testing this patch: validation didn't let me perform the reset action. So I added improvement for that case as well.

  • Open in Jenkins → Open on Drupal.org →
    Environment: PHP 8.0 & MySQL 5.7
    last update over 1 year ago
    Custom Commands Failed
  • Open in Jenkins → Open on Drupal.org →
    Environment: PHP 7.3 & MySQL 5.7
    last update over 1 year ago
    Custom Commands Failed
  • last update over 1 year ago
    Custom Commands Failed
  • 🇺🇸United States edwardsay

    Code style improvement.

  • last update over 1 year ago
    30,341 pass
  • Open in Jenkins → Open on Drupal.org →
    Environment: PHP 7.3 & MySQL 5.7
    last update over 1 year ago
    30,374 pass
  • First commit to issue fork.
  • 🇺🇸United States douggreen Winchester, VA

    I still get a PHP warning in AttributeArray (line 79) because the original input array is statically cached in ViewExecutable::getExposedInput() on line 726, which happens before the data is validated. We need to validate the data before it is saved.

  • Status changed to Needs review 4 months ago
  • 🇺🇸United States Greg Boggs Portland Oregon

    Saw this exception in my log slowing down my website because bots trigger this one a lot. Looks like there's been significant work done on this merge request since it's last review.

  • 🇺🇸United States smustgrave

    Seems will still need test coverage for this. MR should be updated for 11.x

  • 🇬🇧United Kingdom oily Greater London
  • 🇬🇧United Kingdom oily Greater London
  • 🇬🇧United Kingdom oily Greater London

    I have reproduced the error in the UI of Drupal 11.x-dev.

  • 🇬🇧United Kingdom oily Greater London
  • 🇧🇷Brazil aluzzardi Pelotas, RS

    The current solution seems not to solve the issue if a URL accessing the date field has an invalid date format.
    For example, we received this request:

    ?to_date[date]=../admin/noop.cgi?foo=bar&test=blah

    A bot trying to brute force and causing issues, in that case, it throws the same error:

    User error: Exception thrown while calling __toString on a Drupal\Core\Datetime\DrupalDateTime object in /var/www/html/docroot/core/lib/Drupal/Component/Datetime/DateTimePlus.php on line 354: DateTime object not set. in Drupal\Component\Datetime\DateTimePlus->__toString() (line 20 of core/lib/Drupal/Component/Utility/ToStringTrait.php).
    Drupal\Component\Datetime\DateTimePlus->__toString() (Line: 161)
    Drupal\views\Plugin\views\filter\Date->acceptExposedInput(Array) (Line: 184)
    Drupal\views\Form\ViewsExposedForm->submitForm(Array, Object)
    call_user_func_array(Array, Array) (Line: 129)
    Drupal\Core\Form\FormSubmitter->executeSubmitHandlers(Array, Object) (Line: 67)
    Drupal\Core\Form\FormSubmitter->doSubmitForm(Array, Object) (Line: 609)
    Drupal\Core\Form\FormBuilder->processForm('views_exposed_form', Array, Object) (Line: 326)
    Drupal\Core\Form\FormBuilder->buildForm('\Drupal\views\Form\ViewsExposedForm', Object) (Line: 134)
    Drupal\views\Plugin\views\exposed_form\ExposedFormPluginBase->renderExposedForm() (Line: 1304)
    Drupal\views\ViewExecutable->build(NULL) (Line: 1457)
    Drupal\views\ViewExecutable->execute(NULL) (Line: 1520)
    Drupal\views\ViewExecutable->render() (Line: 133)
    Drupal\views\Plugin\views\display\Block->execute() (Line: 1696)
    Drupal\views\ViewExecutable->executeDisplay('block_3', Array) (Line: 81)
    Drupal\views\Element\View::preRenderViewElement(Array) (Line: 61)
    Drupal\views\Plugin\Block\ViewsBlock->build() (Line: 106)
    Drupal\layout_builder\EventSubscriber\BlockComponentRenderArray->onBuildRender(Object, 'section_component.build.render_array', Object)
    call_user_func(Array, Object, 'section_component.build.render_array', Object) (Line: 97)
    Drupal\tracer\EventDispatcher\TraceableEventDispatcher->dispatch(Object, 'section_component.build.render_array') (Line: 90)
    Drupal\layout_builder\SectionComponent->toRenderArray(Array, ) (Line: 88)
    Drupal\layout_builder\Section->toRenderArray(Array) (Line: 331)
    Drupal\layout_builder\Entity\LayoutBuilderEntityViewDisplay->buildSections(Object) (Line: 291)
    Drupal\layout_builder\Entity\LayoutBuilderEntityViewDisplay->buildMultiple(Array) (Line: 340)
    Drupal\Core\Entity\EntityViewBuilder->buildComponents(Array, Array, Array, 'full') (Line: 24)
    Drupal\node\NodeViewBuilder->buildComponents(Array, Array, Array, 'full') (Line: 282)
    Drupal\Core\Entity\EntityViewBuilder->buildMultiple(Array) (Line: 239)
    Drupal\Core\Entity\EntityViewBuilder->build(Array)
    call_user_func_array(Array, Array) (Line: 113)
    Drupal\Core\Render\Renderer->doTrustedCallback(Array, Array, 'Render #pre_render callbacks must be methods of a class that implements \Drupal\Core\Security\TrustedCallbackInterface or be an anonymous function. The callback was %s. See https://www.drupal.org/node/2966725', 'exception', 'Drupal\Core\Render\Element\RenderCallbackInterface') (Line: 870)
    Drupal\Core\Render\Renderer->doCallback('#pre_render', Array, Array) (Line: 432)
    Drupal\Core\Render\Renderer->doRender(Array, ) (Line: 248)
    Drupal\Core\Render\Renderer->render(Array, ) (Line: 238)
    Drupal\Core\Render\MainContent\HtmlRenderer->Drupal\Core\Render\MainContent\{closure}() (Line: 638)
    Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 231)
    Drupal\Core\Render\MainContent\HtmlRenderer->prepare(Array, Object, Object) (Line: 128)
    Drupal\Core\Render\MainContent\HtmlRenderer->renderResponse(Array, Object, Object) (Line: 90)
    Drupal\Core\EventSubscriber\MainContentViewSubscriber->onViewRenderArray(Object, 'kernel.view', Object)
    call_user_func(Array, Object, 'kernel.view', Object) (Line: 97)
    Drupal\tracer\EventDispatcher\TraceableEventDispatcher->dispatch(Object, 'kernel.view') (Line: 186)
    Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 76)
    Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 53)
    Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 48)
    Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 28)
    Drupal\Core\StackMiddleware\ContentLength->handle(Object, 1, 1) (Line: 106)
    Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1) (Line: 85)
    Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 263)
    Drupal\shield\ShieldMiddleware->bypass(Object, 1, 1) (Line: 130)
    Drupal\shield\ShieldMiddleware->handle(Object, 1, 1) (Line: 48)
    Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 38)
    Drupal\tracer\StackMiddleware\TracesMiddleware->handle(Object, 1, 1) (Line: 51)
    Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 36)
    Drupal\Core\StackMiddleware\AjaxPageState->handle(Object, 1, 1) (Line: 51)
    Drupal\Core\StackMiddleware\StackedHttpKernel->handle(Object, 1, 1) (Line: 741)
    Drupal\Core\DrupalKernel->handle(Object) (Line: 19)
    

    I will check to apply a fix for that case too.

  • 🇬🇧United Kingdom oily Greater London

    oily changed the visibility of the branch 2966735-datetime-date-views to hidden.

  • Pipeline finished with Failed
    4 months ago
    #302320
  • Pipeline finished with Failed
    4 months ago
    Total: 88s
    #302328
  • Pipeline finished with Success
    4 months ago
    Total: 847s
    #302341
  • Pipeline finished with Failed
    4 months ago
    Total: 446s
    #302359
  • Pipeline finished with Failed
    4 months ago
    Total: 84s
    #302367
  • Pipeline finished with Failed
    4 months ago
    Total: 142s
    #302371
  • Pipeline finished with Success
    4 months ago
    Total: 954s
    #302386
  • 🇬🇧United Kingdom oily Greater London
    1. A new branch based on 11.x-dev has been created and is now the default branch
    2. The new datetime field validation patch has been committed to a MR
    3. The validation function code has been fixed including phpstan
    4. The pipeline is running green
    5. Testing locally in a test view with an exposed datetime filter, the fix validates wrongly formatted dates

    The nScreenshot showing an invalid date getting validated in exposed filter

  • 🇬🇧United Kingdom oily Greater London

    Test coverage is now in place. Changing status to 'Needs review'.

  • 🇬🇧United Kingdom oily Greater London
  • Pipeline finished with Failed
    4 months ago
    Total: 124s
    #302552
  • Pipeline finished with Failed
    4 months ago
    Total: 172s
    #302558
  • Pipeline finished with Failed
    4 months ago
    #302564
  • The Needs Review Queue Bot tested this issue. It fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".

    This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.

    Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.

  • Pipeline finished with Success
    4 months ago
    Total: 685s
    #306501
  • 🇬🇧United Kingdom oily Greater London
  • 🇬🇧United Kingdom oily Greater London
  • 🇬🇧United Kingdom oily Greater London
  • 🇺🇸United States smustgrave

    Why remove Needs Review Queue Initiative tag?

  • Pipeline finished with Success
    4 months ago
    Total: 1027s
    #316213
  • 🇬🇧United Kingdom oily Greater London

    Good spot. Have restored the attribute.

  • 🇬🇧United Kingdom oily Greater London
  • 🇺🇸United States smustgrave

    Believe feedback has been addressed here.

  • Status changed to RTBC 2 months ago
  • 🇳🇿New Zealand quietone

    I read the comments and the MR and tested the change locally. I find that all questions are answered. I also updated credit.

    And, I'd like to remind committers that screenshots should be available from the issue summary.

    Leaving at RTBC

Production build 0.71.5 2024