1

I have a custom entity with (among others) a field defined this way:

$fields['duedate'] = BaseFieldDefinition::create('datetime')
  ->setLabel(t('Due Date'))
  ->setSetting('datetime_type', 'date')
  ->setDefaultValue(array(0 => array(
    'default_date_type' => 'now',
    'default_date' => '2050-12-31',
  )))
  ->setDisplayOptions('view', [
  'type' => 'datetime_default',
  'settings' => [
    'format_type' => 'date_courte',
  ],
    'weight' => 5,
  ])
  ->setDisplayOptions('form', array(
    'weight' => 5,
  ))
  ->setDisplayConfigurable('form', TRUE)
  ->setDisplayConfigurable('view', TRUE);

'date_courte' is a custom date format I created to display date as 'dd/mm/yyyy'. And it works!
I'd like to have the same result without using 'date_courte' so I tried

  'settings' => [
    'format_type' => 'custom',
    'format' => 'd/m/Y',
  ],

instead of

  'settings' => [
    'format_type' => 'date_courte',
  ],

And it doesn't work!

I'm pretty sure of 'format_type' => 'custom'.
I think the problem comes from 'format' in 'format' => 'd/m/Y'.

What should be the correct syntax?

3

I'm pretty sure you need to use the datetime_custom display type vs datetime_default. You won't need to pass the format_type parameter in your settings array. You will end up with something like:

...
->setDisplayOptions('view', [
  'type' => 'datetime_custom',
  'settings' => [
    'date_format' => 'd/m/Y',
  ],
...

That should work how you'd like. The datetime_custom formatter is defined in core's datetime module.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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