Term.php 6.93 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Contains \Drupal\taxonomy\Entity\Term.
6 7
 */

8
namespace Drupal\taxonomy\Entity;
9

10
use Drupal\Core\Entity\ContentEntityBase;
11
use Drupal\Core\Entity\EntityStorageInterface;
12
use Drupal\Core\Entity\EntityTypeInterface;
13
use Drupal\Core\Field\BaseFieldDefinition;
14
use Drupal\taxonomy\TermInterface;
15 16 17

/**
 * Defines the taxonomy term entity.
18
 *
19
 * @ContentEntityType(
20 21
 *   id = "taxonomy_term",
 *   label = @Translation("Taxonomy term"),
22
 *   bundle_label = @Translation("Vocabulary"),
23
 *   handlers = {
24
 *     "storage" = "Drupal\taxonomy\TermStorage",
25
 *     "storage_schema" = "Drupal\taxonomy\TermStorageSchema",
26
 *     "view_builder" = "Drupal\taxonomy\TermViewBuilder",
27
 *     "access" = "Drupal\taxonomy\TermAccessControlHandler",
28
 *     "views_data" = "Drupal\taxonomy\TermViewsData",
29
 *     "form" = {
30
 *       "default" = "Drupal\taxonomy\TermForm",
31
 *       "delete" = "Drupal\taxonomy\Form\TermDeleteForm"
32
 *     },
33
 *     "translation" = "Drupal\taxonomy\TermTranslationHandler"
34 35
 *   },
 *   base_table = "taxonomy_term_data",
36
 *   data_table = "taxonomy_term_field_data",
37
 *   uri_callback = "taxonomy_term_uri",
38
 *   translatable = TRUE,
39 40
 *   entity_keys = {
 *     "id" = "tid",
41
 *     "bundle" = "vid",
42
 *     "label" = "name",
43
 *     "langcode" = "langcode",
44 45
 *     "uuid" = "uuid"
 *   },
46
 *   bundle_entity_type = "taxonomy_vocabulary",
47
 *   field_ui_base_route = "entity.taxonomy_vocabulary.overview_form",
48
 *   links = {
49 50 51
 *     "canonical" = "entity.taxonomy_term.canonical",
 *     "delete-form" = "entity.taxonomy_term.delete_form",
 *     "edit-form" = "entity.taxonomy_term.edit_form",
52
 *   },
53
 *   permission_granularity = "bundle"
54
 * )
55
 */
56
class Term extends ContentEntityBase implements TermInterface {
57

58 59 60
  /**
   * {@inheritdoc}
   */
61 62
  public static function postDelete(EntityStorageInterface $storage, array $entities) {
    parent::postDelete($storage, $entities);
63

64 65 66 67 68 69 70
    // See if any of the term's children are about to be become orphans.
    $orphans = array();
    foreach (array_keys($entities) as $tid) {
      if ($children = taxonomy_term_load_children($tid)) {
        foreach ($children as $child) {
          // If the term has multiple parents, we don't delete it.
          $parents = taxonomy_term_load_parents($child->id());
71
          if (empty($parents)) {
72 73 74 75 76 77 78 79
            $orphans[] = $child->id();
          }
        }
      }
    }

    // Delete term hierarchy information after looking up orphans but before
    // deleting them so that their children/parent information is consistent.
80
    $storage->deleteTermHierarchy(array_keys($entities));
81 82 83 84 85 86 87 88 89

    if (!empty($orphans)) {
      entity_delete_multiple('taxonomy_term', $orphans);
    }
  }

  /**
   * {@inheritdoc}
   */
90 91
  public function postSave(EntityStorageInterface $storage, $update = TRUE) {
    parent::postSave($storage, $update);
92

93 94
    // Only change the parents if a value is set, keep the existing values if
    // not.
95
    if (isset($this->parent->target_id)) {
96 97
      $storage->deleteTermHierarchy(array($this->id()));
      $storage->updateTermHierarchy($this);
98 99 100
    }
  }

101 102 103
  /**
   * {@inheritdoc}
   */
104
  public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
105
    $fields['tid'] = BaseFieldDefinition::create('integer')
106 107
      ->setLabel(t('Term ID'))
      ->setDescription(t('The term ID.'))
108 109
      ->setReadOnly(TRUE)
      ->setSetting('unsigned', TRUE);
110

111
    $fields['uuid'] = BaseFieldDefinition::create('uuid')
112 113 114 115
      ->setLabel(t('UUID'))
      ->setDescription(t('The term UUID.'))
      ->setReadOnly(TRUE);

116
    $fields['vid'] = BaseFieldDefinition::create('entity_reference')
117 118
      ->setLabel(t('Vocabulary'))
      ->setDescription(t('The vocabulary to which the term is assigned.'))
119
      ->setSetting('target_type', 'taxonomy_vocabulary');
120

121
    $fields['langcode'] = BaseFieldDefinition::create('language')
122 123 124 125 126 127 128 129 130
      ->setLabel(t('Language'))
      ->setDescription(t('The term language code.'))
      ->setDisplayOptions('view', array(
        'type' => 'hidden',
      ))
      ->setDisplayOptions('form', array(
        'type' => 'language_select',
        'weight' => 2,
      ));
131

132
    $fields['name'] = BaseFieldDefinition::create('string')
133
      ->setLabel(t('Name'))
134
      ->setDescription(t('The term name.'))
135
      ->setTranslatable(TRUE)
136
      ->setRequired(TRUE)
137 138 139 140 141 142 143
      ->setSetting('max_length', 255)
      ->setDisplayOptions('view', array(
        'label' => 'hidden',
        'type' => 'string',
        'weight' => -5,
      ))
      ->setDisplayOptions('form', array(
144
        'type' => 'string_textfield',
145 146 147
        'weight' => -5,
      ))
      ->setDisplayConfigurable('form', TRUE);
148

149
    $fields['description'] = BaseFieldDefinition::create('text_long')
150
      ->setLabel(t('Description'))
151
      ->setDescription(t('A description of the term.'))
152
      ->setTranslatable(TRUE)
153 154 155 156 157 158 159 160 161 162 163
      ->setDisplayOptions('view', array(
        'label' => 'hidden',
        'type' => 'text_default',
        'weight' => 0,
      ))
      ->setDisplayConfigurable('view', TRUE)
      ->setDisplayOptions('form', array(
        'type' => 'text_textfield',
        'weight' => 0,
      ))
      ->setDisplayConfigurable('form', TRUE);
164

165
    $fields['weight'] = BaseFieldDefinition::create('integer')
166 167
      ->setLabel(t('Weight'))
      ->setDescription(t('The weight of this term in relation to other terms.'))
168
      ->setDefaultValue(0);
169

170
    $fields['parent'] = BaseFieldDefinition::create('entity_reference')
171 172
      ->setLabel(t('Term Parents'))
      ->setDescription(t('The parents of this term.'))
173
      ->setSetting('target_type', 'taxonomy_term')
174
      ->setCardinality(BaseFieldDefinition::CARDINALITY_UNLIMITED)
175
      ->setCustomStorage(TRUE);
176

177
    $fields['changed'] = BaseFieldDefinition::create('changed')
178
      ->setLabel(t('Changed'))
179
      ->setDescription(t('The time that the term was last edited.'));
180 181

    return $fields;
182 183
  }

184 185 186 187
  /**
   * {@inheritdoc}
   */
  public function getChangedTime() {
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
    return $this->get('changed')->value;
  }

  /**
   * {@inheritdoc}
   */
  public function getDescription() {
    return $this->get('description')->value;
  }

  /**
   * {@inheritdoc}
   */
  public function setDescription($description) {
    $this->set('description', $description);
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function getFormat() {
    return $this->get('description')->format;
  }

  /**
   * {@inheritdoc}
   */
  public function setFormat($format) {
    $this->get('description')->format = $format;
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function getName() {
    return $this->label();
  }

  /**
   * {@inheritdoc}
   */
  public function setName($name) {
    $this->set('name', $name);
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function getWeight() {
    return $this->get('weight')->value;
  }

  /**
   * {@inheritdoc}
   */
  public function setWeight($weight) {
    $this->set('weight', $weight);
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function getVocabularyId() {
    return $this->get('vid')->target_id;
256 257
  }

258
}