Problem/Motivation
There are several attempts to provide the same views integration to base fields then the one we can get by using the Field UI.
Most promising seems to be #2337515: Allow @FieldType to customize views data
Until this nice to have feature will be in, we could create a documentation on this topic.
I think we need it because I could not find any step by step tutorial on the web and I found a lot of people stuck by this issue.
Most of them turn back and remove the base fields and replace them by config fields. (which sometimes is a poor design)
My idea is to have an example for most used base fields: datetime, reference_field, integer, ...
Comments
Comment #2
DuneBLLet first start with an exemple for a datetime field used in a BankOperation entity which should store bank operation data
=>I need help to extends properly
EntityViewsData
1-The base field is created as follow in the
modules/custom/bank_operation/src/Entity/BankOperation.php
Notes:
a- The
views_data
key is pointing to a class which will be created in step 2b- The operation_date base field have the
datetime
type2- To integrate with Views we must extends and use our own EntityViewsData class (mandatory until #2337515: Allow @FieldType to customize views data is landed)
Extract from
modules/custom/bank_operation/src/BankOperationViewsData.php
:Note:
This class is created according to the value of the
views_data
key used in the entity annotation.Comment #4
fvictoire CreditAttribution: fvictoire commentedI am not sure it answers to all your question but you can find documentation with the hook_views_data (in api.drupal.org). The getViewsData method of class EntityViewsData returns the same format data like this hook.
Comment #7
ptmkenny CreditAttribution: ptmkenny commentedTo update the example in #2 point 2, the code should be this:
Based on this comment