0

I have a entity that has allready been enabled and the baseFieldDefinitions have created the fields in the database. But now i want to add a single field, and the obvious solution in dev is just to uninstall --> reinstall. But if the site is in production there must be a way to run a retrigger of that method? I have tried to clear the cache and updateing via the update.php file, but to no prevail. Ive allso tried to just create a entity with the new field in it, to see if it behaved like a ordinary ORM system where the initial creation of a entity has update triggers tied to it, but that allso did not work.

So im at a lost, i read through the drupal entity api, and a guy asked the exact same question but with no answer, so im thinking that it would be nice to put the answer here whatever it might be :)

My entity declaration(with the new field "processed" added):

 @ContentEntityType(
 *   id = "person",
 *   label = @Translation("A entity to contain contact information for future customers"),
 *   handlers = {
 *     "view_builder" = "Drupal\Core\Entity\EntityViewBuilder",
 *     "list_builder" = "Drupal\person\PersonListBuilder",
 *     "views_data" = "Drupal\person\Entity\PersonViewsData",
 *     "form" = {
 *       "default" = "Drupal\person\Form\PersonForm",
 *       "add" = "Drupal\person\Form\PersonForm",
 *       "edit" = "Drupal\person\Form\PersonForm",
 *       "delete" = "Drupal\person\Form\PersonDeleteForm",
 *     },
 *     "access" = "Drupal\person\PersonAccessControlHandler",
 *   },
 *   base_table = "person",
 *   admin_permission = "administer Person entities",
 *   entity_keys = {
 *     "id" = "id",
 *     "label" = "name",
 *     "uuid" = "uuid",
 *     "uid" = "user_id",
 *     "email" = "email",
 *     "phone" = "phone",
 *     "description" = "description",
 *     "langcode" = "langcode",
 *     "processed" = "processed"
 *   },
 *   links = {
 *     "canonical" = "/admin/person/{person}",
 *     "edit-form" = "/admin/person/{person}/edit",
 *     "delete-form" = "/admin/person/{person}/delete"
 *   },
 *   field_ui_base_route = "person.settings"
 * )
 */
class Person extends ContentEntityBase implements PersonInterface
{

My baseFieldDefinitions method inside the entity looks like this:

 /**
     * {@inheritdoc}
     */
    public static function baseFieldDefinitions(EntityTypeInterface $entity_type)
    {
        $fields['id'] = BaseFieldDefinition::create('integer')
            ->setLabel(t('ID'))
            ->setDescription(t('The ID of the A entity to contain contact information for future customers entity.'))
            ->setReadOnly(TRUE);
        $fields['uuid'] = BaseFieldDefinition::create('uuid')
            ->setLabel(t('UUID'))
            ->setDescription(t('The UUID of the A entity to contain contact information for future customers entity.'))
            ->setReadOnly(TRUE);

        $fields['user_id'] = BaseFieldDefinition::create('entity_reference')
            ->setLabel(t('Authored by'))
            ->setDescription(t('The user ID of author of the A entity to contain contact information for future customers entity.'))
            ->setRevisionable(TRUE)
            ->setSetting('target_type', 'user')
            ->setSetting('handler', 'default')
            ->setDefaultValueCallback('Drupal\node\Entity\Node::getCurrentUserId')
            ->setTranslatable(TRUE)
            ->setDisplayOptions('view', array(
                'label' => 'hidden',
                'type' => 'author',
                'weight' => 0,
            ))
            ->setDisplayOptions('form', array(
                'type' => 'entity_reference_autocomplete',
                'weight' => 5,
                'settings' => array(
                    'match_operator' => 'CONTAINS',
                    'size' => '60',
                    'autocomplete_type' => 'tags',
                    'placeholder' => '',
                ),
            ))
            ->setDisplayConfigurable('form', TRUE)
            ->setDisplayConfigurable('view', TRUE);

        $fields['name'] = BaseFieldDefinition::create('string')
            ->setLabel(t('Name'))
            ->setDescription(t('The name of the A entity to contain contact information for future customers entity.'))
            ->setSettings(array(
                'max_length' => 50,
                'text_processing' => 0,
            ))
            ->setDefaultValue('')
            ->setDisplayOptions('view', array(
                'label' => 'above',
                'type' => 'string',
                'weight' => -4,
            ))
            ->setDisplayOptions('form', array(
                'type' => 'string_textfield',
                'weight' => -4,
            ))
            ->setDisplayConfigurable('form', TRUE)
            ->setDisplayConfigurable('view', TRUE);

        $fields['status'] = BaseFieldDefinition::create('boolean')
            ->setLabel(t('Publishing status'))
            ->setDescription(t('A boolean indicating whether the A entity to contain contact information for future customers is published.'))
            ->setDefaultValue(TRUE);

        $fields['processed'] = BaseFieldDefinition::create('boolean')
            ->setLabel(t('Processed'))
            ->setDescription(t('A boolean describeing if a customer has been contacted or not.'))
            ->setDefaultValue(FALSE);

        $fields['langcode'] = BaseFieldDefinition::create('language')
            ->setLabel(t('Language code'))
            ->setDescription(t('The language code for the A entity to contain contact information for future customers entity.'));

        $fields['created'] = BaseFieldDefinition::create('created')
            ->setLabel(t('Created'))
            ->setDescription(t('The time that the entity was created.'));

        $fields['changed'] = BaseFieldDefinition::create('changed')
            ->setLabel(t('Changed'))
            ->setDescription(t('The time that the entity was last edited.'));

        $fields['phone'] = BaseFieldDefinition::create('string')
            ->setLabel(t('Phone'))
            ->setDescription(t('The phonenumber of future customers'))
            ->setDefaultValue('');

        $fields['email'] = BaseFieldDefinition::create('string')
            ->setLabel(t('Email'))
            ->setDescription(t('The email of future customers'))
            ->setDefaultValue('');

        $fields['description'] = BaseFieldDefinition::create('string')
            ->setLabel(t('Description'))
            ->setDescription(t('A description of whatever task the future customer wants to be contacted about'))
            ->setDefaultValue('');
        return $fields;
    }
1

1 Answer 1

2

See node_update_8001():

/**
 * Add 'revision_translation_affected' field to 'node' entities.
 */
function node_update_8001() {
  // Install the definition that this field had in
  // \Drupal\node\Entity\Node::baseFieldDefinitions()
  // at the time that this update function was written. If/when code is
  // deployed that changes that definition, the corresponding module must
  // implement an update function that invokes
  // \Drupal::entityDefinitionUpdateManager()->updateFieldStorageDefinition()
  // with the new definition.
  $storage_definition = BaseFieldDefinition::create('boolean')
      ->setLabel(t('Revision translation affected'))
      ->setDescription(t('Indicates if the last edit of a translation belongs to current revision.'))
      ->setReadOnly(TRUE)
      ->setRevisionable(TRUE)
      ->setTranslatable(TRUE);

  \Drupal::entityDefinitionUpdateManager()
    ->installFieldStorageDefinition('revision_translation_affected', 'node', 'node', $storage_definition);
}
1

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.