0

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.

2
  • This is not a database schema change and has nothing to do with what you had read somewhere. And it is not a config change either because the base field is not set to be configurable. So a simple cache clear should update the code or an empty update hook if you want to force a cache clear when the module update is deployed.
    – 4uk4
    Commented May 13, 2020 at 15:47
  • Thanks 4k4, useful to know I don't need to faff with update hooks. But have you any idea why it isn't updating then? I cleared the cache many times in the process of trying to do all that
    – naomi
    Commented May 13, 2020 at 19:06

1 Answer 1

1

When I changed base fields in the past they always updated in the plugin definitions on a cache clear. Also if this involved a schema update, by the way, which then produced a warning on the status page that there is a mismatch with the database, for which you then would need what you've described in the question.

In your case, though, my guess would be that the new settings are not valid for a timestamp field. The field formatter datetime_default is probably only working for a datetime field.

So try to change the field formatter in the base field definition to:

    'type' => 'timestamp',
1
  • Nope, that didn't work either! I've given up on it and will put up with the standard date format. Perhaps with it being a timestamp field it can't be changed? But thanks for explaining about not needing the update hook, that helped my general understanding of how the entities work.
    – naomi
    Commented Jun 5, 2020 at 18:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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