0

I created a custom entity type that has a base field definition like this:

$fields['start_date'] = BaseFieldDefinition::create('datetime')
  ->setLabel(t('Start Date'))
  ->setDescription(t('The date and time to start.'))
  ->setDisplayOptions('view', array(
    'type' => 'datetime_default',
    'weight' => 3,
  ))
  ->setDisplayOptions('form', [
    'type' => 'datetime_default',
    'weight' => 3,
  ])
  ->setRequired(TRUE);

When I create a view that displays entities of this type, and try to create a filter on this base field, I get the generic filter provided by views, instead of the one defined in Drupal\datetime\Plugin\views\filter\Date.

Why is that?

2
  • I'm guessing drupal.org/node/2489476#comment-11891454 is you, then you figured it out by now ;) As a workaround, you can specify the filter yourself for now
    – Berdir
    Jan 27 '17 at 20:40
  • hah - yes. I'm working on a hook_views_data implementation now, then will post it as an answer here.
    – Brian
    Jan 27 '17 at 20:44
2

Looks like this is a known major issue. Here's the main "parent" issue where most of the work is going down: https://www.drupal.org/node/2337515

I think, by default, there is some basic views integration for all entities, but it's not that robust.

As a workaround, I was able to implement hook_views_data_alter to tell views to use the 'datetime' filter plugin instead of the default 'string' plugin it had.

function MY_MODULE_views_data_alter(array &$data) {
  $data['my_entity_type']['start_date']['filter']['id'] = 'datetime';
}
3
  • Can you specify what changes you made in the filter? It seems that the MY_MODULE_run_views_data_alter does not exist, is an error and I have to use MY_MODULE_views_data_alter ?
    – ferriol
    Feb 10 '17 at 13:19
  • Sorry, I that was a type in the hook name. Updated answer to fix that and removed the last bit about creating a custom filter. I think that was incorrect as well, it shouldn't have been required.
    – Brian
    Feb 10 '17 at 14:27
  • I tested and doesn't work for me, when I try to add a filter with this field, the popup window doesn't show me the options and reappear the add filter page. When I reload the edit view page, shows me this error "Notice: Undefined index: field_name in Drupal\datetime\Plugin\views\filter\Date->getFieldStorageDefinition() (line 56 of /var/www/.../core/modules/views/src/FieldAPIHandlerTrait.php)"
    – ferriol
    Feb 10 '17 at 16:24

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.