0

I have created a custom entity as follow:

Here is an extract of my entity definition in modules/custom/bank_operation/src/Entity/BankOperation.php

Notes:

1- As you can see, I have changed the value of the views_data key

2- 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,
      ])

    ...
}

Here is an extract of my overrode EntityViewsData in modules/custom/bank_operation/src/BankOperationViewsData.php

This class is used according to the value of the views_data key. This is my attempt to allow a view to filter on the operation_date field

class BankOperationViewsData extends EntityViewsData {

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

}

After that, I have created a view to display my BankOperation entities with an exposed filter on the operation_date field.

Here is the error produced when using this filter (I don't understand why ViewsYearFilterDatetime is called)

Exception: DateTime object not set. in Drupal\Component\Datetime\DateTimePlus->__call() (line 360 of core/lib/Drupal/Component/Datetime/DateTimePlus.php). 

Drupal\datetime\Plugin\views\filter\Date->opSimple() (Line: 63)
Drupal\views_year_filter\Plugin\views\filter\ViewsYearFilterDatetime->opSimple() (Line: 330)
Drupal\views\Plugin\views\filter\NumericFilter->query() (Line: 1373)
Drupal\views\ViewExecutable->_build() (Line: 1262)
Drupal\views\ViewExecutable->build() (Line: 390)
Drupal\views\Plugin\views\display\PathPluginBase->execute() (Line: 180)
Drupal\views\Plugin\views\display\Page->execute() (Line: 1630)
Drupal\views\ViewExecutable->executeDisplay() (Line: 77)

I know that there are other questions like this one, but none of them are providing a detailed answer (only links).

I have setup my question to allow people who are facing the same issue to quickly find a path to solve it.

0

As there are so many difficulties with base fields (sorting, displaying, filter etc in views), I decided to remove all of them and I re-created them back in the field UI.

To allow this, I set fieldable = TRUE, and field_ui_base_route = some_path in the annotations of my entity. This is of course poor design because each field will create a table on its own but I had no time to spend on this...

Feel free to provide a better answer... I think we need a real how-to to provide the same functionalities we have for fields created in UI then for base fields

Edit to add: I have created an issue to create an help item on this topic: https://www.drupal.org/project/drupal/issues/3213021

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.