Term.php 7.06 KB
Newer Older
1 2
<?php

3
namespace Drupal\taxonomy\Entity;
4

5
use Drupal\Core\Entity\ContentEntityBase;
6
use Drupal\Core\Entity\EntityChangedTrait;
7
use Drupal\Core\Entity\EntityStorageInterface;
8
use Drupal\Core\Entity\EntityTypeInterface;
9
use Drupal\Core\Field\BaseFieldDefinition;
10
use Drupal\taxonomy\TermInterface;
11 12 13

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

57 58
  use EntityChangedTrait;

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

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

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

    if (!empty($orphans)) {
84
      $storage->delete($orphans);
85 86 87 88 89 90
    }
  }

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

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

102 103 104
  /**
   * {@inheritdoc}
   */
105
  public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
106 107 108 109 110 111 112 113 114 115 116 117
    /** @var \Drupal\Core\Field\BaseFieldDefinition[] $fields */
    $fields = parent::baseFieldDefinitions($entity_type);

    $fields['tid']->setLabel(t('Term ID'))
      ->setDescription(t('The term ID.'));

    $fields['uuid']->setDescription(t('The term UUID.'));

    $fields['vid']->setLabel(t('Vocabulary'))
      ->setDescription(t('The vocabulary to which the term is assigned.'));

    $fields['langcode']->setDescription(t('The term language code.'));
118

119
    $fields['name'] = BaseFieldDefinition::create('string')
120
      ->setLabel(t('Name'))
121
      ->setTranslatable(TRUE)
122
      ->setRequired(TRUE)
123
      ->setSetting('max_length', 255)
124
      ->setDisplayOptions('view', [
125 126 127
        'label' => 'hidden',
        'type' => 'string',
        'weight' => -5,
128 129
      ])
      ->setDisplayOptions('form', [
130
        'type' => 'string_textfield',
131
        'weight' => -5,
132
      ])
133
      ->setDisplayConfigurable('form', TRUE);
134

135
    $fields['description'] = BaseFieldDefinition::create('text_long')
136
      ->setLabel(t('Description'))
137
      ->setTranslatable(TRUE)
138
      ->setDisplayOptions('view', [
139 140 141
        'label' => 'hidden',
        'type' => 'text_default',
        'weight' => 0,
142
      ])
143
      ->setDisplayConfigurable('view', TRUE)
144
      ->setDisplayOptions('form', [
145 146
        'type' => 'text_textfield',
        'weight' => 0,
147
      ])
148
      ->setDisplayConfigurable('form', TRUE);
149

150
    $fields['weight'] = BaseFieldDefinition::create('integer')
151 152
      ->setLabel(t('Weight'))
      ->setDescription(t('The weight of this term in relation to other terms.'))
153
      ->setDefaultValue(0);
154

155
    $fields['parent'] = BaseFieldDefinition::create('entity_reference')
156 157
      ->setLabel(t('Term Parents'))
      ->setDescription(t('The parents of this term.'))
158
      ->setSetting('target_type', 'taxonomy_term')
159
      ->setCardinality(BaseFieldDefinition::CARDINALITY_UNLIMITED)
160
      ->setCustomStorage(TRUE);
161

162
    $fields['changed'] = BaseFieldDefinition::create('changed')
163
      ->setLabel(t('Changed'))
164 165
      ->setDescription(t('The time that the term was last edited.'))
      ->setTranslatable(TRUE);
166 167

    return $fields;
168 169
  }

170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 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
  /**
   * {@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() {
234 235
    @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);
    return $this->bundle();
236 237
  }

238 239 240 241 242 243 244 245 246 247 248 249 250
  /**
   * {@inheritdoc}
   */
  protected function getFieldsToSkipFromTranslationChangesCheck() {
    // @todo the current implementation of the parent field makes it impossible
    // for ::hasTranslationChanges() to correctly check the field for changes,
    // so it is currently skipped from the comparision and has to be fixed by
    // https://www.drupal.org/node/2843060.
    $fields = parent::getFieldsToSkipFromTranslationChangesCheck();
    $fields[] = 'parent';
    return $fields;
  }

251
}