Timestamp field type uses numeric filter plugin rather than date filter plugin

Created on 19 March 2021, over 4 years ago
Updated 28 September 2023, almost 2 years ago

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, 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

🐛 Bug report
Status

Active

Version

9.5

Component
Views 

Last updated 9 days ago

Created by

Live updates comments and jobs are added and updated live.
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.

  • 🇮🇳India vivek.kekare

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

  • 🇳🇿New Zealand john pitcairn

    Should probably move this to 11.x now.

  • 🇬🇧United Kingdom niklp Nottingham

    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... :/

  • 🇺🇸United States jphelan

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

  • 🇫🇷France PhilY 🇪🇺🇫🇷 Paris, France

    #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.

  • 🇺🇸United States smustgrave

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

  • 🇺🇸United States jphelan

    I've opened an MR.

  • Pipeline finished with Success
    10 months ago
    Total: 458s
    #343682
  • 🇩🇪Germany u.kurilla

    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! :-)

  • First commit to issue fork.
  • Pipeline finished with Success
    9 months ago
    #366208
  • Pipeline finished with Success
    9 months ago
    Total: 1028s
    #366212
  • 🇬🇧United Kingdom oily Greater London

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

  • 🇬🇧United Kingdom oily Greater London
  • Status changed to Needs work 8 months ago
  • 🇮🇳India joshua1234511 Goa

    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...


  • 🇫🇮Finland Tuuuukka

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

  • 🇦🇺Australia gargsuchi Melbourne

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

  • 🇮🇳India joshua1234511 Goa

    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

  • 🇮🇳India niranjan_panem Gurugram

    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

    After

  • 🇺🇸United States dcam

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

  • Merge request !11895Split the code and added a test → (Open) created by dcam
  • Pipeline finished with Failed
    5 months ago
    Total: 185s
    #478085
  • Pipeline finished with Success
    5 months ago
    Total: 618s
    #478086
  • 🇺🇸United States dcam

    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.

  • 🇺🇸United States smustgrave

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

  • 🇺🇸United States dcam

    Screenshots added.

  • 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.

  • 🇺🇸United States dcam

    Rebased

  • 🇺🇸United States dcam

    Sorry, this was in NR status.

  • Pipeline finished with Success
    4 months ago
    Total: 723s
    #494941
  • 🇺🇸United States smustgrave

    Left a comment on the MR

  • 🇺🇸United States smustgrave

    Left comments on the MR.

  • Pipeline finished with Failed
    2 months ago
    Total: 131s
    #543160
  • Pipeline finished with Failed
    2 months ago
    Total: 123s
    #543225
  • Pipeline finished with Success
    2 months ago
    Total: 414s
    #543230
  • 🇺🇸United States dcam

    Feedback was addressed.

  • 🇺🇸United States smustgrave

    Feedback appears to be have been addressed thanks @dcam!

  • 🇺🇸United States xjm

    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.

  • 🇺🇸United States xjm

    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?

  • Pipeline finished with Failed
    about 2 months ago
    Total: 176s
    #558130
  • Pipeline finished with Failed
    about 2 months ago
    Total: 244s
    #558146
  • Pipeline finished with Success
    about 2 months ago
    #558149
  • 🇺🇸United States dcam

    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 .

Production build 0.71.5 2024