3 namespace Drupal\taxonomy\Entity;
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;
15 * Defines the taxonomy term entity.
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",
27 * bundle_label = @Translation("Vocabulary"),
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",
36 * "default" = "Drupal\taxonomy\TermForm",
37 * "delete" = "Drupal\taxonomy\Form\TermDeleteForm"
39 * "translation" = "Drupal\taxonomy\TermTranslationHandler"
41 * base_table = "taxonomy_term_data",
42 * data_table = "taxonomy_term_field_data",
43 * uri_callback = "taxonomy_term_uri",
44 * translatable = TRUE,
49 * "langcode" = "langcode",
51 * "published" = "status",
53 * bundle_entity_type = "taxonomy_vocabulary",
54 * field_ui_base_route = "entity.taxonomy_vocabulary.overview_form",
55 * common_reference_target = TRUE,
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",
62 * permission_granularity = "bundle"
65 class Term extends ContentEntityBase implements TermInterface {
67 use EntityChangedTrait;
68 use EntityPublishedTrait;
73 public static function postDelete(EntityStorageInterface $storage, array $entities) {
74 parent::postDelete($storage, $entities);
76 // See if any of the term's children are about to be become 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;
89 // If the term has multiple parents, we don't delete it.
90 if ($parent->count()) {
100 if (!empty($orphans)) {
101 $storage->delete($orphans);
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;
119 public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
120 /** @var \Drupal\Core\Field\BaseFieldDefinition[] $fields */
121 $fields = parent::baseFieldDefinitions($entity_type);
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);
129 $fields['tid']->setLabel(t('Term ID'))
130 ->setDescription(t('The term ID.'));
132 $fields['uuid']->setDescription(t('The term UUID.'));
134 $fields['vid']->setLabel(t('Vocabulary'))
135 ->setDescription(t('The vocabulary to which the term is assigned.'));
137 $fields['langcode']->setDescription(t('The term language code.'));
139 $fields['name'] = BaseFieldDefinition::create('string')
140 ->setLabel(t('Name'))
141 ->setTranslatable(TRUE)
143 ->setSetting('max_length', 255)
144 ->setDisplayOptions('view', [
149 ->setDisplayOptions('form', [
150 'type' => 'string_textfield',
153 ->setDisplayConfigurable('form', TRUE);
155 $fields['description'] = BaseFieldDefinition::create('text_long')
156 ->setLabel(t('Description'))
157 ->setTranslatable(TRUE)
158 ->setDisplayOptions('view', [
160 'type' => 'text_default',
163 ->setDisplayConfigurable('view', TRUE)
164 ->setDisplayOptions('form', [
165 'type' => 'text_textfield',
168 ->setDisplayConfigurable('form', TRUE);
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);
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);
181 $fields['changed'] = BaseFieldDefinition::create('changed')
182 ->setLabel(t('Changed'))
183 ->setDescription(t('The time that the term was last edited.'))
184 ->setTranslatable(TRUE);
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]]);
202 public function getDescription() {
203 return $this->get('description')->value;
209 public function setDescription($description) {
210 $this->set('description', $description);
217 public function getFormat() {
218 return $this->get('description')->format;
224 public function setFormat($format) {
225 $this->get('description')->format = $format;
232 public function getName() {
233 return $this->label();
239 public function setName($name) {
240 $this->set('name', $name);
247 public function getWeight() {
248 return $this->get('weight')->value;
254 public function setWeight($weight) {
255 $this->set('weight', $weight);
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();