1

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
    Commented Jan 27, 2017 at 20:40
  • hah - yes. I'm working on a hook_views_data implementation now, then will post it as an answer here.
    – Brian
    Commented Jan 27, 2017 at 20:44

2 Answers 2

3

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
    Commented Feb 10, 2017 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
    Commented Feb 10, 2017 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
    Commented Feb 10, 2017 at 16:24
0

An entity type can be revisionable and translatable and in these cases, the table name will not be entity_type. Check the @ContentEntityType annotation on the entity class for data_table and revision_data_table keys.

Also, the ListField Views filter class needs field_name set.

Here's a working example for a base field set on media:

function sd8_media_views_data_alter(array &$data) {
  $data['media_field_data']['embed_status']['filter']['id'] = 'list_field';
  $data['media_field_data']['embed_status']['filter']['field_name'] = 'embed_status';
}

My field is not revisionable and so I didn't need to set the same on $data['media_field_revision'] but if your field is, then you need that too. xdebug is very much your friend, put a breakpoint inside the hook_views_data_alter function and look at $data.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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