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"),
15 * label_singular = @Translation("vocabulary"),
16 * label_plural = @Translation("vocabularies"),
17 * label_collection = @Translation("Taxonomy"),
18 * label_count = @PluralTranslation(
19 * singular = "@count vocabulary",
20 * plural = "@count vocabularies"
23 * "storage" = "Drupal\taxonomy\VocabularyStorage",
24 * "list_builder" = "Drupal\taxonomy\VocabularyListBuilder",
25 * "access" = "Drupal\taxonomy\VocabularyAccessControlHandler",
27 * "default" = "Drupal\taxonomy\VocabularyForm",
28 * "reset" = "Drupal\taxonomy\Form\VocabularyResetForm",
29 * "delete" = "Drupal\taxonomy\Form\VocabularyDeleteForm",
30 * "overview" = "Drupal\taxonomy\Form\OverviewTerms"
32 * "route_provider" = {
33 * "html" = "Drupal\taxonomy\Entity\Routing\VocabularyRouteProvider",
36 * admin_permission = "administer taxonomy",
37 * config_prefix = "vocabulary",
38 * bundle_of = "taxonomy_term",
45 * "add-form" = "/admin/structure/taxonomy/add",
46 * "delete-form" = "/admin/structure/taxonomy/manage/{taxonomy_vocabulary}/delete",
47 * "reset-form" = "/admin/structure/taxonomy/manage/{taxonomy_vocabulary}/reset",
48 * "overview-form" = "/admin/structure/taxonomy/manage/{taxonomy_vocabulary}/overview",
49 * "edit-form" = "/admin/structure/taxonomy/manage/{taxonomy_vocabulary}",
50 * "collection" = "/admin/structure/taxonomy",
61 class Vocabulary extends ConfigEntityBundleBase implements VocabularyInterface {
64 * The taxonomy vocabulary ID.
71 * Name of the vocabulary.
78 * Description of the vocabulary.
82 protected $description;
85 * The type of hierarchy allowed within the vocabulary.
88 * - VocabularyInterface::HIERARCHY_DISABLED: No parents.
89 * - VocabularyInterface::HIERARCHY_SINGLE: Single parent.
90 * - VocabularyInterface::HIERARCHY_MULTIPL: Multiple parents.
94 protected $hierarchy = VocabularyInterface::HIERARCHY_DISABLED;
97 * The weight of this vocabulary in relation to other vocabularies.
101 protected $weight = 0;
106 public function getHierarchy() {
107 return $this->hierarchy;
113 public function setHierarchy($hierarchy) {
114 $this->hierarchy = $hierarchy;
121 public function id() {
128 public function getDescription() {
129 return $this->description;
135 public static function preDelete(EntityStorageInterface $storage, array $entities) {
136 parent::preDelete($storage, $entities);
138 // Only load terms without a parent, child terms will get deleted too.
139 entity_delete_multiple('taxonomy_term', $storage->getToplevelTids(array_keys($entities)));
145 public static function postDelete(EntityStorageInterface $storage, array $entities) {
146 parent::postDelete($storage, $entities);
149 $storage->resetCache(array_keys($entities));
151 if (reset($entities)->isSyncing()) {
156 foreach ($entities as $vocabulary) {
157 $vocabularies[$vocabulary->id()] = $vocabulary->id();
159 // Load all Taxonomy module fields and delete those which use only this
161 $field_storages = entity_load_multiple_by_properties('field_storage_config', ['module' => 'taxonomy']);
162 foreach ($field_storages as $field_storage) {
163 $modified_storage = FALSE;
164 // Term reference fields may reference terms from more than one
166 foreach ($field_storage->getSetting('allowed_values') as $key => $allowed_value) {
167 if (isset($vocabularies[$allowed_value['vocabulary']])) {
168 $allowed_values = $field_storage->getSetting('allowed_values');
169 unset($allowed_values[$key]);
170 $field_storage->setSetting('allowed_values', $allowed_values);
171 $modified_storage = TRUE;
174 if ($modified_storage) {
175 $allowed_values = $field_storage->getSetting('allowed_values');
176 if (empty($allowed_values)) {
177 $field_storage->delete();
180 // Update the field definition with the new allowed values.
181 $field_storage->save();