Date filters on Views don't work

Created on 29 June 2024, 5 months ago
Updated 29 August 2024, 3 months ago

Problem/Motivation

Date filters on Views don't work when exposed in the block configuration.

Steps to reproduce

  1. Install the latest version of Panopoly 3.0.x, with demo content.
  2. Log in as an administrator.
  3. Create a new Content page.
  4. Edit the Panopoly Demo view to add an exposed date filter: Filter on the Authored On date of the content item. Choose the operator "greater or equal."
  5. In the Views UI preview, set the exposed sorts to Post date, Desc. You should see your new content item at the top of the list, followed by 2 of the 3 demo items.
  6. In the Authored on filter field, enter the date "2024-06-01" and click Apply.
  7. Because the authored on dates of the demo content are quite old (2012), you will only see the item you just created in the preview. This is the expected behavior.
  8. Save the View.
  9. Clear caches.
  10. Create a new Landing page.
  11. Customize the page, adding the Demo view to the layout.
  12. In the block config, set the Sort to Post date, Desc, and enter the date "2024-06-01."
  13. Update the preview.
  14. Note that three items are displayed in the preview, including 2 that were authored before 2024-06-01.
  15. Save the block and save the page.
  16. Note that three items are displayed in the view, including 2 that were authored before 2024-06-01.

Given that this is working in the Views UI but not the rendered block, there may be a problem somewhere in Panopoly Magic. Because date filters can have multiple fields (min and max if you choose the "between" operator), the MagicBlock processing may not be handling the filters correctly.

Proposed resolution

Fix the date filters.

Remaining tasks

Investigate, patch, and test.

User interface changes

None.

API changes

None.

Data model changes

None.

πŸ› Bug report
Status

Fixed

Version

3.0

Component

Magic

Created by

πŸ‡ΊπŸ‡ΈUnited States cboyden

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

Comments & Activities

  • Issue created by @cboyden
  • Status changed to Needs review 4 months ago
  • πŸ‡ΊπŸ‡ΈUnited States dsnopek USA

    Here's a patch that fixes this in my limited testing - please let me know if it works for you!

    And here's a test build which will hopefully show no regressions: https://gitlab.com/dsnopek/panopoly/-/jobs/7408713134

  • Status changed to Needs work 4 months ago
  • πŸ‡ΊπŸ‡ΈUnited States cboyden

    The patch fixes things when the operator is not exposed, but if it's exposed, there are JS errors in the console. It looks like the states that control the appearance of the single date field vs. the min/max fields used for Between operators are broken.

    Steps to reproduce:

    1. Download the latest version of Panopoly 3.0.x and apply the patch.
    2. Install the site with demo content.
    3. Log in as an administrator.
    4. Create a new Content page.
    5. Edit the Panopoly Demo view to add an exposed date filter: Filter on the Authored On date of the content item. Choose the operator "greater or equal" as the default. Also expose the operator.
    6. Save the view.
    7. In the Views UI preview, set the exposed sorts to Post date, Desc. You should see your new content item at the top of the list, followed by 2 of the 3 demo items.
    8. Change the operator in the Views UI preview to "Is between."
    9. Note the single date field disappears and is replaced by a set of Min and Max fields.
    10. In the Min filter field, enter the date "2024-06-01"; in the Max filter field, enter "2024-08-01" (or a date later than today's) and click Apply.
    11. Because the authored on dates of the demo content are quite old (2012), you will only see the item you just created in the preview. This is the expected behavior.
    12. Save the View.
    13. Clear caches.
    14. Create a new Landing page.
    15. Customize the page, adding the Demo view to the layout.
    16. Make sure the operator dropdown shows "Is greater than"
    17. Note the "Authored on" fieldset shows the operator dropdown and also three fields: one unlabeled, one labeled Min, and one labeled Max.
    18. Open the browser's JS console.
    19. In the block config, change the Sort order control back and forth between Ascending and Descending a few times.
    20. Note there is a JS error in the console each time you change the sort order.
    21. Change the operator to "Is between."
    22. Clear the browser JS console.
    23. In the block config, change the Sort order control back and forth between Ascending and Descending a few times.
    24. Note there are no JS errors this time.
    25. Note there is no content displayed in the Preview.
    26. In the Min filter field, enter the date "2024-06-01"; in the Max filter field, enter "2024-08-01" (or a date later than today's) and click Preview.
    27. Note there is still no content displayed in the Preview.

    The JS error is:

    message: 
    "An AJAX HTTP error occurred.
    HTTP Result Code: 200
    Debugging information follows.
    Path: /layout_builder/add/block/overrides/node.6/0/contentmain/views_block%3Apanopoly_demo-demo_listing?_wrapper_format=drupal_dialog&ajax_form=1
    StatusText: parsererror
    ResponseText: TypeError: strtotime(): Argument #1 ($datetime) must be of type string, array given in strtotime() (line 185 of /var/www/html/web/core/modules/views/src/Plugin/views/filter/Date.php)."
    name: "AjaxError"
    stack: "Error
    at https://panopoly.localhost/core/misc/ajax.js?v=10.3.0:196:32
    at https://panopoly.localhost/core/misc/ajax.js?v=10.3.0:1921:3"
    
  • πŸ‡ΊπŸ‡ΈUnited States cboyden

    See screenshot of fieldset with three date fields where only one should be available:

  • Status changed to Needs review 4 months ago
  • πŸ‡ΊπŸ‡ΈUnited States dsnopek USA

    Here's a new patch that fixes the issues described in #3 in my testing. Please let me know if it works for you!

  • Status changed to Needs work 4 months ago
  • πŸ‡ΊπŸ‡ΈUnited States cboyden

    The latest patch fixes the states and the JS error, but there's one more thing. Depending on which operator is chosen, the view is filtered, but I wouldn't expect any filter to take effect until you enter a date.

    Steps to reproduce:

    1. Download the latest version of Panopoly 3.0.x and apply the latest patch.
    2. Install the site with demo content.
    3. Log in as an administrator.
    4. Edit the Panopoly Demo view to add an exposed date filter: Filter on the Authored On date of the content item. Choose the operator "Is less than" as the default. Also expose the operator.
    5. Save the view.
    6. In the Views UI preview, note that there are three items displayed.
    7. Change the operator in the Views UI preview to "Is between" and click Apply.
    8. Note that there are still three items displayed. This is the expected behavior; you haven't entered a date for the filter, so all content is still showing.
    9. Clear caches.
    10. Create a new Landing page.
    11. Customize the page, adding the Demo view to the layout.
    12. Make sure the operator dropdown shows "Is less than."
    13. Note the preview shows no items.
    14. Change the operator to "Is greater than."
    15. Note the preview shows 3 items.
    16. Change the operator to "Is less than or equal to."
    17. Note the preview shows no items.
    18. Save the block and save the layout.
    19. Note there are no items displayed in the view.

    Expected behavior: Regardless of the operator dropdown, the block preview should show all otherwise-unfiltered items until the editor puts in their date(s).

  • Status changed to Needs review 4 months ago
  • πŸ‡ΊπŸ‡ΈUnited States dsnopek USA

    Thanks! Here's a patch that fixes the issue described in #6 in my testing.

    However, it's a rather large change, so this really needs to be tested quite a bit!

  • Status changed to Needs work 4 months ago
  • πŸ‡ΊπŸ‡ΈUnited States cboyden

    I think this is almost there. When following the steps in #6 while using the previous patch, the initial preview in the choose block dialog and in the configure dialog doesn't show any items. When you make any change to the config that causes preview to fire again, the items display as expected.

    Starting just after step 10 above:

    1. Click the Layout tab.
    2. Click Add block.
    3. Select the Demo category.
    4. Click List of demo items, to show the preview in the "Choose a block" dialog.
    5. Note there are no items displayed.
    6. Click Add.
    7. In the Configure dialog preview, note there are no items displayed.
    8. Select the "Featured" radio button from the "View mode" section.
    9. Note there are three items displayed in the preview.
  • πŸ‡ΊπŸ‡ΈUnited States cboyden

    Also, there are a bunch of PHP warnings in the log.

    For example:

    Warning: Undefined array key "created_op" in Drupal\views\Plugin\views\filter\Date->acceptExposedInput() (line 144 of /var/www/html/web/core/modules/views/src/Plugin/views/filter/Date.php)
    #0 /var/www/html/web/core/includes/bootstrap.inc(166): _drupal_error_handler_real(2, 'Undefined array...', '/var/www/html/w...', 144)
    #1 /var/www/html/web/core/modules/views/src/Plugin/views/filter/Date.php(144): _drupal_error_handler(2, 'Undefined array...', '/var/www/html/w...', 144)
    #2 /var/www/html/web/profiles/contrib/panopoly/modules/panopoly/panopoly_magic/src/Plugin/views/display/MagicBlock.php(626): Drupal\views\Plugin\views\filter\Date->acceptExposedInput(Array)
    #3 /var/www/html/web/profiles/contrib/panopoly/modules/panopoly/panopoly_magic/src/Plugin/Block/MagicViewsBlock.php(21): Drupal\panopoly_magic\Plugin\views\display\MagicBlock->preBlockBuild(Object(Drupal\panopoly_magic\Plugin\Block\MagicViewsBlock))
    #4 /var/www/html/web/core/modules/layout_builder/src/EventSubscriber/BlockComponentRenderArray.php(106): Drupal\panopoly_magic\Plugin\Block\MagicViewsBlock->build()
    #5 [internal function]: Drupal\layout_builder\EventSubscriber\BlockComponentRenderArray->onBuildRender(Object(Drupal\layout_builder\Event\SectionComponentBuildRenderArrayEvent), 'section_compone...', Object(Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher))
    #6 /var/www/html/web/core/lib/Drupal/Component/EventDispatcher/ContainerAwareEventDispatcher.php(111): call_user_func(Array, Object(Drupal\layout_builder\Event\SectionComponentBuildRenderArrayEvent), 'section_compone...', Object(Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher))
    #7 /var/www/html/web/core/modules/layout_builder/src/SectionComponent.php(90): Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch(Object(Drupal\layout_builder\Event\SectionComponentBuildRenderArrayEvent), 'section_compone...')
    #8 /var/www/html/web/modules/contrib/page_manager/src/Plugin/DisplayVariant/LayoutBuilderDisplayVariant.php(107): Drupal\layout_builder\SectionComponent->toRenderArray(Array, false)
    #9 /var/www/html/web/modules/contrib/page_manager/src/Entity/PageVariantViewBuilder.php(29): Drupal\page_manager\Plugin\DisplayVariant\LayoutBuilderDisplayVariant->build()
    #10 /var/www/html/web/core/lib/Drupal/Core/Entity/Controller/EntityViewController.php(134): Drupal\page_manager\Entity\PageVariantViewBuilder->view(Object(Drupal\page_manager\Entity\PageVariant), 'full')
    #11 [internal function]: Drupal\Core\Entity\Controller\EntityViewController->view(Object(Drupal\page_manager\Entity\PageVariant), 'full')
    #12 /var/www/html/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(123): call_user_func_array(Array, Array)
    #13 /var/www/html/web/core/lib/Drupal/Core/Render/Renderer.php(638): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}()
    #14 /var/www/html/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(124): Drupal\Core\Render\Renderer->executeInRenderContext(Object(Drupal\Core\Render\RenderContext), Object(Closure))
    #15 /var/www/html/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(97): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(Array, Array)
    #16 /var/www/html/vendor/symfony/http-kernel/HttpKernel.php(181): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}()
    #17 /var/www/html/vendor/symfony/http-kernel/HttpKernel.php(76): Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object(Symfony\Component\HttpFoundation\Request), 1)
    #18 /var/www/html/web/core/lib/Drupal/Core/StackMiddleware/Session.php(53): Symfony\Component\HttpKernel\HttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
    #19 /var/www/html/web/core/lib/Drupal/Core/StackMiddleware/KernelPreHandle.php(48): Drupal\Core\StackMiddleware\Session->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
    #20 /var/www/html/web/core/lib/Drupal/Core/StackMiddleware/ContentLength.php(28): Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
    #21 /var/www/html/web/core/modules/page_cache/src/StackMiddleware/PageCache.php(106): Drupal\Core\StackMiddleware\ContentLength->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
    #22 /var/www/html/web/core/modules/page_cache/src/StackMiddleware/PageCache.php(85): Drupal\page_cache\StackMiddleware\PageCache->pass(Object(Symfony\Component\HttpFoundation\Request), 1, true)
    #23 /var/www/html/web/core/lib/Drupal/Core/StackMiddleware/ReverseProxyMiddleware.php(48): Drupal\page_cache\StackMiddleware\PageCache->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
    #24 /var/www/html/web/core/lib/Drupal/Core/StackMiddleware/NegotiationMiddleware.php(51): Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
    #25 /var/www/html/web/core/lib/Drupal/Core/StackMiddleware/AjaxPageState.php(36): Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
    #26 /var/www/html/web/core/lib/Drupal/Core/StackMiddleware/StackedHttpKernel.php(51): Drupal\Core\StackMiddleware\AjaxPageState->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
    #27 /var/www/html/web/core/lib/Drupal/Core/DrupalKernel.php(741): Drupal\Core\StackMiddleware\StackedHttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
    #28 /var/www/html/web/index.php(19): Drupal\Core\DrupalKernel->handle(Object(Symfony\Component\HttpFoundation\Request))
    #29 {main}
    

    There are 4 other PHP warnings about the same function Drupal\views\Plugin\views\filter\Date->acceptExposedInput(), and then there's a deprecated function notice:

    Deprecated function: strtotime(): Passing null to parameter #1 ($datetime) of type string is deprecated in Drupal\views\Plugin\views\filter\Date->opSimple() (line 185 of /var/www/html/web/core/modules/views/src/Plugin/views/filter/Date.php)
    #0 /var/www/html/web/core/includes/bootstrap.inc(166): _drupal_error_handler_real(8192, 'strtotime(): Pa...', '/var/www/html/w...', 185)
    #1 [internal function]: _drupal_error_handler(8192, 'strtotime(): Pa...', '/var/www/html/w...', 185)
    #2 /var/www/html/web/core/modules/views/src/Plugin/views/filter/Date.php(185): strtotime(NULL, 0)
    #3 /var/www/html/web/core/modules/views/src/Plugin/views/filter/NumericFilter.php(339): Drupal\views\Plugin\views\filter\Date->opSimple('node_field_data...')
    #4 /var/www/html/web/core/modules/views/src/ViewExecutable.php(1432): Drupal\views\Plugin\views\filter\NumericFilter->query(false)
    #5 /var/www/html/web/core/modules/views/src/ViewExecutable.php(1321): Drupal\views\ViewExecutable->_build('filter')
    #6 /var/www/html/web/core/modules/views/src/ViewExecutable.php(1450): Drupal\views\ViewExecutable->build(NULL)
    #7 /var/www/html/web/core/modules/views/src/ViewExecutable.php(1513): Drupal\views\ViewExecutable->execute(NULL)
    #8 /var/www/html/web/core/modules/views/src/Plugin/views/display/Block.php(133): Drupal\views\ViewExecutable->render()
    #9 /var/www/html/web/core/modules/views/src/ViewExecutable.php(1689): Drupal\views\Plugin\views\display\Block->execute()
    #10 /var/www/html/web/core/modules/views/src/Element/View.php(81): Drupal\views\ViewExecutable->executeDisplay('demo_listing', Array)
    #11 /var/www/html/web/profiles/contrib/panopoly/modules/panopoly/panopoly_magic/src/Plugin/Block/MagicViewsBlock.php(78): Drupal\views\Element\View::preRenderViewElement(Array)
    #12 /var/www/html/web/core/modules/layout_builder/src/EventSubscriber/BlockComponentRenderArray.php(106): Drupal\panopoly_magic\Plugin\Block\MagicViewsBlock->build()
    #13 [internal function]: Drupal\layout_builder\EventSubscriber\BlockComponentRenderArray->onBuildRender(Object(Drupal\layout_builder\Event\SectionComponentBuildRenderArrayEvent), 'section_compone...', Object(Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher))
    #14 /var/www/html/web/core/lib/Drupal/Component/EventDispatcher/ContainerAwareEventDispatcher.php(111): call_user_func(Array, Object(Drupal\layout_builder\Event\SectionComponentBuildRenderArrayEvent), 'section_compone...', Object(Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher))
    #15 /var/www/html/web/core/modules/layout_builder/src/SectionComponent.php(90): Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch(Object(Drupal\layout_builder\Event\SectionComponentBuildRenderArrayEvent), 'section_compone...')
    #16 /var/www/html/web/modules/contrib/page_manager/src/Plugin/DisplayVariant/LayoutBuilderDisplayVariant.php(107): Drupal\layout_builder\SectionComponent->toRenderArray(Array, false)
    #17 /var/www/html/web/modules/contrib/page_manager/src/Entity/PageVariantViewBuilder.php(29): Drupal\page_manager\Plugin\DisplayVariant\LayoutBuilderDisplayVariant->build()
    #18 /var/www/html/web/core/lib/Drupal/Core/Entity/Controller/EntityViewController.php(134): Drupal\page_manager\Entity\PageVariantViewBuilder->view(Object(Drupal\page_manager\Entity\PageVariant), 'full')
    #19 [internal function]: Drupal\Core\Entity\Controller\EntityViewController->view(Object(Drupal\page_manager\Entity\PageVariant), 'full')
    #20 /var/www/html/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(123): call_user_func_array(Array, Array)
    #21 /var/www/html/web/core/lib/Drupal/Core/Render/Renderer.php(638): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}()
    #22 /var/www/html/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(124): Drupal\Core\Render\Renderer->executeInRenderContext(Object(Drupal\Core\Render\RenderContext), Object(Closure))
    #23 /var/www/html/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(97): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(Array, Array)
    #24 /var/www/html/vendor/symfony/http-kernel/HttpKernel.php(181): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}()
    #25 /var/www/html/vendor/symfony/http-kernel/HttpKernel.php(76): Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object(Symfony\Component\HttpFoundation\Request), 1)
    #26 /var/www/html/web/core/lib/Drupal/Core/StackMiddleware/Session.php(53): Symfony\Component\HttpKernel\HttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
    #27 /var/www/html/web/core/lib/Drupal/Core/StackMiddleware/KernelPreHandle.php(48): Drupal\Core\StackMiddleware\Session->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
    #28 /var/www/html/web/core/lib/Drupal/Core/StackMiddleware/ContentLength.php(28): Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
    #29 /var/www/html/web/core/modules/page_cache/src/StackMiddleware/PageCache.php(106): Drupal\Core\StackMiddleware\ContentLength->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
    #30 /var/www/html/web/core/modules/page_cache/src/StackMiddleware/PageCache.php(85): Drupal\page_cache\StackMiddleware\PageCache->pass(Object(Symfony\Component\HttpFoundation\Request), 1, true)
    #31 /var/www/html/web/core/lib/Drupal/Core/StackMiddleware/ReverseProxyMiddleware.php(48): Drupal\page_cache\StackMiddleware\PageCache->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
    #32 /var/www/html/web/core/lib/Drupal/Core/StackMiddleware/NegotiationMiddleware.php(51): Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
    #33 /var/www/html/web/core/lib/Drupal/Core/StackMiddleware/AjaxPageState.php(36): Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
    #34 /var/www/html/web/core/lib/Drupal/Core/StackMiddleware/StackedHttpKernel.php(51): Drupal\Core\StackMiddleware\AjaxPageState->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
    #35 /var/www/html/web/core/lib/Drupal/Core/DrupalKernel.php(741): Drupal\Core\StackMiddleware\StackedHttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
    #36 /var/www/html/web/index.php(19): Drupal\Core\DrupalKernel->handle(Object(Symfony\Component\HttpFoundation\Request))
    #37 {main}
    
  • Status changed to Needs review 3 months ago
  • πŸ‡ΊπŸ‡ΈUnited States dsnopek USA

    Here's a patch that I think should fix the PHP warnings.

    However, related to this bit:

    When following the steps in #6 while using the previous patch, the initial preview in the choose block dialog and in the configure dialog doesn't show any items. When you make any change to the config that causes preview to fire again, the items display as expected.

    Unfortunately, I wasn't able to reproduce this: I'm seeing the preview with items in the choose block dialog, and in the configure dialog before making any changes.

  • πŸ‡ΊπŸ‡ΈUnited States cboyden

    Thanks, the patch is working and not creating any PHP notices. There is some extra stuff in it though, there are some panopoly_media files included in the patch.

  • Status changed to RTBC 3 months ago
  • πŸ‡ΊπŸ‡ΈUnited States dsnopek USA

    There is some extra stuff in it though, there are some panopoly_media files included in the patch.

    Eep! Sorry about that. It looks like some changes related to #3467225: Switch back to checkboxes on media library β†’ snuck in there.

    Here's a clean patch.

  • Status changed to Fixed 3 months ago
  • πŸ‡ΊπŸ‡ΈUnited States dsnopek USA
  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.71.5 2024