ec8b8663e7e5f51fcc54e63ddfc86e52bfe2c048
[yaffs-website] / web / core / modules / taxonomy / src / Entity / Vocabulary.php
1 <?php
2
3 namespace Drupal\taxonomy\Entity;
4
5 use Drupal\Core\Config\Entity\ConfigEntityBundleBase;
6 use Drupal\Core\Entity\EntityStorageInterface;
7 use Drupal\taxonomy\VocabularyInterface;
8
9 /**
10  * Defines the taxonomy vocabulary entity.
11  *
12  * @ConfigEntityType(
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"
21  *   ),
22  *   handlers = {
23  *     "storage" = "Drupal\taxonomy\VocabularyStorage",
24  *     "list_builder" = "Drupal\taxonomy\VocabularyListBuilder",
25  *     "access" = "Drupal\taxonomy\VocabularyAccessControlHandler",
26  *     "form" = {
27  *       "default" = "Drupal\taxonomy\VocabularyForm",
28  *       "reset" = "Drupal\taxonomy\Form\VocabularyResetForm",
29  *       "delete" = "Drupal\taxonomy\Form\VocabularyDeleteForm",
30  *       "overview" = "Drupal\taxonomy\Form\OverviewTerms"
31  *     },
32  *     "route_provider" = {
33  *       "html" = "Drupal\taxonomy\Entity\Routing\VocabularyRouteProvider",
34  *     }
35  *   },
36  *   admin_permission = "administer taxonomy",
37  *   config_prefix = "vocabulary",
38  *   bundle_of = "taxonomy_term",
39  *   entity_keys = {
40  *     "id" = "vid",
41  *     "label" = "name",
42  *     "weight" = "weight"
43  *   },
44  *   links = {
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",
51  *   },
52  *   config_export = {
53  *     "name",
54  *     "vid",
55  *     "description",
56  *     "hierarchy",
57  *     "weight",
58  *   }
59  * )
60  */
61 class Vocabulary extends ConfigEntityBundleBase implements VocabularyInterface {
62
63   /**
64    * The taxonomy vocabulary ID.
65    *
66    * @var string
67    */
68   protected $vid;
69
70   /**
71    * Name of the vocabulary.
72    *
73    * @var string
74    */
75   protected $name;
76
77   /**
78    * Description of the vocabulary.
79    *
80    * @var string
81    */
82   protected $description;
83
84   /**
85    * The type of hierarchy allowed within the vocabulary.
86    *
87    * Possible values:
88    * - VocabularyInterface::HIERARCHY_DISABLED: No parents.
89    * - VocabularyInterface::HIERARCHY_SINGLE: Single parent.
90    * - VocabularyInterface::HIERARCHY_MULTIPL: Multiple parents.
91    *
92    * @var int
93    */
94   protected $hierarchy = VocabularyInterface::HIERARCHY_DISABLED;
95
96   /**
97    * The weight of this vocabulary in relation to other vocabularies.
98    *
99    * @var int
100    */
101   protected $weight = 0;
102
103   /**
104    * {@inheritdoc}
105    */
106   public function getHierarchy() {
107     return $this->hierarchy;
108   }
109
110   /**
111    * {@inheritdoc}
112    */
113   public function setHierarchy($hierarchy) {
114     $this->hierarchy = $hierarchy;
115     return $this;
116   }
117
118   /**
119    * {@inheritdoc}
120    */
121   public function id() {
122     return $this->vid;
123   }
124
125   /**
126    * {@inheritdoc}
127    */
128   public function getDescription() {
129     return $this->description;
130   }
131
132   /**
133    * {@inheritdoc}
134    */
135   public static function preDelete(EntityStorageInterface $storage, array $entities) {
136     parent::preDelete($storage, $entities);
137
138     // Only load terms without a parent, child terms will get deleted too.
139     entity_delete_multiple('taxonomy_term', $storage->getToplevelTids(array_keys($entities)));
140   }
141
142   /**
143    * {@inheritdoc}
144    */
145   public static function postDelete(EntityStorageInterface $storage, array $entities) {
146     parent::postDelete($storage, $entities);
147
148     // Reset caches.
149     $storage->resetCache(array_keys($entities));
150
151     if (reset($entities)->isSyncing()) {
152       return;
153     }
154
155     $vocabularies = [];
156     foreach ($entities as $vocabulary) {
157       $vocabularies[$vocabulary->id()] = $vocabulary->id();
158     }
159     // Load all Taxonomy module fields and delete those which use only this
160     // vocabulary.
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
165       // vocabulary.
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;
172         }
173       }
174       if ($modified_storage) {
175         $allowed_values = $field_storage->getSetting('allowed_values');
176         if (empty($allowed_values)) {
177           $field_storage->delete();
178         }
179         else {
180           // Update the field definition with the new allowed values.
181           $field_storage->save();
182         }
183       }
184     }
185   }
186
187 }