Pull merge.
[yaffs-website] / web / core / modules / taxonomy / src / Entity / Term.php
1 <?php
2
3 namespace Drupal\taxonomy\Entity;
4
5 use Drupal\Core\Entity\ContentEntityBase;
6 use Drupal\Core\Entity\EntityChangedTrait;
7 use Drupal\Core\Entity\EntityPublishedTrait;
8 use Drupal\Core\Entity\EntityStorageInterface;
9 use Drupal\Core\Entity\EntityTypeInterface;
10 use Drupal\Core\Field\BaseFieldDefinition;
11 use Drupal\taxonomy\TermInterface;
12 use Drupal\user\StatusItem;
13
14 /**
15  * Defines the taxonomy term entity.
16  *
17  * @ContentEntityType(
18  *   id = "taxonomy_term",
19  *   label = @Translation("Taxonomy term"),
20  *   label_collection = @Translation("Taxonomy terms"),
21  *   label_singular = @Translation("taxonomy term"),
22  *   label_plural = @Translation("taxonomy terms"),
23  *   label_count = @PluralTranslation(
24  *     singular = "@count taxonomy term",
25  *     plural = "@count taxonomy terms",
26  *   ),
27  *   bundle_label = @Translation("Vocabulary"),
28  *   handlers = {
29  *     "storage" = "Drupal\taxonomy\TermStorage",
30  *     "storage_schema" = "Drupal\taxonomy\TermStorageSchema",
31  *     "view_builder" = "Drupal\Core\Entity\EntityViewBuilder",
32  *     "list_builder" = "Drupal\Core\Entity\EntityListBuilder",
33  *     "access" = "Drupal\taxonomy\TermAccessControlHandler",
34  *     "views_data" = "Drupal\taxonomy\TermViewsData",
35  *     "form" = {
36  *       "default" = "Drupal\taxonomy\TermForm",
37  *       "delete" = "Drupal\taxonomy\Form\TermDeleteForm"
38  *     },
39  *     "translation" = "Drupal\taxonomy\TermTranslationHandler"
40  *   },
41  *   base_table = "taxonomy_term_data",
42  *   data_table = "taxonomy_term_field_data",
43  *   uri_callback = "taxonomy_term_uri",
44  *   translatable = TRUE,
45  *   entity_keys = {
46  *     "id" = "tid",
47  *     "bundle" = "vid",
48  *     "label" = "name",
49  *     "langcode" = "langcode",
50  *     "uuid" = "uuid",
51  *     "published" = "status",
52  *   },
53  *   bundle_entity_type = "taxonomy_vocabulary",
54  *   field_ui_base_route = "entity.taxonomy_vocabulary.overview_form",
55  *   common_reference_target = TRUE,
56  *   links = {
57  *     "canonical" = "/taxonomy/term/{taxonomy_term}",
58  *     "delete-form" = "/taxonomy/term/{taxonomy_term}/delete",
59  *     "edit-form" = "/taxonomy/term/{taxonomy_term}/edit",
60  *     "create" = "/taxonomy/term",
61  *   },
62  *   permission_granularity = "bundle"
63  * )
64  */
65 class Term extends ContentEntityBase implements TermInterface {
66
67   use EntityChangedTrait;
68   use EntityPublishedTrait;
69
70   /**
71    * {@inheritdoc}
72    */
73   public static function postDelete(EntityStorageInterface $storage, array $entities) {
74     parent::postDelete($storage, $entities);
75
76     // See if any of the term's children are about to be become orphans.
77     $orphans = [];
78     /** @var \Drupal\taxonomy\TermInterface $term */
79     foreach ($entities as $tid => $term) {
80       if ($children = $storage->getChildren($term)) {
81         /** @var \Drupal\taxonomy\TermInterface $child */
82         foreach ($children as $child) {
83           $parent = $child->get('parent');
84           // Update child parents item list.
85           $parent->filter(function ($item) use ($tid) {
86             return $item->target_id != $tid;
87           });
88
89           // If the term has multiple parents, we don't delete it.
90           if ($parent->count()) {
91             $child->save();
92           }
93           else {
94             $orphans[] = $child;
95           }
96         }
97       }
98     }
99
100     if (!empty($orphans)) {
101       $storage->delete($orphans);
102     }
103   }
104
105   /**
106    * {@inheritdoc}
107    */
108   public function preSave(EntityStorageInterface $storage) {
109     parent::preSave($storage);
110     // Terms with no parents are mandatory children of <root>.
111     if (!$this->get('parent')->count()) {
112       $this->parent->target_id = 0;
113     }
114   }
115
116   /**
117    * {@inheritdoc}
118    */
119   public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
120     /** @var \Drupal\Core\Field\BaseFieldDefinition[] $fields */
121     $fields = parent::baseFieldDefinitions($entity_type);
122
123     // Add the published field.
124     $fields += static::publishedBaseFieldDefinitions($entity_type);
125     // @todo Remove the usage of StatusItem in
126     //   https://www.drupal.org/project/drupal/issues/2936864.
127     $fields['status']->getItemDefinition()->setClass(StatusItem::class);
128
129     $fields['tid']->setLabel(t('Term ID'))
130       ->setDescription(t('The term ID.'));
131
132     $fields['uuid']->setDescription(t('The term UUID.'));
133
134     $fields['vid']->setLabel(t('Vocabulary'))
135       ->setDescription(t('The vocabulary to which the term is assigned.'));
136
137     $fields['langcode']->setDescription(t('The term language code.'));
138
139     $fields['name'] = BaseFieldDefinition::create('string')
140       ->setLabel(t('Name'))
141       ->setTranslatable(TRUE)
142       ->setRequired(TRUE)
143       ->setSetting('max_length', 255)
144       ->setDisplayOptions('view', [
145         'label' => 'hidden',
146         'type' => 'string',
147         'weight' => -5,
148       ])
149       ->setDisplayOptions('form', [
150         'type' => 'string_textfield',
151         'weight' => -5,
152       ])
153       ->setDisplayConfigurable('form', TRUE);
154
155     $fields['description'] = BaseFieldDefinition::create('text_long')
156       ->setLabel(t('Description'))
157       ->setTranslatable(TRUE)
158       ->setDisplayOptions('view', [
159         'label' => 'hidden',
160         'type' => 'text_default',
161         'weight' => 0,
162       ])
163       ->setDisplayConfigurable('view', TRUE)
164       ->setDisplayOptions('form', [
165         'type' => 'text_textfield',
166         'weight' => 0,
167       ])
168       ->setDisplayConfigurable('form', TRUE);
169
170     $fields['weight'] = BaseFieldDefinition::create('integer')
171       ->setLabel(t('Weight'))
172       ->setDescription(t('The weight of this term in relation to other terms.'))
173       ->setDefaultValue(0);
174
175     $fields['parent'] = BaseFieldDefinition::create('entity_reference')
176       ->setLabel(t('Term Parents'))
177       ->setDescription(t('The parents of this term.'))
178       ->setSetting('target_type', 'taxonomy_term')
179       ->setCardinality(BaseFieldDefinition::CARDINALITY_UNLIMITED);
180
181     $fields['changed'] = BaseFieldDefinition::create('changed')
182       ->setLabel(t('Changed'))
183       ->setDescription(t('The time that the term was last edited.'))
184       ->setTranslatable(TRUE);
185
186     return $fields;
187   }
188
189   /**
190    * {@inheritdoc}
191    */
192   public static function bundleFieldDefinitions(EntityTypeInterface $entity_type, $bundle, array $base_field_definitions) {
193     // Only terms in the same bundle can be a parent.
194     $fields['parent'] = clone $base_field_definitions['parent'];
195     $fields['parent']->setSetting('handler_settings', ['target_bundles' => [$bundle => $bundle]]);
196     return $fields;
197   }
198
199   /**
200    * {@inheritdoc}
201    */
202   public function getDescription() {
203     return $this->get('description')->value;
204   }
205
206   /**
207    * {@inheritdoc}
208    */
209   public function setDescription($description) {
210     $this->set('description', $description);
211     return $this;
212   }
213
214   /**
215    * {@inheritdoc}
216    */
217   public function getFormat() {
218     return $this->get('description')->format;
219   }
220
221   /**
222    * {@inheritdoc}
223    */
224   public function setFormat($format) {
225     $this->get('description')->format = $format;
226     return $this;
227   }
228
229   /**
230    * {@inheritdoc}
231    */
232   public function getName() {
233     return $this->label();
234   }
235
236   /**
237    * {@inheritdoc}
238    */
239   public function setName($name) {
240     $this->set('name', $name);
241     return $this;
242   }
243
244   /**
245    * {@inheritdoc}
246    */
247   public function getWeight() {
248     return $this->get('weight')->value;
249   }
250
251   /**
252    * {@inheritdoc}
253    */
254   public function setWeight($weight) {
255     $this->set('weight', $weight);
256     return $this;
257   }
258
259   /**
260    * {@inheritdoc}
261    */
262   public function getVocabularyId() {
263     @trigger_error('The ' . __METHOD__ . ' method is deprecated since version 8.4.0 and will be removed before 9.0.0. Use ' . __CLASS__ . '::bundle() instead to get the vocabulary ID.', E_USER_DEPRECATED);
264     return $this->bundle();
265   }
266
267 }