Views filter for core dates not working

Created on 25 October 2024, about 2 months ago

Problem/Motivation

When having a core Date field (not a timestamp, just "Date"), Views filter don't work for them when Smart Date is installed.

Steps to reproduce

  • Have a fresh core installation using standard profile
  • Install Smart Date
  • Go to content types -> basic page -> add a new date field "field_mydate", its configuration doesn't matter
  • Create one content of type basic page and set a date value, e.g. 2024-10-25
  • Create a new Views configuration listing content of type basic page
  • Add a filter for "field_mydate", choose "greater than or equal to" as operator, and paste in 2024-10-24

Expected: The view lists the created node, because it has value 2024-10-25 which is greater than 2024-10-24.
Actual result: The view lists nothing.

Taking a look into the built Views query gives a hint to the reason of the problem:

SELECT "node_field_data"."created" AS "node_field_data_created", "node_field_data"."nid" AS "nid"
FROM
{node_field_data} "node_field_data"
LEFT JOIN {node__field_mydate} "node__field_mydate" ON node_field_data.nid = node__field_mydate.entity_id AND node__field_mydate.deleted = '0'
WHERE ("node_field_data"."status" = '1') AND ("node_field_data"."type" IN ('page')) AND ((node__field_mydate.field_mydate_value >= 1672527600))
ORDER BY "node_field_data_created" DESC
LIMIT 11 OFFSET 0

The particular part ((node__field_mydate.field_mydate_value >= 1672527600)) cannot work because date values are being stored as strings, formatted as "Y-m-d". They are not being stored as timestamps.

Smart Date replaces the Views filter plugin by its own:

<?php
/**
 * Implements hook_views_plugins_filter_alter().
 *
 * Replace the core date views filter with a version supporting granularity.
 */
function smart_date_views_plugins_filter_alter(array &$plugins) {
  if (isset($plugins['date'])) {
    $plugins['date']['class'] = 'Drupal\smart_date\Plugin\views\filter\Date';
    $plugins['date']['provider'] = 'smart_date';
  }
}
?>

.. whereas Smart Date's filter plugin extends from core date class Drupal\views\Plugin\views\filter\Date but usually this should be handled by Drupal\datetime\Plugin\views\filter\Date. The datetime class implementation also properly handles the "offset" option whereas Smart Date's implementation does not support it entirely.

Proposed resolution

TBD

Remaining tasks

TBD

User interface changes

API changes

Data model changes

🐛 Bug report
Status

Active

Version

4.2

Component

Code

Created by

🇩🇪Germany mxh Offenburg

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

Comments & Activities

Production build 0.71.5 2024