Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Problem/Motivation
#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.
Proposed resolution
TBD
Remaining tasks
Find a solution
User interface changes
N/A
API changes
N/A
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\Date
filter plugin usesDrupal\views\FieldAPIHandlerTrait
to 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. CreditAttribution: Dom. as a volunteer and at ACINO 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 CreditAttribution: 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
Lukas von Blarer@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
Lukas von BlarerOk, 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 CreditAttribution: 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 CreditAttribution: 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 CreditAttribution: Sam152 as a volunteer and at PreviousNext 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_time
was the name of by base field:Comment #19
joachim CreditAttribution: joachim as a volunteer 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 CreditAttribution: joachim as a volunteer 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 CreditAttribution: 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 CreditAttribution: joachim as a volunteer 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 CreditAttribution: joachim as a volunteer 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
EntityViewsData
Any help appreciated
https://www.drupal.org/project/drupal/issues/3213021#comment-14097782
Comment #34
DamienMcKenna