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

DuneBL created an issue. See original summary.

DuneBL’s picture

Let 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 2
b- The operation_date base field have the datetime type

 * @ContentEntityType(
 *   id = "bank_operation",
 ...
 *     "views_data" = "Drupal\bank_operation\BankOperationViewsData",
 ...
class BankOperation extends ContentEntityBase implements BankOperationInterface {

  public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
    ...
    $fields['operation_date'] = BaseFieldDefinition::create('datetime')
      ->setLabel(t("Operation date"))
      ->setRequired(TRUE)
      ->setSettings([
        'datetime_type' => DateTimeItem::DATETIME_TYPE_DATE,
      ])
    ...
}

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

class BankOperationViewsData extends EntityViewsData {

  public function getViewsData() {
    $data = parent::getViewsData();
    $data['bank_operation']['operation_date']['filter'] = [
       // I nee help to handle this
    ];
    return $data;
  }

}

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

Drupal 9.1.10 (June 4, 2021) and Drupal 9.2.10 (November 24, 2021) were the last bugfix releases of those minor version series. Drupal 9 bug reports should be targeted for the 9.3.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

fvictoire’s picture

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

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.

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.

ptmkenny’s picture

To update the example in #2 point 2, the code should be this:

class BankOperationViewsData extends EntityViewsData {

  public function getViewsData() {
    $data = parent::getViewsData();
    $data['bank_operation']['operation_date']['filter'] = [
         'id' => 'datetime',
         'field_name' => 'operation_date',
    ];
    return $data;
  }

}

Based on this comment

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

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.