comparison core/modules/taxonomy/src/Entity/Vocabulary.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 1fec387a4317
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
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 * handlers = {
16 * "storage" = "Drupal\taxonomy\VocabularyStorage",
17 * "list_builder" = "Drupal\taxonomy\VocabularyListBuilder",
18 * "form" = {
19 * "default" = "Drupal\taxonomy\VocabularyForm",
20 * "reset" = "Drupal\taxonomy\Form\VocabularyResetForm",
21 * "delete" = "Drupal\taxonomy\Form\VocabularyDeleteForm"
22 * }
23 * },
24 * admin_permission = "administer taxonomy",
25 * config_prefix = "vocabulary",
26 * bundle_of = "taxonomy_term",
27 * entity_keys = {
28 * "id" = "vid",
29 * "label" = "name",
30 * "weight" = "weight"
31 * },
32 * links = {
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",
39 * },
40 * config_export = {
41 * "name",
42 * "vid",
43 * "description",
44 * "hierarchy",
45 * "weight",
46 * }
47 * )
48 */
49 class Vocabulary extends ConfigEntityBundleBase implements VocabularyInterface {
50
51 /**
52 * The taxonomy vocabulary ID.
53 *
54 * @var string
55 */
56 protected $vid;
57
58 /**
59 * Name of the vocabulary.
60 *
61 * @var string
62 */
63 protected $name;
64
65 /**
66 * Description of the vocabulary.
67 *
68 * @var string
69 */
70 protected $description;
71
72 /**
73 * The type of hierarchy allowed within the vocabulary.
74 *
75 * Possible values:
76 * - VocabularyInterface::HIERARCHY_DISABLED: No parents.
77 * - VocabularyInterface::HIERARCHY_SINGLE: Single parent.
78 * - VocabularyInterface::HIERARCHY_MULTIPL: Multiple parents.
79 *
80 * @var int
81 */
82 protected $hierarchy = VocabularyInterface::HIERARCHY_DISABLED;
83
84 /**
85 * The weight of this vocabulary in relation to other vocabularies.
86 *
87 * @var int
88 */
89 protected $weight = 0;
90
91 /**
92 * {@inheritdoc}
93 */
94 public function getHierarchy() {
95 return $this->hierarchy;
96 }
97
98 /**
99 * {@inheritdoc}
100 */
101 public function setHierarchy($hierarchy) {
102 $this->hierarchy = $hierarchy;
103 return $this;
104 }
105
106 /**
107 * {@inheritdoc}
108 */
109 public function id() {
110 return $this->vid;
111 }
112
113 /**
114 * {@inheritdoc}
115 */
116 public function getDescription() {
117 return $this->description;
118 }
119
120 /**
121 * {@inheritdoc}
122 */
123 public static function preDelete(EntityStorageInterface $storage, array $entities) {
124 parent::preDelete($storage, $entities);
125
126 // Only load terms without a parent, child terms will get deleted too.
127 entity_delete_multiple('taxonomy_term', $storage->getToplevelTids(array_keys($entities)));
128 }
129
130 /**
131 * {@inheritdoc}
132 */
133 public static function postDelete(EntityStorageInterface $storage, array $entities) {
134 parent::postDelete($storage, $entities);
135
136 // Reset caches.
137 $storage->resetCache(array_keys($entities));
138
139 if (reset($entities)->isSyncing()) {
140 return;
141 }
142
143 $vocabularies = [];
144 foreach ($entities as $vocabulary) {
145 $vocabularies[$vocabulary->id()] = $vocabulary->id();
146 }
147 // Load all Taxonomy module fields and delete those which use only this
148 // vocabulary.
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
153 // vocabulary.
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;
160 }
161 }
162 if ($modified_storage) {
163 $allowed_values = $field_storage->getSetting('allowed_values');
164 if (empty($allowed_values)) {
165 $field_storage->delete();
166 }
167 else {
168 // Update the field definition with the new allowed values.
169 $field_storage->save();
170 }
171 }
172 }
173 }
174
175 }