Adding a base field definition in an update hook - it's not appearing I want to add another base field to a Drupal entity. It used to be possible to just change the code for the entity definition and then update it via the Drupal console. But now it has to be done in an update hook.

I've added the following update hook, done drush updb and the process completes without errors. I then did drush cr to clear the cache, and then opened up the edit form for the entity in question and gave it a hard refresh. But the new field has not appeared. Any idea what I'm doing wrong? Thanks. The code is based on this: https://www.drupal.org/node/3034742, section named "Installing a new field storage definition".

function opencase_entities_update_8002() {
  $storage_definition =  \Drupal\Core\Field\BaseFieldDefinition::create('datetime')
      -&gt;setLabel(t('Date and time'))
      -&gt;setDescription(t('When the activity started.'))
      -&gt;setRevisionable(TRUE)
      -&gt;setSettings([
        'datetime_type' =&gt; 'date'
      ])
      -&gt;setDefaultValue('2000-01-01')
      -&gt;setDisplayOptions('view', [
        'label' =&gt; 'above',
        'weight' =&gt; -3,
      ])
      -&gt;setDisplayOptions('form', [
        'weight' =&gt; -3,
      ]);

  \Drupal::entityDefinitionUpdateManager()
    -&gt;installFieldStorageDefinition('activity_date_time', 'oc_activity', 'oc_activity', $storage_definition);
}
The problem lies in your display options configuration (reference 4k4's answer to #244122).

Try this:

function opencase_entities_update_8002() {
  $storage_definition =  \Drupal\Core\Field\BaseFieldDefinition::create('datetime')
      -&gt;setLabel(t('Date and time'))
      -&gt;setDescription(t('When the activity started.'))
      -&gt;setRevisionable(TRUE)
      -&gt;setSettings([
        'datetime_type' =&gt; 'date'
      ])
      -&gt;setDefaultValue('2000-01-01')
      -&gt;setDisplayOptions('view', [
        'label' =&gt; 'above',
        'type' =&gt; 'datetime_default',
        'settings' =&gt; [
          'format_type' =&gt; 'medium',
        ],
        'weight' =&gt; -3,
      ])
      -&gt;setDisplayOptions('form', [
        'type' =&gt; 'datetime_default',
        'weight' =&gt; -3,
      ]);

  \Drupal::entityDefinitionUpdateManager()
    -&gt;installFieldStorageDefinition('activity_date_time', 'oc_activity', 'oc_activity', $storage_definition);
}


Edit: Don't forget to update your Entity definition as well!