1

I have a custom module with a custom entity which has its base fields specified in baseFieldDefinitions.

Now I want to add another base field to the entity but I can't see it in neither edit nor form display when applying the code below. I can see it gets added in the database and I have also tried just copy-pasting existing fields from baseFieldDefinitions() (which I know works) without luck. It seems it just doesn't update the frontend.

function word_update_9001() {
  $storage_definition = BaseFieldDefinition::create('string')
    ->setLabel(t('Temp Word'))
    ->setCardinality(4)
    ->setDisplayOptions('form', [
      'type' => 'string_textfield',
      'weight' => -5,
    ])
    ->setDisplayConfigurable('form', true);

  $update_manager = \Drupal::entityDefinitionUpdateManager();
  $update_manager->installFieldStorageDefinition('locked', 'word', 'word', $storage_definition);

Cache has been cleared several times.

2
  • Is this helpful? stefvanlooveren.me/blog/…
    – mona lisa
    Jul 22, 2022 at 21:51
  • That code seems similar to the code shown in drupal.org/docs/drupal-apis/update-api/…. The only difference is that the Drupal.org example is for a custom block, while the code in this question is for a custom entity we don't know. Apart noticing the field label is much different from the field machine name (Temp Word versus locked), there isn't much we can say without having more details on that entity.
    – apaderno
    Jul 23, 2022 at 11:02

2 Answers 2

1

You should do this in two steps. First make a new version of the entity class. Test it by reinstalling or, if you want to keep the data, by installing it on a different site. If the new entity version works and you can edit the entities including the new fields, then write the update hook. You can test the hook also on a second test site by installing first the old version and test the new code, before you apply it on production data.

1

This is for adding base field to any entity.

  1. Implement hook_base_field_info().
  2. Update schema (you already did this).

https://bryanmanalo.com/what-drupal-base-field-and-when-do-you-need-create-one

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.