1

I am trying to add a new bundle field into my custom entity, I added the following code into the MyCustomEntity.php file, I uninstalled my module, so, I installed it again, the field is showing up on the view, but in my data base the field is not added.

Could you please support me?

$fields['new_name'] = BaseFieldDefinition::create('text_long')
  ->setLabel(t('New Name'))
  ->setDisplayOptions('form', [
    'type' => 'text_textarea',
  ])
  ->setRevisionable(TRUE)
  ->setTargetBundle($bundle);

Thank you, I will really appreciate your support.

1

For the Entity base field update, You'll need to also add an update hook to apply it

For example:

Installing a new field storage definition

use Drupal\Core\Field\BaseFieldDefinition;

/**
 * Implements hook_update_N
 */
function example_update_8701() {
  $bundle = 'article';
  $field_storage_definition = BaseFieldDefinition::create('text_long')
  ->setLabel(t('New Name'))
  ->setDisplayOptions('form', [
    'type' => 'text_textarea',
  ])->setRevisionable(TRUE)
  ->setTargetBundle($bundle);

  \Drupal::entityDefinitionUpdateManager()
    ->installFieldStorageDefinition('new_name', 'node', 'EXAMPLE_MODULE', $field_storage_definition);
}

Documentation: https://www.drupal.org/node/3034742

3
  • it does not work for me – Karen Gonzalez Jan 12 at 21:13
  • Make sure the bundle param matches, it should add a field to the article node type. – David Thomas Jan 12 at 22:21
  • 1
    thank you! it works now – Karen Gonzalez Jan 13 at 18:02

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.