Question
How to update schema (field definition) of an Entity of a drupal 8 custom entity ?
Add a new field:
Example 1 Automatic update:
1. Update baseFieldDefinitions() of the Entity.
2. Apply update from a mymodule_update_N(). Ex:
function mymodule_update_8001() {
//Update Entitys from baseFieldDefinitions();
\Drupal::entityDefinitionUpdateManager()->applyUpdates();
}
Examples from : https://www.drupal.org/node/2554097
API : https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Entity!EntityDefinitionUpdateManager.php/class/EntityDefinitionUpdateManager/8.2.x
Comments
Unrecommended?
Hello!
Thanks for the snippet, but it looks like it's not recommended. From https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Extension%21module.api.php/function/hook_update_N/8.5.x
Never call \Drupal::entityDefinitionUpdateManager()::applyUpdates() in an update function, as it will apply updates for any module not only yours, which will lead to unpredictable results.
Add new comment