Postponed
Project:
Drupal core
Version:
main
Component:
datetime.module
Priority:
Major
Category:
Task
Assigned:
Unassigned
Reporter:
Created:
15 May 2015 at 19:33 UTC
Updated:
10 Feb 2025 at 03:29 UTC
Jump to comment: Most recent
#1838242: Provide Views integration for datetime field uses hook_field_views_data() to add proper views integration. However, due to limitations of the Views API, this mechanism does not work for base table fields.
Datetime is the *only* module that adds field types, apparently (the rest have moved into \Drupal\Core), so until #2337515: Allow @FieldType to customize views data is in this might be overly complicated.
TBD
Find a solution
N/A
N/A
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
Comment #1
jibran#2548395: Provide views relationships for DER base fields is also related here.
Comment #2
jibranI think it's at least a major.
Comment #5
bkosborneDang, this just bit me.
Anyone know if there's a workaround for this, perhaps something I can use in a custom module just to tell views to use the proper views filter plugin for datetime base fields?
Comment #6
bkosborneThe existingDrupal\datetime\Plugin\views\filter\Datefilter plugin usesDrupal\views\FieldAPIHandlerTraitto determine if the underlying field storage definition is configured as a "date" or a "datetime" element. I think this needs to be refactored before the filter could be used for base field definitions.Nevermind, this base field definitions work just fine, I just had an error elsewhere that led me to thing otherwise
Comment #8
dom. commentedIs there a workaround about this so views can use datetime exposed filter as date ?
Comment #9
jibran@Dom. you can implement hook_views_data the basefield for now just like http://cgit.drupalcode.org/dynamic_entity_reference/tree/tests/modules/d....
Postpone on #2337515: Allow @FieldType to customize views data.
Comment #11
grubshka_v2 commented@jibran, could you explain what should be put in views data ?
I'm trying to use the calendar module with a datetime field defined in a custom entity.
I manage to create the calendar template by creating this views data :
However the calendar is not working and I get this error :
So it make me think my views data is not well defined.
Comment #12
luksak@grubshka_v2 i am also trying to get this working. I guess this is the starting point:
The problem is that this is for FieldStorageConfig fields, not for base fields. Right now i am also trying to build the array on my own, but without success so far... Does anyone know a module that uses a datetime base field and exposes it for views?
Comment #13
luksakOk, I managed to solve this for my entity type:
This goes into the getViewsData() method of the views_data handler of the entity.
Comment #16
mariaioann commentedA workaround for it could be to use a contextual filter as query parameter and in the views exposed form alter to add a select with the same query parameter as an exposed filter. For more details see here: http://drupalsun.com/fabrice/2017/10/19/filter-content-year-views-drupal-8
Comment #17
rfulcher commentedI have tried to do #13 and can't get the getViewsData() routine to run, I have tried everything. I have a custom entity and the stub files for this entity were created by Drupal console. The function getViewsData() is not being called at all.
Comment #18
sam152 commentedThis was super tricky, I managed to get some basic integration for a datetime range field using the following snippet in my custom views data handler, where
date_timewas the name of by base field:Comment #19
joachim commentedWould #2930736: EntityViewsData assumes BaseFieldDefinitions where it should use FieldDefinitionInterface help at all with this?
If #2337515: Allow @FieldType to customize views data doesn't look like it will land in time for Drupal 9, we should take advantage of the BC break, and change hook_field_views_data()'s parameter to be a FieldStorageDefinitionInterface. We could then invoke it from EntityViewsData with base fields.
Also, taking the title from #2903770: Base fields miss out on Views data from hook_field_views_data() which was closed as a duplicate, which is MUCH clearer and explains the wider problem.
Comment #20
berdir> If #2337515: Allow @FieldType to customize views data doesn't look like it will land in time for Drupal 9
Why not? What the issue requires is someone dedicating some time to it and getting it started, it should not be that complicated.
> we should take advantage of the BC break, and change hook_field_views_data()'s parameter to be a FieldStorageDefinitionInterface.
There is no BC break with D9, the idea is that the only thing we do between 8.9 and 9.0 is remove *already* deprecated code, so that modules can be compatible with both versions at the same time. That's not possible with a type hint change like that, which is why we need a new way to integrate that.
Comment #21
joachim commented> There is no BC break with D9
I know that's what's been announced, but I don't buy it. We need to *sometimes* be able to break BC. There are some things that aren't possible without a BC break.
Comment #22
the_glitch commentedPP
Comment #25
duneblAs it looks like it is difficult to land, I propose to create an help topic on how to integrate some most used base fields type into views.
This will ease the entity creation for a lot of people.
Comment #26
joachim commentedThis is probably soft-blocked on #3116481: Convert EntityViewsDataTest from a unit test to a kernel test like a lot of things to do with Views Data.
Comment #27
duneblDon't you think we need a clear documentation for developers without experiences in views in order to help them to create entities with base fields integrated in views?
This is a 6 year old thread... maybe we will wait one or two years...
Comment #28
joachim commentedYes, of course, we need docs for workarounds. I meant that the fix is blocked by the tests, not the documentation. Sorry for the confusion!
Comment #29
duneblthank you, I have added the first example with a datetime field, but I am stuck on how to override properly
EntityViewsDataAny help appreciated
https://www.drupal.org/project/drupal/issues/3213021#comment-14097782
Comment #34
damienmckennaComment #35
psf_ commentedHi,
Notes that may light somebody.
I found why the default type is string to datetime basefields. In web/core/modules/views/views.views.inc we have a switch that set it how string:
The datetime field type must not have a valid filter handler defined.
In datetime_type_field_views_data_helper(), in datetime core module, we have a comment that say:
Comment #36
psf_ commentedI think that the problem is the base fields don't have Field Storage.
If I execute:
$fields don't have any base fields.
I fix my custom entity fields filters with two hooks:
Comment #39
maskedjellybeanI realize the status of this is postponed, but the fix for issue as I understand it seemed doable so I opened a MR.
This is what I think the goal of this issue is (correct me if I'm wrong):
If you have a base table (in Drupal speak, apparently this means a table unrelated to any entity) that has a datetime field/column, it should be filterable in views the same way that a timestamp integer field would be. Additionally the datetime field should be displayed in the view the same way that a timestamp field can be displayed.
If that's correct, then the MR does the trick for me.
For example, given this:
I can make a view of "An example table", and add a "A datetime field/column" field and filter.
Comment #40
berdir> If you have a base table (in Drupal speak, apparently this means a table unrelated to any entity)
That is not what base table means. Base table and base field specifically refers to entity base fields to goal is to have them declared automatically viewing any manual views data hooks.
Comment #41
maskedjellybeanAh perhaps I'm in the wrong place then. I'll open a different issue. The problem I'm trying to solve is:
1. Views does not support filtering on a datetime database field.
2. Views does not support displaying a datetime database field.