Problem/Motivation

When using a custom timestamp field to filter a view the filter type used is the numeric filter, rather than the expected date filter, this prevents the view from being filtered by an offset of the timestamp, and also results in an exposed filter on a timestamp field expecting the user to input a valid unix timestamp value, rather than being presented with a date picker element.

note that this only applies to custom fields of the type timestamp, fields such as creayed date and updated date, which are stored in the db as timestamps are handled as expected, using the date filter plugin.

Steps to reproduce

Create a content type with a custom field of type timestamp Add date-time field with timestamp formatter
Or add a custom field of type timestamp to the user object, populate the field with data.

create a view displaying the the content type, or users, and add a filter on the created timestamp field, the filter interface will use the numeric plugin, rather than the date plugin, removing the option to use an offset value in the filter, and displaying the exposed filter as a text input rather than as a date picker.

I've checked this on a simplytest.me instance on drupal 8.9.13 with no additional modules installed to ensure that the issue is present in a base drupal install

Proposed resolution

Ensure that the timestamp field type uses the date filter rather than the numeric filter.

User interface changes

in the views admin the user will be able to select to use an offset, rather than a specific date, where the filter is exposed a date picker will be used rather than an standard text input

Before:

After:

Issue fork drupal-3204558

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

jkeasley created an issue. See original summary.

claudiu.cristea’s picture

Status: Active » Closed (duplicate)

This seems a duplicate of #2648950: [PP-2] Use form element of type date instead textfield when selecting a date in an exposed filter. Closing this, but feel free to reopen if you think that this is a different issue,

jkeasley’s picture

Status: Closed (duplicate) » Active

Reopening this ticket as it seems to be relating to a different issue to the ticket referred to when marking as duplicate.

The issue referred to seems to relate to the date plugin not providing the expected datepicker elements, while the issue here is that custom timestamp fields use the incorrect plugin 'numeric', rather than the 'date' filter plugin.

note that on testing and reviewing the patches from the referenced issue I noted that the tests for the timestamp filter handling referenced in the patches would not catch this issue, as they test the functionality using filters applied to the created_date field, which is filtered using the expected date filter.

While created_date and updated_date use the expected date filter when adding a view filter to those fields, a custom created timestamp field does not.

Version: 8.9.x-dev » 9.2.x-dev

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.15 was released on June 1st, 2022 and is the final full bugfix release for the Drupal 9.3.x series. Drupal 9.3.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.4.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

tuuuukka’s picture

In https://git.drupalcode.org/project/drupal/-/blob/9.5.x/core/modules/view... the filter is set to date, which is correct. But this is later overridden in https://git.drupalcode.org/project/drupal/-/blob/9.5.x/core/modules/view... ($attributes['type'] is int) and the filter is changed to numeric.

As a quick test I did this:

diff --git a/core/modules/views/views.views.inc b/core/modules/views/views.views.inc
index de79773321..9c7fe8f6e6 100644
--- a/core/modules/views/views.views.inc
+++ b/core/modules/views/views.views.inc
@@ -605,6 +605,9 @@ function views_field_default_views_data(FieldStorageConfigInterface $field_stora
         if ($field_storage->getType() == 'boolean') {
           $filter = 'boolean';
         }
+        elseif ($field_storage->getType() == 'timestamp') {
+          $filter = 'date';
+        }
         break;
 
       case 'blob':

..and it seems to work, so I think that's where it gets twisted.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.9 was released on December 7, 2022 and is the final full bugfix release for the Drupal 9.4.x series. Drupal 9.4.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.5.x-dev branch from now on, and new development or disruptive changes should be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

vivek.kekare’s picture

This issue present in Drupal 10.0.x as well. Any update expected for this issue in core itself?

johnpitcairn’s picture

Version: 9.5.x-dev » 11.x-dev

Should probably move this to 11.x now.

niklp’s picture

I tried the hack in #7 but it didn't work for me under 10.x

I have had some success in combating the problem by using https://www.drupal.org/project/better_exposed_filters but it's not perfect.

This is a very annoying bug for filtering dates (manifesting as timestamps) by exposed filter... :/

jphelan’s picture

#7 works with me in conjunction with the date_popup module.

phily’s picture

Status: Active » Needs review
StatusFileSize
new504 bytes

#7 works for me using Drupal 10.3.6, patch is attached.
It should work with Drupal 11 as the views.views.inc file is the same for these 2 releases.

smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs tests

Thanks for working on this but issues should be in MRs vs patches and will need test coverage.

jphelan’s picture

I've opened an MR.

u.kurilla’s picture

Like #13 the patch does the job for me in Drupal 10.3.6.
Thanks for the MR #16
I was happy when I found this issue and its solution - after a couple of hours! :-)

oily made their first commit to this issue’s fork.

ironnuts’s picture

Minor refactor of MR for code readability. Rebased MR. Test coverage still required.

ironnuts’s picture

Issue summary: View changes

Added a screenshot to the steps to reproduce. It shows the form where the date time field with timestamp formatter is selected. The field is being added to the 'Article' content type.

joshua1234511’s picture

This MR is no longer necessary as the required changes have already been addressed in the source branch. The fix for the issue can be found in EntityViewsData.php.

https://git.drupalcode.org/project/drupal/-/blob/11.x/core/modules/views...

Patch
Core

tuuuukka’s picture

#21 if you're sure it's been fixed, this issue status can be set to fixed or closed..?

gargsuchi’s picture

Can someone please test issue against 11.x?
Also, do we still need tests for this?

joshua1234511’s picture

Tested the scenario again 11
- Steps taken
-- Created a content type and added a custom field of type timestamp.
-- Created a view that displays the content type
-- Added a filter for the created timestamp field.
- Test Failed

Even after applying the change from this Drupal core commit, which sets the filter type to date, the mapping still does not work correctly for custom fields.
I am investigating further to resolve this issue.

Related issue: https://www.drupal.org/node/2337515

niranjan_panem’s picture

We can alter the numeric date field to actual date time field through drupal's hook_form_views_exposed_form_alter
below is the code using the hook

function views_form_views_exposed_form_alter(&$form, FormStateInterface $form_state, $form_id){
  if($form_id=="views_exposed_form"){
    $form["field_published_date_time_stamp_value"]["#type"]='datetime';
  }
}

Below are the screen shots of before and after applying the drupal hook

Before

before altering field

After
After Altering field

dcam changed the visibility of the branch 3204558-timestamp-field-type to hidden.

dcam’s picture

Status: Needs work » Needs review
Issue tags: -Drupal 10 compatibility, -Needs tests

I closed the old MR because the .inc file it was modifying has been changed into a service class. There's no point in rebasing that.

I started to write a test and then decided "There is no way I'm writing a Unit test that covers an entire, complicated, nearly 500 line function." So I split the offending code out into its own function in the same class. I tried to leave it as unmolested as possible - no arbitrary code formatting changes. That way it should be easier to verify that not much changed.

smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs screenshots

With UI changes need before/after screenshots added to the summary, depending how big the change may need UX sign off or a CR

dcam’s picture

Issue summary: View changes
Status: Needs work » Needs review
Issue tags: -Needs screenshots
StatusFileSize
new19.43 KB
new27.81 KB

Screenshots added.

needs-review-queue-bot’s picture

Status: Needs review » Needs work
StatusFileSize
new91 bytes

The Needs Review Queue Bot tested this issue. It no longer applies to Drupal core. 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.

dcam’s picture

Status: Needs work » Reviewed & tested by the community

Rebased

dcam’s picture

Status: Reviewed & tested by the community » Needs review

Sorry, this was in NR status.

smustgrave’s picture

Left a comment on the MR

smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs Review Queue Initiative

Left comments on the MR.

dcam’s picture

Status: Needs work » Needs review

Feedback was addressed.

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Feedback appears to be have been addressed thanks @dcam!

xjm’s picture

Title: Timestamp field type uses numeric filter plugin rather than date filter plugin » Timestamp field type uses numeric filter plugin rather than date filter plugin in Views
Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs change record

Posted some small stuff on the MR.

Meanwhile, is the IS up to date here? I didn't see anything in the patch that would result in the indicated UI. If it does, that's some serious magic. (I mean, it is Views, so maybe.)

Does this issue apply only to Views?

Finally, I think it would need a CR. Even if it's a bugfix, it may result in a behavior change in some circumstances.

xjm’s picture

Issue tags: +Needs followup

defaultFieldImplementation() is too big as it is. If we want this code to be more testable, then it needs to be broken up. This is just one step toward doing that. No, it isn't strictly necessary from a functionality standpoint. But I will argue that it's necessary for sanity.

Should we have a followup to do the rest?

dcam’s picture

Status: Needs work » Needs review

Meanwhile, is the IS up to date here? I didn't see anything in the patch that would result in the indicated UI.

Yes. The addition of the condition...

        elseif ($field_type == 'timestamp') {
          $filter = 'date';
        }

...enables the date filter for timestamps. I understand if that's hard to see because I moved all that code.

Does this issue apply only to Views?

I'm not sure what other part of Core this problem might apply to. If there is one, then I think that should be handled in a separate issue.

Finally, I think it would need a CR. Even if it's a bugfix, it may result in a behavior change in some circumstances.

I added a CR at https://www.drupal.org/node/3538264.

drumm’s picture

Issue tags: +affects drupal.org

We ran into this when we used a timestamp field and had a surprising deficit of functionality.

lostcarpark’s picture

I think it's confusing that relative dates work on Datetime fields, but not on Timestamps, so it would be great to get this over the line.

smustgrave’s picture

Issue tags: -Needs change record

Can the follow up mentioned be created

needs-review-queue-bot’s picture

Status: Needs review » Needs work
StatusFileSize
new91 bytes

The Needs Review Queue Bot tested this issue. It no longer applies to Drupal core. 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.

dcam’s picture

Status: Needs work » Needs review
Issue tags: -Needs followup

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.

dcam’s picture

Status: Needs review » Reviewed & tested by the community

I should have moved this back to RTBC when I created the follow-up issue.

dww’s picture

Status: Reviewed & tested by the community » Needs review
Issue tags: +Bug Smash Initiative
Related issues: +#2648950: [PP-2] Use form element of type date instead textfield when selecting a date in an exposed filter

Adding a monster related issue, and also a minor concern on the MR.

dcam’s picture

I don't have an answer for the question of whether we should change it for the argument as well. It's understandable why this issue has only been about the filter plugin. This is more of a user-facing issue. For the record, I couldn't find an existing issue about the argument plugin.

Considering that changing the argument plugin would increase the potential for disruption to existing views I'm not willing to make that call unilaterally. Tagging for subsystem maintainer review due to the question on the MR.

dww’s picture

#2544832: Views argument for the datetime module should use maths. is the closest I know of about the date argument question. Maybe we should indeed resurrect that from PMNMI and handle my concern over there.

Meanwhile, there have been a series of these "use a more useful filter plugin" issues. E.g. the linked one about dates, or #3347343: Add Views EntityReference filter to support better UX for exposed filters. There were all sorts of hassles with potential disruptions, what to do about existing views, etc, etc. I'm not clear why this issue can so easily change this, without similar concerns. 😅

graber’s picture

Title: Timestamp field type uses numeric filter plugin rather than date filter plugin in Views » Timestamp field type uses numeric filter and argument plugins rather than date in Views
Status: Needs review » Needs work
Issue tags: -Needs subsystem maintainer review

A few nits plus I agree with @dww that we should also fix the argument plugin here as it's a tiny change and that also touches the UI. Unfortunately at the moment the date filter argument doesn't support any additional input options (offset) as the filter does so no UI changes there really but maybe eventually as it should really for consistency so better to cover it early.
Removing the "Needs subsystem maintainer review" tag, I can review again if needed, hopefully VBO D8+ and date_filter modules creator will be enough ;)