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 Drupalkabukis_testsEntity; use DrupalviewsEntityViewsData; 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 :
DrupalcalendarPluginviewsstyleCalendarStyle: A calendar date argument is required when using the calendar style, but it is missing or is not using the default date.