1. 8.3.x core/modules/taxonomy/src/Plugin/views/argument_validator/Term.php
  2. 8.3.x core/modules/taxonomy/src/Plugin/migrate/source/d6/Term.php
  3. 8.3.x core/modules/taxonomy/src/Plugin/migrate/source/d7/Term.php
  4. 8.3.x core/modules/taxonomy/src/Entity/Term.php
  5. 8.3.x core/modules/taxonomy/src/Plugin/migrate/source/Term.php
  6. 8.0.x core/modules/taxonomy/src/Entity/Term.php
  7. 8.0.x core/modules/taxonomy/src/Plugin/migrate/source/Term.php
  8. 8.0.x core/modules/taxonomy/src/Plugin/views/argument_validator/Term.php
  9. 8.1.x core/modules/taxonomy/src/Plugin/views/argument_validator/Term.php
  10. 8.1.x core/modules/taxonomy/src/Entity/Term.php
  11. 8.1.x core/modules/taxonomy/src/Plugin/migrate/source/Term.php
  12. 8.2.x core/modules/taxonomy/src/Plugin/migrate/source/d7/Term.php
  13. 8.2.x core/modules/taxonomy/src/Plugin/migrate/source/d6/Term.php
  14. 8.2.x core/modules/taxonomy/src/Plugin/views/argument_validator/Term.php
  15. 8.2.x core/modules/taxonomy/src/Plugin/migrate/source/Term.php
  16. 8.2.x core/modules/taxonomy/src/Entity/Term.php
  17. 8.4.x core/modules/taxonomy/src/Plugin/migrate/source/d7/Term.php
  18. 8.4.x core/modules/taxonomy/src/Plugin/migrate/source/d6/Term.php
  19. 8.4.x core/modules/taxonomy/src/Entity/Term.php
  20. 8.4.x core/modules/taxonomy/src/Plugin/migrate/source/Term.php
  21. 8.4.x core/modules/taxonomy/src/Plugin/views/argument_validator/Term.php

Namespace

Drupal\taxonomy\Entity

File

core/modules/taxonomy/src/Entity/Term.php
View source
  1. <?php
  2. namespace Drupal\taxonomy\Entity;
  3. use Drupal\Core\Entity\ContentEntityBase;
  4. use Drupal\Core\Entity\EntityChangedTrait;
  5. use Drupal\Core\Entity\EntityStorageInterface;
  6. use Drupal\Core\Entity\EntityTypeInterface;
  7. use Drupal\Core\Field\BaseFieldDefinition;
  8. use Drupal\taxonomy\TermInterface;
  9. /**
  10. * Defines the taxonomy term entity.
  11. *
  12. * @ContentEntityType(
  13. * id = "taxonomy_term",
  14. * label = @Translation("Taxonomy term"),
  15. * bundle_label = @Translation("Vocabulary"),
  16. * handlers = {
  17. * "storage" = "Drupal\taxonomy\TermStorage",
  18. * "storage_schema" = "Drupal\taxonomy\TermStorageSchema",
  19. * "view_builder" = "Drupal\taxonomy\TermViewBuilder",
  20. * "access" = "Drupal\taxonomy\TermAccessControlHandler",
  21. * "views_data" = "Drupal\taxonomy\TermViewsData",
  22. * "form" = {
  23. * "default" = "Drupal\taxonomy\TermForm",
  24. * "delete" = "Drupal\taxonomy\Form\TermDeleteForm"
  25. * },
  26. * "translation" = "Drupal\taxonomy\TermTranslationHandler"
  27. * },
  28. * base_table = "taxonomy_term_data",
  29. * data_table = "taxonomy_term_field_data",
  30. * uri_callback = "taxonomy_term_uri",
  31. * translatable = TRUE,
  32. * entity_keys = {
  33. * "id" = "tid",
  34. * "bundle" = "vid",
  35. * "label" = "name",
  36. * "langcode" = "langcode",
  37. * "uuid" = "uuid"
  38. * },
  39. * bundle_entity_type = "taxonomy_vocabulary",
  40. * field_ui_base_route = "entity.taxonomy_vocabulary.overview_form",
  41. * common_reference_target = TRUE,
  42. * links = {
  43. * "canonical" = "/taxonomy/term/{taxonomy_term}",
  44. * "delete-form" = "/taxonomy/term/{taxonomy_term}/delete",
  45. * "edit-form" = "/taxonomy/term/{taxonomy_term}/edit",
  46. * },
  47. * permission_granularity = "bundle"
  48. * )
  49. */
  50. class Term extends ContentEntityBase implements TermInterface {
  51. use EntityChangedTrait;
  52. /**
  53. * {@inheritdoc}
  54. */
  55. public static function postDelete(EntityStorageInterface $storage, array $entities) {
  56. parent::postDelete($storage, $entities);
  57. // See if any of the term's children are about to be become orphans.
  58. $orphans = array();
  59. foreach (array_keys($entities) as $tid) {
  60. if ($children = $storage->loadChildren($tid)) {
  61. foreach ($children as $child) {
  62. // If the term has multiple parents, we don't delete it.
  63. $parents = $storage->loadParents($child->id());
  64. if (empty($parents)) {
  65. $orphans[] = $child->id();
  66. }
  67. }
  68. }
  69. }
  70. // Delete term hierarchy information after looking up orphans but before
  71. // deleting them so that their children/parent information is consistent.
  72. $storage->deleteTermHierarchy(array_keys($entities));
  73. if (!empty($orphans)) {
  74. entity_delete_multiple('taxonomy_term', $orphans);
  75. }
  76. }
  77. /**
  78. * {@inheritdoc}
  79. */
  80. public function postSave(EntityStorageInterface $storage, $update = TRUE) {
  81. parent::postSave($storage, $update);
  82. // Only change the parents if a value is set, keep the existing values if
  83. // not.
  84. if (isset($this->parent->target_id)) {
  85. $storage->deleteTermHierarchy(array($this->id()));
  86. $storage->updateTermHierarchy($this);
  87. }
  88. }
  89. /**
  90. * {@inheritdoc}
  91. */
  92. public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
  93. /** @var \Drupal\Core\Field\BaseFieldDefinition[] $fields */
  94. $fields = parent::baseFieldDefinitions($entity_type);
  95. $fields['tid']->setLabel(t('Term ID'))
  96. ->setDescription(t('The term ID.'));
  97. $fields['uuid']->setDescription(t('The term UUID.'));
  98. $fields['vid']->setLabel(t('Vocabulary'))
  99. ->setDescription(t('The vocabulary to which the term is assigned.'));
  100. $fields['langcode']->setDescription(t('The term language code.'));
  101. $fields['name'] = BaseFieldDefinition::create('string')
  102. ->setLabel(t('Name'))
  103. ->setDescription(t('The term name.'))
  104. ->setTranslatable(TRUE)
  105. ->setRequired(TRUE)
  106. ->setSetting('max_length', 255)
  107. ->setDisplayOptions('view', array(
  108. 'label' => 'hidden',
  109. 'type' => 'string',
  110. 'weight' => -5,
  111. ))
  112. ->setDisplayOptions('form', array(
  113. 'type' => 'string_textfield',
  114. 'weight' => -5,
  115. ))
  116. ->setDisplayConfigurable('form', TRUE);
  117. $fields['description'] = BaseFieldDefinition::create('text_long')
  118. ->setLabel(t('Description'))
  119. ->setDescription(t('A description of the term.'))
  120. ->setTranslatable(TRUE)
  121. ->setDisplayOptions('view', array(
  122. 'label' => 'hidden',
  123. 'type' => 'text_default',
  124. 'weight' => 0,
  125. ))
  126. ->setDisplayConfigurable('view', TRUE)
  127. ->setDisplayOptions('form', array(
  128. 'type' => 'text_textfield',
  129. 'weight' => 0,
  130. ))
  131. ->setDisplayConfigurable('form', TRUE);
  132. $fields['weight'] = BaseFieldDefinition::create('integer')
  133. ->setLabel(t('Weight'))
  134. ->setDescription(t('The weight of this term in relation to other terms.'))
  135. ->setDefaultValue(0);
  136. $fields['parent'] = BaseFieldDefinition::create('entity_reference')
  137. ->setLabel(t('Term Parents'))
  138. ->setDescription(t('The parents of this term.'))
  139. ->setSetting('target_type', 'taxonomy_term')
  140. ->setCardinality(BaseFieldDefinition::CARDINALITY_UNLIMITED)
  141. ->setCustomStorage(TRUE);
  142. $fields['changed'] = BaseFieldDefinition::create('changed')
  143. ->setLabel(t('Changed'))
  144. ->setDescription(t('The time that the term was last edited.'))
  145. ->setTranslatable(TRUE);
  146. return $fields;
  147. }
  148. /**
  149. * {@inheritdoc}
  150. */
  151. public function getDescription() {
  152. return $this->get('description')->value;
  153. }
  154. /**
  155. * {@inheritdoc}
  156. */
  157. public function setDescription($description) {
  158. $this->set('description', $description);
  159. return $this;
  160. }
  161. /**
  162. * {@inheritdoc}
  163. */
  164. public function getFormat() {
  165. return $this->get('description')->format;
  166. }
  167. /**
  168. * {@inheritdoc}
  169. */
  170. public function setFormat($format) {
  171. $this->get('description')->format = $format;
  172. return $this;
  173. }
  174. /**
  175. * {@inheritdoc}
  176. */
  177. public function getName() {
  178. return $this->label();
  179. }
  180. /**
  181. * {@inheritdoc}
  182. */
  183. public function setName($name) {
  184. $this->set('name', $name);
  185. return $this;
  186. }
  187. /**
  188. * {@inheritdoc}
  189. */
  190. public function getWeight() {
  191. return $this->get('weight')->value;
  192. }
  193. /**
  194. * {@inheritdoc}
  195. */
  196. public function setWeight($weight) {
  197. $this->set('weight', $weight);
  198. return $this;
  199. }
  200. /**
  201. * {@inheritdoc}
  202. */
  203. public function getVocabularyId() {
  204. return $this->get('vid')->target_id;
  205. }
  206. }

Classes

Namesort descending Description
Term Defines the taxonomy term entity.