6

My module adds a basefield 'myfield' to the core 'file' content entity.

I now need to uninstall my module for development purposes, but I can't because drush refuses to uninstall the module, saying "There is data for the field myfield on entity type File".

My understanding is that this is a known issue:

  • Because my module adds this field to an entity it does not own, this uninstallation will not be taken care of automatically by the core uninnstall procedure,

  • Therefore my module needs to have a hook_uninstall that triggers field uninstallation using \Drupal::entityDefinitionUpdateManager()->uninstallFieldStorageDefinition

  • However, a field can't be uninstalled if it still has data in it, so I first need to remove the data.

  • But the system to purge field data currently only works for bundle fields not base fields.

  • There may be other ways to delete field data, but they won't scale to large quantities of data.

  • Therefore there is no robust way to uninstall my module and Drupal core is correct to refuse to uninstall it.

Given all that, how best can I uninstall it when I need to do so for development purposes (short of reinstalling Drupal)?

Here is how I am creating it:

In mymodule.module:

function mymodule_entity_base_field_info(\Drupal\Core\Entity\EntityTypeInterface $entity_type) {
  if ($entity_type->id() == 'file') {
    $fields = array();
    $fields['myfield'] = BaseFieldDefinition::create('boolean')
      ->setLabel(t('My field'))
    return $fields;
  }
}

In mymodule.install:

  $entity_manager = \Drupal::entityManager();
  $definition = $entity_manager->getFieldStorageDefinitions('file')['myfield'];
  $entity_manager->onFieldStorageDefinitionCreate($definition);

2 Answers 2

1

This is a known Drupal Core limitation that looks close to being ready so hopefully will be fixed in D8.5. In the meantime you could apply the patch linked to the issue.

0

Does drush field-delete myfieldname work for base fields? If so, you could look in drush's source code to see how it accomplishes this.

I don't see any information regarding this in the drush 8.x documentation.

1
  • Looks like the field commands are not yet working for any type of field in D8
    – AdamS
    Commented Nov 12, 2017 at 10: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.