Skip to content

Instantly share code, notes, and snippets.

@lcube45
Created June 1, 2020 14:56
Show Gist options
  • Save lcube45/4364f8ae3e0a10a830ea33317b6606f6 to your computer and use it in GitHub Desktop.
Save lcube45/4364f8ae3e0a10a830ea33317b6606f6 to your computer and use it in GitHub Desktop.
Drupal 8 - hook update - installEntityType/uninstallEntityType
<?php
/**
* Update- Create your_entity_name entity.
*/
function my_module_update_8001() {
//check if the table exists first. If not, then create the entity.
if(!db_table_exists('your_entity_name')) {
\Drupal::entityTypeManager()->clearCachedDefinitions();
\Drupal::entityDefinitionUpdateManager()
->installEntityType(\Drupal::entityTypeManager()->getDefinition('your_entity_name'));
}
else {
return 'your_entity_name entity already exists';
}
}
/**
* Uninstall- Create your_entity_name entity.
*/
function my_module_update_8003() {
//check if the table exists first. If not, then create the entity.
if(db_table_exists('your_entity_name')) {
\Drupal::entityTypeManager()->clearCachedDefinitions();
\Drupal::entityDefinitionUpdateManager()
->uninstallEntityType(\Drupal::entityTypeManager()->getDefinition('faq_answer_entity'));
}
else {
return 'faq_answer_entity entity do not exist';
}
}
@lcube45
Copy link
Author

lcube45 commented Jun 1, 2020

Since Drupal 8.7.x, drush entup and automatic entity updates have been removed.

2 ways :

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment