3

I have a custom entity with a datetime field. This datetime is saved in the database as an ISO string. I would like to use Views on this entity, and select entities with dates before, after, or between certain dates. I see if I make a view against the system log, these options are presented when filtering on Timestamp. How can I enable the same filtering options on my field?

The field definition is:

$fields['set_datetime'] = BaseFieldDefinition::create('datetime')
  ->setLabel(t('Set DateTime'))
  ->setDescription(t('Date and time fermentor was set.'))
  ->setSettings(array(
    'datetime_type' => 'datetime',
  ))
  ->setDefaultValue('')
  ->setDisplayOptions('view', array(
    'label' => 'above',
    'type' => 'string',
    'weight' => -4,
  ))
  ->setDisplayOptions('form', array(
    'weight' => -4,
  ))
  ->setDisplayConfigurable('form', TRUE)
  ->setDisplayConfigurable('view', TRUE);
2
  • 1
    So you have a custom entity type with a datetime as a base field, and not a custom field type?
    – mpdonadio
    Commented Jan 31, 2017 at 20:55
  • Correct. Updated question with the code. Commented Jan 31, 2017 at 21:00

2 Answers 2

2

I posted the following as a bug report to Drupal Core

When a custom entity is created, and one of the fields is a datetime FieldType, views filters are not treating this field like a datetime field, but, instead, like a string. Timestamp Fieldtypes on the other hand are presented with the correct filter options.

The response I received was:

Yes, it does default to a string. I think the underlying reason is this:

Datetime support comes from the datetime module, so if you want your entity to use that, add a dependency to datetime and override any defaults for your entity in an extension of \Drupal\views\EntityViewsData.

The Views 'date' filter only works for timestamps (hence your timestamp fields work out-of-the-box), so just setting the filter plugin_id to 'date' won't help either.

So this works as designed I think.

1
  • What was the question for which that quote in your answer here was the response? And how does this answer you posted here answer your question here? Commented Feb 8, 2017 at 20:49
1
+25

After checking your module provided for me on github and some reverse-engineering I find out the problem is on create('datetime') and you should use create('created') So the following code

$fields['date'] = BaseFieldDefinition::create('created')
  ->setLabel(t('DateTime'))
  ->setDescription(t('Date and time.'))
  ->setDisplayOptions('view', array(
    'label' => 'above',
    'type' => 'timestamp',
    'weight' => -4,
  ))
  ->setDisplayOptions('form', array(
    'type' => 'datetime_timestamp',
    'weight' => -4,
  ))
  ->setDisplayConfigurable('form', TRUE)
  ->setDisplayConfigurable('view', TRUE);

Results the below image for me. it seems it's Ok now.

enter image description here

14
  • The only thing that changes is the field's widget on the Entity addition page. It removes the ability to add a 'time'. Commented Feb 3, 2017 at 1:33
  • @KevinNowaczyk I update the answer, try that
    – Yuseferi
    Commented Feb 3, 2017 at 16:12
  • Nope, Views is still filtering as if it was a string. Commented Feb 3, 2017 at 18:46
  • @KevinNowaczyk did you clear the cache? because we didn't use string type anymore
    – Yuseferi
    Commented Feb 4, 2017 at 14:10
  • 1
    I checked my new module a second time, and it was not working. (github.com/Beakerboy/drupal_test) Commented Feb 5, 2017 at 22:50

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.