1

I've made a base field on a custom entity with type "datetime" using "datetime_default" for the form display. It's displaying as only a date picker, with no time component, whereas I want it to look like this:

Date picker with time component

If I create a field in the GUI I can select between "date only" or "date and time". (The image above is the result of picking "date and time"). But I can't work out how to do this programmatically. I've tried creating fields of both types and exporting the config, but there is no difference between them, which is a mystery.

I suspect I need to put something other than "datetime_default" but I can't find any documentation on how to programmatically refer to the various date and time widgets.

public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields = parent::baseFieldDefinitions($entity_type);

$fields['activity_date_time'] = BaseFieldDefinition::create('datetime')
  ->setLabel(t('Date and time'))
  ->setDescription('')
  ->setRevisionable(TRUE)
  ->setRequired(TRUE)
  ->setSettings([
    'datetime_type' => 'date'
  ])  
  ->setDefaultValueCallback('Drupal\opencase_entities\Entity\OCActivity::currentDateTime')
  ->setDisplayOptions('view', [
    'label' => 'above',
    'type' => 'datetime_default',
    'settings' => [
      'format_type' => 'medium',
    ],
    'weight' => -3, 
  ])  
  ->setDisplayOptions('form', [
    'type' => 'datetime_default',
    'weight' => -3, 
  ]); 
4
  • 1
    setSettings(['datetime_type' => 'date']) this seems to be to blame. See the switch in the source here - if type is 'date' (implemented as a const) then only show date form element, otherwise show date+ time elements. Feb 23, 2020 at 19:16
  • 1
    You should put that as an answer to be up-voted.
    – Jaypan
    Feb 24, 2020 at 3:57
  • Brilliant, thanks @stevekeiretsu! I just removed that "setSettings" bit and it worked. Please do put it as an answer so I can accept it.
    – naomi
    Feb 24, 2020 at 10:52
  • I wasn't entirely sure if it would work, or whether naomi needed to remove that bit or replace it with something else. I'll make it an answer now. Feb 24, 2020 at 11:36

1 Answer 1

2

setSettings(['datetime_type' => 'date']) - this seems to be to blame. See the switch in the source here which effectively says if type is 'date' (implemented as a const) then only show the date form element, otherwise show the date + time elements.

1
  • 1
    I wouldn't say this is 100% accurate. The setSettings(['datetime_type' => 'date']) sets the field type to date-only (and you should really use setSettings(['datetime_type' => DateTimeItem::DATETIME_TYPE_DATE]). When you remove that from the field definition, it uses the default value, which happens to be 'datetime', which is date+time. Using setSettings(['datetime_type' => DateTimeItem::DATETIME_TYPE_DATETIME]) would also have the same effect here.
    – mpdonadio
    Feb 25, 2020 at 13:26

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.