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

jibran’s picture

jibran’s picture

Priority: Normal » Major

I think it's at least a major.

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

bkosborne’s picture

Dang, 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?

bkosborne’s picture

The existing Drupal\datetime\Plugin\views\filter\Date filter plugin uses Drupal\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

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Dom.’s picture

Is there a workaround about this so views can use datetime exposed filter as date ?

jibran’s picture

Title: Provide Views integration for datetime fields used as base fields » [PP-1] Provide Views integration for datetime fields used as base fields
Status: Active » Postponed

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

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

grubshka_v2’s picture

@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 :

namespace Drupal\kaukis_tests\Entity;
use Drupal\views\EntityViewsData;

class TestEntityViewsData extends EntityViewsData {
  public function getViewsData() {
    $additionalData = [
      'test_date' => [
        'title' => new TranslatableMarkup('Birth date'),
        'help' => new TranslatableMarkup('Birth date'),
        'field' => [ 'id' => 'field' ],
        'argument' => [ 'id' => 'date' ],
        'filter' => [ 'id' => 'date' ],
        'sort' => [ 'id' => 'date' ],
        'entity field' => 'test_date',
      ]
    ];
    $data['test_field_entity_field_data'] = array_merge($data['test_field_entity_field_data'], $additionalData);
    return $data;
  }
}

However the calendar is not working and I get this error :

\Drupal\calendar\Plugin\views\style\CalendarStyle: A calendar date argument is required when using the calendar style, but it is missing or is not using the default date.

So it make me think my views data is not well defined.

Lukas von Blarer’s picture

@grubshka_v2 i am also trying to get this working. I guess this is the starting point:

<?php
function datetime_range_field_views_data(FieldStorageConfigInterface $field_storage) {
  \Drupal::moduleHandler()->loadInclude('datetime', 'inc', 'datetime.views');

  // Get datetime field data for value and end_value.
  $data = datetime_type_field_views_data($field_storage, [], 'value');
  $data = datetime_type_field_views_data($field_storage, $data, 'end_value');

  return $data;
}
?>

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?

Lukas von Blarer’s picture

Ok, I managed to solve this for my entity type:

<?php
$data['event_registration_time_slot']['daterange__value']['filter'] = [
      'field' => 'daterange__value',
      'table' => 'event_registration_time_slot',
      'id' => 'datetime',
      'field_name' => 'daterange',
      'entity_type' => 'event_registration_time_slot',
      'allow empty' => TRUE,
    ];
    $data['event_registration_time_slot']['daterange__value']['sort'] = [
      'field' => 'daterange__value',
      'table' => 'event_registration_time_slot',
      'id' => 'datetime',
      'field_name' => 'daterange',
      'entity_type' => 'event_registration_time_slot',
    ];
?>

This goes into the getViewsData() method of the views_data handler of the entity.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

MariaIoann’s picture

A 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

rfulcher’s picture

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

class TimeOffDayViewsData extends EntityViewsData {

  /**
   * {@inheritdoc}
   */
  public function getViewsData() {
    $data = parent::getViewsData();

    // Additional information for Views integration, such as table joins, can be
    // put here.

    $data['TimeOffDay']['timeoffdate']['filter'] = [
      'field' => 'timeoffdate',
      'table' => 'TimeOffDay',
      'id' => 'datetime',
      'field_name' => 'daterange',
      'entity_type' => 'TimeOffDay',
      'allow empty' => TRUE,
    ];

    return $data;
  }

}
Sam152’s picture

This 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:

  /**
   * {@inheritdoc}
   */
  public function getViewsData() {
    $data = parent::getViewsData();
    $this->attachDateTimeViewsData($data);
    return $data;
  }

  /**
   * Fix views data integration for the datetime field.
   */
  protected function attachDateTimeViewsData(&$data) {
    // Automatic integration blocked behind https://www.drupal.org/node/2489476.
    $datetime_columns = [
      'date_time__value',
      'date_time__end_value',
    ];
    foreach ($data as $table_name => $table_data) {
      foreach ($datetime_columns as $datetime_column_name) {
        $data[$table_name][$datetime_column_name]['filter']['id'] = 'datetime';
        $data[$table_name][$datetime_column_name]['filter']['field_name'] = 'date_time';
        $data[$table_name][$datetime_column_name]['argument']['id'] = 'datetime';
        $data[$table_name][$datetime_column_name]['argument']['field_name'] = 'date_time';
        $data[$table_name][$datetime_column_name]['sort']['id'] = 'datetime';
        $data[$table_name][$datetime_column_name]['sort']['field_name'] = 'date_time';
      }
    }
  }
joachim’s picture

Title: [PP-1] Provide Views integration for datetime fields used as base fields » [PP-1] Base fields miss out on Views data from hook_field_views_data()

Would #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.

Berdir’s picture

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

joachim’s picture

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

the_glitch’s picture

PP

Version: 8.6.x-dev » 8.8.x-dev

Drupal 8.6.x will not receive any further development aside from security fixes. Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

DuneBL’s picture

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

joachim’s picture

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

DuneBL’s picture

Don'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...

joachim’s picture

Yes, of course, we need docs for workarounds. I meant that the fix is blocked by the tests, not the documentation. Sorry for the confusion!

DuneBL’s picture

thank 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

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

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

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

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.

DamienMcKenna’s picture

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