3 namespace Drupal\taxonomy\Entity;
5 use Drupal\Core\Config\Entity\ConfigEntityBundleBase;
6 use Drupal\Core\Entity\EntityStorageInterface;
7 use Drupal\taxonomy\VocabularyInterface;
10 * Defines the taxonomy vocabulary entity.
13 * id = "taxonomy_vocabulary",
14 * label = @Translation("Taxonomy vocabulary"),
16 * "storage" = "Drupal\taxonomy\VocabularyStorage",
17 * "list_builder" = "Drupal\taxonomy\VocabularyListBuilder",
19 * "default" = "Drupal\taxonomy\VocabularyForm",
20 * "reset" = "Drupal\taxonomy\Form\VocabularyResetForm",
21 * "delete" = "Drupal\taxonomy\Form\VocabularyDeleteForm"
24 * admin_permission = "administer taxonomy",
25 * config_prefix = "vocabulary",
26 * bundle_of = "taxonomy_term",
33 * "add-form" = "/admin/structure/taxonomy/manage/{taxonomy_vocabulary}/add",
34 * "delete-form" = "/admin/structure/taxonomy/manage/{taxonomy_vocabulary}/delete",
35 * "reset-form" = "/admin/structure/taxonomy/manage/{taxonomy_vocabulary}/reset",
36 * "overview-form" = "/admin/structure/taxonomy/manage/{taxonomy_vocabulary}/overview",
37 * "edit-form" = "/admin/structure/taxonomy/manage/{taxonomy_vocabulary}",
38 * "collection" = "/admin/structure/taxonomy",
49 class Vocabulary extends ConfigEntityBundleBase implements VocabularyInterface {
52 * The taxonomy vocabulary ID.
59 * Name of the vocabulary.
66 * Description of the vocabulary.
70 protected $description;
73 * The type of hierarchy allowed within the vocabulary.
76 * - VocabularyInterface::HIERARCHY_DISABLED: No parents.
77 * - VocabularyInterface::HIERARCHY_SINGLE: Single parent.
78 * - VocabularyInterface::HIERARCHY_MULTIPL: Multiple parents.
82 protected $hierarchy = VocabularyInterface::HIERARCHY_DISABLED;
85 * The weight of this vocabulary in relation to other vocabularies.
89 protected $weight = 0;
94 public function getHierarchy() {
95 return $this->hierarchy;
101 public function setHierarchy($hierarchy) {
102 $this->hierarchy = $hierarchy;
109 public function id() {
116 public function getDescription() {
117 return $this->description;
123 public static function preDelete(EntityStorageInterface $storage, array $entities) {
124 parent::preDelete($storage, $entities);
126 // Only load terms without a parent, child terms will get deleted too.
127 entity_delete_multiple('taxonomy_term', $storage->getToplevelTids(array_keys($entities)));
133 public static function postDelete(EntityStorageInterface $storage, array $entities) {
134 parent::postDelete($storage, $entities);
137 $storage->resetCache(array_keys($entities));
139 if (reset($entities)->isSyncing()) {
144 foreach ($entities as $vocabulary) {
145 $vocabularies[$vocabulary->id()] = $vocabulary->id();
147 // Load all Taxonomy module fields and delete those which use only this
149 $field_storages = entity_load_multiple_by_properties('field_storage_config', ['module' => 'taxonomy']);
150 foreach ($field_storages as $field_storage) {
151 $modified_storage = FALSE;
152 // Term reference fields may reference terms from more than one
154 foreach ($field_storage->getSetting('allowed_values') as $key => $allowed_value) {
155 if (isset($vocabularies[$allowed_value['vocabulary']])) {
156 $allowed_values = $field_storage->getSetting('allowed_values');
157 unset($allowed_values[$key]);
158 $field_storage->setSetting('allowed_values', $allowed_values);
159 $modified_storage = TRUE;
162 if ($modified_storage) {
163 $allowed_values = $field_storage->getSetting('allowed_values');
164 if (empty($allowed_values)) {
165 $field_storage->delete();
168 // Update the field definition with the new allowed values.
169 $field_storage->save();