2

I created an entity in a module, using code. I generated skeletons with drupal console generate:entity:content (without bundles), and then added a datetime field to my TestFieldEntityItem.php, in baseFieldDefinitions().

$fields['test_date'] = BaseFieldDefinition::create('datetime')
  ->setLabel(t('Test date'))
  ->setDescription(t('Test.'))
  ->setRevisionable(TRUE)
  ->setSettings([
    'datetime_type' => 'date'
  ])
  ->setDefaultValue('')
  ->setDisplayOptions('view', [
    'label' => 'above',
    'type' => 'datetime_default',
    'settings' => [
      'format_type' => 'medium',
    ],
    'weight' => 14,
  ])
  ->setDisplayOptions('form', [
    'type' => 'datetime_default',
    'weight' => 14,
  ])
  ->setDisplayConfigurable('form', TRUE)
  ->setDisplayConfigurable('view', TRUE);

I installed my module, cleared the cache, reinstalled calendar and calendar_datetime modules, but when I try to add a view from a template, like the calendar module suggests, I can't see any template matching my datetime field.

If I create a datetime field on this entity through the UI, it appears in view templates. What am I missing?

Inspecting the calendar code, I found the function that search datetime fields, in ViewsFieldTemplate.php. This function finds my field, but the field_info does not match isDateField() because of the argument property.

isDateField() wants 'datetime' as argument ID.

protected function isDateField($field_info) {
  if (!empty($field_info['field']['id']) && $field_info['field']['id'] == 'field') {
    if (!empty($field_info['argument']['id']) && $field_info['argument']['id'] == 'date') {
      return TRUE;
    }
  }
  return FALSE;
}

My field_info argument is 'string'.

array (size=7)
  'title' => ...
  'help' => ...
  'field' => 
      'id' => string 'field' (length=5)
  'argument' => 
      'id' => string 'string' (length=6)
  'filter' => 
      'id' => string 'string' (length=6)
  'sort' => 
      'id' => string 'standard' (length=8)
  'entity field' => string 'test_date' (length=4)

So thanks to @4k4 answer, I tried to do the same as the working date field, and I created my own EntityViewsData (I tried hook_views_data_alter() first but the field was not present in input data)

namespace Drupal\kabukis_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;
  }
}

Now, the field appears in views template list ! However, the generated view is not working, I get the following 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.

1
  • 3
    I assume you have "views_data" = "Drupal\views\EntityViewsData" in your entity. But this doesn't help for a datatime basefield, because of this issue drupal.org/node/2489476, so you need a hook_view_data, see #9 in this issue.
    – 4uk4
    Aug 18, 2017 at 21:27

2 Answers 2

3

EntityViewsData provides the information for views for entity types, so that they are included in views. For datetime basefields this is not working at the moment, because of this issue https://www.drupal.org/node/2489476. So you need to provide the data in a hook_views_data or by extending EntityViewsData. From the documentation:

  • argument: Specifies a handler to make it available to Views as an argument, or contextual filter as it is known in the UI.

https://api.drupal.org/api/drupal/core%21modules%21views%21views.api.php/function/hook_views_data/8.3.x

You've mentioned that you have a working example if you use a field configured in UI, so you could try to copy the views data.

3
  • I tried this, it makes the field to appear in views template, but I get another error (see updated question), maybe I need to define more things in my EntityViewsData class ?
    – Grubshka
    Aug 21, 2017 at 8:30
  • This is where #9 of the issue ends, it doesn't say which view data you should provide, it only has an example for another field type. But you could place both a base and a UI configured (bundle) datetime field in the entity and compare the views_data (which is one big array). And if you've found it add the information to the issue.
    – 4uk4
    Aug 21, 2017 at 8:48
  • The field is accessible in views, as field, filter sort. However I still have the same error when I try to make a calendar view.
    – Grubshka
    Aug 21, 2017 at 14:06
0

There is an error in

namespace Drupal\kabukis_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' ],
   ...

You have to write :

...'field' => [ 'id' => 'date' ],...

according to the documentation in api.drupal.org of hook_views_data()(it is the same returned format of data like in this class).

Your Answer

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

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