Term.php 6.73 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\FieldDefinition;
14
use Drupal\Core\Field\FieldStorageDefinitionInterface;
15
use Drupal\Core\TypedData\DataDefinition;
16
use Drupal\taxonomy\TermInterface;
17 18 19

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

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

63 64 65 66 67 68 69
    // 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());
70
          if (empty($parents)) {
71 72 73 74 75 76 77 78
            $orphans[] = $child->id();
          }
        }
      }
    }

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

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

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

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

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

    $fields['uuid'] = FieldDefinition::create('uuid')
      ->setLabel(t('UUID'))
      ->setDescription(t('The term UUID.'))
      ->setReadOnly(TRUE);

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

    $fields['langcode'] = FieldDefinition::create('language')
      ->setLabel(t('Language code'))
      ->setDescription(t('The term language code.'));

    $fields['name'] = FieldDefinition::create('string')
      ->setLabel(t('Name'))
126 127
      ->setDescription(t('The term name.'))
      ->setRequired(TRUE)
128 129 130 131 132 133 134 135 136 137 138
      ->setSetting('max_length', 255)
      ->setDisplayOptions('view', array(
        'label' => 'hidden',
        'type' => 'string',
        'weight' => -5,
      ))
      ->setDisplayOptions('form', array(
        'type' => 'string',
        'weight' => -5,
      ))
      ->setDisplayConfigurable('form', TRUE);
139

140
    $fields['description'] = FieldDefinition::create('text_long')
141
      ->setLabel(t('Description'))
142
      ->setDescription(t('A description of the term.'))
143 144 145 146 147 148 149 150 151 152 153 154
      ->setSetting('text_processing', 1)
      ->setDisplayOptions('view', array(
        'label' => 'hidden',
        'type' => 'text_default',
        'weight' => 0,
      ))
      ->setDisplayConfigurable('view', TRUE)
      ->setDisplayOptions('form', array(
        'type' => 'text_textfield',
        'weight' => 0,
      ))
      ->setDisplayConfigurable('form', TRUE);
155 156 157 158

    $fields['weight'] = FieldDefinition::create('integer')
      ->setLabel(t('Weight'))
      ->setDescription(t('The weight of this term in relation to other terms.'))
159
      ->setDefaultValue(0);
160

161 162
    // @todo Convert this to an entity_reference field, see
    // https://drupal.org/node/1915056
163 164 165
    $fields['parent'] = FieldDefinition::create('integer')
      ->setLabel(t('Term Parents'))
      ->setDescription(t('The parents of this term.'))
166
      ->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED)
167
      // Save new terms with no parents by default.
168
      ->setDefaultValue(0)
169 170
      ->setSetting('unsigned', TRUE)
      ->addConstraint('TermParent', array());
171

172
    $fields['changed'] = FieldDefinition::create('changed')
173
      ->setLabel(t('Changed'))
174
      ->setDescription(t('The time that the term was last edited.'));
175 176

    return $fields;
177 178
  }

179 180 181 182
  /**
   * {@inheritdoc}
   */
  public function getChangedTime() {
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 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
    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;
251 252
  }

253
}