In would like to display the created date of a custom entity in the "short" format, whereas currently it displays in some default format. This is in a module that is already in, production so I need to do it as an update. I've changed the custom entity code so that it looks like this:
$fields['created'] = BaseFieldDefinition::create('created')
->setLabel(t('Created on'))
->setDescription(t('When the case was created.'))
->setDisplayOptions('view', [
'type' => "datetime_default",
'label' => 'above',
'settings' => [
'date_format' => 'short'
]
]);
Then I ran drupal update:entities
but it did not change. I was not surprised, because I had read somewhere recently (I can't find it now, sorry) that it had become obsolete & custom entity updates now have to be done in update hooks.
But I am stumped as to how to update it in the update hook. I've tried this
function opencase_entities_update_8016() {
$update_manager = \Drupal::entityDefinitionUpdateManager();
$definition = $update_manager->getFieldStorageDefinition('created', 'oc_case');
$update_manager->updateFieldStorageDefinition($definition);
}
... which did nothing, and then I looked at this documentation which helped me understand it's not the field storage definition that needs updating but the view config. So I made another update hook following the suggestion there, but simplifying it for my own case. I must have got it wrong because the date is still stubbornly showing in the default format. Can you help?
function opencase_entities_update_8017() {
$update_manager = \Drupal::entityDefinitionUpdateManager();
$field = $update_manager->getFieldStorageDefinition('created', 'oc_case');
$properties = array(
'targetEntityType' => $field->getTargetEntityTypeId(),
'bundle' => $field->getTargetBundle()
);
if ($view_displays = \Drupal::entityTypeManager()->getStorage('entity_view_display')->loadByProperties($properties)) {
foreach ($view_displays as $view_display) {
$view_display->setComponent('created', array(
'type' => 'datetime_default',
'settings' => ['format_type' => 'short'],
) + $component)->save();
}
}
}
I randrush updb
each time and it appeared to complete successfully. No errors in log.