Remove entity_keys from the Entity using hook_update
Table of contents
No headings in the article.
If we have a custom entity, we have a property called entity_keys[ https://www.drupal.org/docs/drupal-apis/entity-api/creating-a-content-entity-type-in-drupal-8],
- entity_keys: How to access the fields. Analog to 'nid' or 'uid'.
if we create a custom entity, and if we define keys in the "entity_keys", all the fields will be required, if we want to remove the required property for those fields after the entity is created, we need to use hook_update to do that.
Below is the same code I used to do it.
In the Module install my_module.install file we need to create the below function
function module_name_update_9001() {
$manager = \Drupal::entityDefinitionUpdateManager();
$entity_type = $manager->getEntityType('entity_name'); // entity_name need to be replaced
$entity_keys = $entity_type->getKeys();
unset($entity_keys['property_name or field_name']); // property_name or field_name needs to be replaced. ex: status
$entity_type->set('entity_keys', $entity_keys);
$manager->updateEntityType($entity_type);
$manager->updateFieldStorageDefinition($manager->getFieldStorageDefinition('property_name or field_name', 'entity_name'));
}
After adding the above hook, run "drush updb -y"