Custom entity datetime field in calendar

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.

This article was republished from its original source.
Call Us: 1(800)730-2416

Pixeldust is a 20-year-old web development agency specializing in Drupal and WordPress and working with clients all over the country. With our best in class capabilities, we work with small businesses and fortune 500 companies alike. Give us a call at 1(800)730-2416 and let’s talk about your project.

FREE Drupal SEO Audit

Test your site below to see which issues need to be fixed. We will fix them and optimize your Drupal site 100% for Google and Bing. (Allow 30-60 seconds to gather data.)

Powered by

Custom entity datetime field in calendar

On-Site Drupal SEO Master Setup

We make sure your site is 100% optimized (and stays that way) for the best SEO results.

With Pixeldust On-site (or On-page) SEO we make changes to your site’s structure and performance to make it easier for search engines to see and understand your site’s content. Search engines use algorithms to rank sites by degrees of relevance. Our on-site optimization ensures your site is configured to provide information in a way that meets Google and Bing standards for optimal indexing.

This service includes:

  • Pathauto install and configuration for SEO-friendly URLs.
  • Meta Tags install and configuration with dynamic tokens for meta titles and descriptions for all content types.
  • Install and fix all issues on the SEO checklist module.
  • Install and configure XML sitemap module and submit sitemaps.
  • Install and configure Google Analytics Module.
  • Install and configure Yoast.
  • Install and configure the Advanced Aggregation module to improve performance by minifying and merging CSS and JS.
  • Install and configure Schema.org Metatag.
  • Configure robots.txt.
  • Google Search Console setup snd configuration.
  • Find & Fix H1 tags.
  • Find and fix duplicate/missing meta descriptions.
  • Find and fix duplicate title tags.
  • Improve title, meta tags, and site descriptions.
  • Optimize images for better search engine optimization. Automate where possible.
  • Find and fix the missing alt and title tag for all images. Automate where possible.
  • The project takes 1 week to complete.