I am trying to insert a new vocabulary to taxonomy because a custom entity from my custom module needs that vocabulary as an entity reference. I think it would be better to always insert the reference vocabulary to make it certain that it will always be there instead of doing it manually by the site.
The problem is the $entity->save()
doesn’t work neither in hook_install or in hook_modules_installed. I don’t know what is the problem. My code is below:
Drupalmy_modulemy_module.install.php
$vocabulary = Drupal::entityTypeManager()->getStorage('taxonomy_vocabulary')
->loadByProperties(['vid' => 'class']);
if (!empty($vocabulary)) {
return;
}
$vocabulary = Drupal::entityTypeManager()->getStorage('taxonomy_vocabulary')
->create([
'vid' => 'class',
'name' => 'Class',
'description' => 'Product classes'
]);
$vocabulary->save();
The code is very similar in Drupalmy_modulemy_module.module. The diference is that I check if $module is the my_module first.
Does somebody have a clue about what is the problem?