Same name in this branch
  1. 10 core/modules/taxonomy/src/Entity/Term.php \Drupal\taxonomy\Entity\Term
  2. 10 core/modules/taxonomy/src/Plugin/migrate/source/d6/Term.php \Drupal\taxonomy\Plugin\migrate\source\d6\Term
  3. 10 core/modules/taxonomy/src/Plugin/migrate/source/d7/Term.php \Drupal\taxonomy\Plugin\migrate\source\d7\Term
Same name and namespace in other branches
  1. 8.9.x core/modules/taxonomy/src/Entity/Term.php \Drupal\taxonomy\Entity\Term
  2. 9 core/modules/taxonomy/src/Entity/Term.php \Drupal\taxonomy\Entity\Term

Defines the taxonomy term entity.

Plugin annotation


@ContentEntityType(
  id = "taxonomy_term",
  label = @Translation("Taxonomy term"),
  label_collection = @Translation("Taxonomy terms"),
  label_singular = @Translation("taxonomy term"),
  label_plural = @Translation("taxonomy terms"),
  label_count = @PluralTranslation(
    singular = "@count taxonomy term",
    plural = "@count taxonomy terms",
  ),
  bundle_label = @Translation("Vocabulary"),
  handlers = {
    "storage" = "Drupal\taxonomy\TermStorage",
    "storage_schema" = "Drupal\taxonomy\TermStorageSchema",
    "view_builder" = "Drupal\Core\Entity\EntityViewBuilder",
    "list_builder" = "Drupal\Core\Entity\EntityListBuilder",
    "access" = "Drupal\taxonomy\TermAccessControlHandler",
    "views_data" = "Drupal\taxonomy\TermViewsData",
    "form" = {
      "default" = "Drupal\taxonomy\TermForm",
      "delete" = "Drupal\taxonomy\Form\TermDeleteForm",
      "revision-delete" = \Drupal\Core\Entity\Form\RevisionDeleteForm::class,
      "revision-revert" = \Drupal\Core\Entity\Form\RevisionRevertForm::class,
    },
    "route_provider" = {
      "revision" = \Drupal\Core\Entity\Routing\RevisionHtmlRouteProvider::class,
    },
    "translation" = "Drupal\taxonomy\TermTranslationHandler"
  },
  base_table = "taxonomy_term_data",
  data_table = "taxonomy_term_field_data",
  revision_table = "taxonomy_term_revision",
  revision_data_table = "taxonomy_term_field_revision",
  show_revision_ui = TRUE,
  translatable = TRUE,
  entity_keys = {
    "id" = "tid",
    "revision" = "revision_id",
    "bundle" = "vid",
    "label" = "name",
    "langcode" = "langcode",
    "uuid" = "uuid",
    "published" = "status",
  },
  revision_metadata_keys = {
    "revision_user" = "revision_user",
    "revision_created" = "revision_created",
    "revision_log_message" = "revision_log_message",
  },
  bundle_entity_type = "taxonomy_vocabulary",
  field_ui_base_route = "entity.taxonomy_vocabulary.overview_form",
  common_reference_target = TRUE,
  links = {
    "canonical" = "/taxonomy/term/{taxonomy_term}",
    "delete-form" = "/taxonomy/term/{taxonomy_term}/delete",
    "edit-form" = "/taxonomy/term/{taxonomy_term}/edit",
    "create" = "/taxonomy/term",
    "revision" = "/taxonomy/term/{taxonomy_term}/revision/{taxonomy_term_revision}/view",
    "revision-delete-form" = "/taxonomy/term/{taxonomy_term}/revision/{taxonomy_term_revision}/delete",
    "revision-revert-form" = "/taxonomy/term/{taxonomy_term}/revision/{taxonomy_term_revision}/revert",
    "version-history" = "/taxonomy/term/{taxonomy_term}/revisions",
  },
  permission_granularity = "bundle",
  collection_permission = "access taxonomy overview",
  constraints = {
    "TaxonomyHierarchy" = {}
  }
)

Hierarchy

Expanded class hierarchy of Term

51 files declare their use of Term
CommentTokenReplaceTest.php in core/modules/comment/tests/src/Functional/CommentTokenReplaceTest.php
ContentEntityTest.php in core/modules/migrate_drupal/tests/src/Kernel/Plugin/migrate/source/ContentEntityTest.php
DefaultViewsTest.php in core/modules/views/tests/src/Functional/DefaultViewsTest.php
EntityCrudHookTest.php in core/tests/Drupal/KernelTests/Core/Entity/EntityCrudHookTest.php
EntityFilteringThemeTest.php in core/modules/system/tests/src/Functional/Theme/EntityFilteringThemeTest.php

... See full list

2 string references to 'Term'
forum_views_data in core/modules/forum/forum.views.inc
Implements hook_views_data().
TermViewsData::getViewsData in core/modules/taxonomy/src/TermViewsData.php
Returns views data for the entity type.

File

core/modules/taxonomy/src/Entity/Term.php, line 84

Namespace

Drupal\taxonomy\Entity
View source
class Term extends EditorialContentEntityBase implements TermInterface {

  /**
   * {@inheritdoc}
   */
  public static function postDelete(EntityStorageInterface $storage, array $entities) {
    parent::postDelete($storage, $entities);

    // See if any of the term's children are about to be become orphans.
    $orphans = [];

    /** @var \Drupal\taxonomy\TermInterface $term */
    foreach ($entities as $tid => $term) {
      if ($children = $storage
        ->getChildren($term)) {

        /** @var \Drupal\taxonomy\TermInterface $child */
        foreach ($children as $child) {
          $parent = $child
            ->get('parent');

          // Update child parents item list.
          $parent
            ->filter(function ($item) use ($tid) {
            return $item->target_id != $tid;
          });

          // If the term has multiple parents, we don't delete it.
          if ($parent
            ->count()) {
            $child
              ->save();
          }
          else {
            $orphans[] = $child;
          }
        }
      }
    }
    if (!empty($orphans)) {
      $storage
        ->delete($orphans);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function preSave(EntityStorageInterface $storage) {
    parent::preSave($storage);

    // Terms with no parents are mandatory children of <root>.
    if (!$this
      ->get('parent')
      ->count()) {
      $this->parent->target_id = 0;
    }
  }

  /**
   * {@inheritdoc}
   */
  public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {

    /** @var \Drupal\Core\Field\BaseFieldDefinition[] $fields */
    $fields = parent::baseFieldDefinitions($entity_type);

    // @todo Remove the usage of StatusItem in
    //   https://www.drupal.org/project/drupal/issues/2936864.
    $fields['status']
      ->getItemDefinition()
      ->setClass(StatusItem::class);
    $fields['tid']
      ->setLabel(t('Term ID'))
      ->setDescription(t('The term ID.'));
    $fields['uuid']
      ->setDescription(t('The term UUID.'));
    $fields['status']
      ->setDisplayOptions('form', [
      'type' => 'boolean_checkbox',
      'settings' => [
        'display_label' => TRUE,
      ],
      'weight' => 100,
    ])
      ->setDisplayConfigurable('form', TRUE);
    $fields['vid']
      ->setLabel(t('Vocabulary'))
      ->setDescription(t('The vocabulary to which the term is assigned.'));
    $fields['langcode']
      ->setDescription(t('The term language code.'));
    $fields['name'] = BaseFieldDefinition::create('string')
      ->setLabel(t('Name'))
      ->setTranslatable(TRUE)
      ->setRevisionable(TRUE)
      ->setRequired(TRUE)
      ->setSetting('max_length', 255)
      ->setDisplayOptions('view', [
      'label' => 'hidden',
      'type' => 'string',
      'weight' => -5,
    ])
      ->setDisplayOptions('form', [
      'type' => 'string_textfield',
      'weight' => -5,
    ])
      ->setDisplayConfigurable('form', TRUE);
    $fields['description'] = BaseFieldDefinition::create('text_long')
      ->setLabel(t('Description'))
      ->setTranslatable(TRUE)
      ->setRevisionable(TRUE)
      ->setDisplayOptions('view', [
      'label' => 'hidden',
      'type' => 'text_default',
      'weight' => 0,
    ])
      ->setDisplayConfigurable('view', TRUE)
      ->setDisplayOptions('form', [
      'type' => 'text_textfield',
      'weight' => 0,
    ])
      ->setDisplayConfigurable('form', TRUE);
    $fields['weight'] = BaseFieldDefinition::create('integer')
      ->setLabel(t('Weight'))
      ->setDescription(t('The weight of this term in relation to other terms.'))
      ->setDefaultValue(0);
    $fields['parent'] = BaseFieldDefinition::create('entity_reference')
      ->setLabel(t('Term Parents'))
      ->setDescription(t('The parents of this term.'))
      ->setSetting('target_type', 'taxonomy_term')
      ->setCardinality(BaseFieldDefinition::CARDINALITY_UNLIMITED);
    $fields['changed'] = BaseFieldDefinition::create('changed')
      ->setLabel(t('Changed'))
      ->setDescription(t('The time that the term was last edited.'))
      ->setTranslatable(TRUE)
      ->setRevisionable(TRUE);

    // @todo Keep this field hidden until we have a revision UI for terms.
    // @see https://www.drupal.org/project/drupal/issues/2936995
    $fields['revision_log_message']
      ->setDisplayOptions('form', [
      'region' => 'hidden',
    ]);
    return $fields;
  }

  /**
   * {@inheritdoc}
   */
  public static function bundleFieldDefinitions(EntityTypeInterface $entity_type, $bundle, array $base_field_definitions) {

    // Only terms in the same bundle can be a parent.
    $fields['parent'] = clone $base_field_definitions['parent'];
    $fields['parent']
      ->setSetting('handler_settings', [
      'target_bundles' => [
        $bundle => $bundle,
      ],
    ]);
    return $fields;
  }

  /**
   * {@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 (int) $this
      ->get('weight')->value;
  }

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

}

Members

Namesort descending Modifiers Type Description Overrides
AccessibleInterface::access public function Checks data value access. 6
CacheableDependencyInterface::getCacheContexts public function The cache contexts associated with this object. 19
CacheableDependencyInterface::getCacheMaxAge public function The maximum age for which this object may be cached. 19
CacheableDependencyInterface::getCacheTags public function The cache tags associated with this object. 12
EntityChangedTrait::getChangedTime public function Gets the timestamp of the last entity change for the current translation.
EntityChangedTrait::getChangedTimeAcrossTranslations public function Returns the timestamp of the last entity change across all translations.
EntityChangedTrait::setChangedTime public function Sets the timestamp of the last entity change for the current translation.
EntityInterface::bundle public function Gets the bundle of the entity. 1
EntityInterface::create public static function Constructs a new entity object, without permanently saving it. 1
EntityInterface::createDuplicate public function Creates a duplicate of the entity. 2
EntityInterface::delete public function Deletes an entity permanently. 1
EntityInterface::enforceIsNew public function Enforces an entity to be new. 1
EntityInterface::getCacheTagsToInvalidate public function Returns the cache tags that should be used to invalidate caches. 3
EntityInterface::getConfigDependencyKey public function Gets the key that is used to store configuration dependencies. 1
EntityInterface::getConfigDependencyName public function Gets the configuration dependency name. 1
EntityInterface::getConfigTarget public function Gets the configuration target identifier for the entity. 1
EntityInterface::getEntityTypeId public function Gets the ID of the type of the entity. 1
EntityInterface::getOriginalId public function Gets the original ID. 1
EntityInterface::getTypedData public function Gets a typed data object for this entity object. 1
EntityInterface::hasLinkTemplate public function Indicates if a link template exists for a given key. 1
EntityInterface::id public function Gets the identifier. 1
EntityInterface::isNew public function Determines whether the entity is new. 1
EntityInterface::label public function Gets the label of the entity. 3
EntityInterface::language public function Gets the language of the entity. 1
EntityInterface::load public static function Loads an entity. 1
EntityInterface::loadMultiple public static function Loads one or more entities. 1
EntityInterface::postCreate public function Acts on a created entity before hooks are invoked. 1
EntityInterface::postLoad public static function Acts on loaded entities. 2
EntityInterface::postSave public function Acts on a saved entity before the insert or update hook is invoked. 8
EntityInterface::preCreate public static function Changes the values of an entity before it is created. 4
EntityInterface::preDelete public static function Acts on entities before they are deleted and before hooks are invoked. 6
EntityInterface::referencedEntities public function Gets a list of entities referenced by this entity. 2
EntityInterface::save public function Saves an entity permanently. 3
EntityInterface::setOriginalId public function Sets the original ID. 1
EntityInterface::toArray public function Gets an array of all property values. 2
EntityInterface::toLink public function Generates the HTML for a link to this entity. 1
EntityInterface::toUrl public function Gets the URL object for the entity. 1
EntityInterface::uriRelationships public function Gets a list of URI relationships supported by this entity. 1
EntityInterface::uuid public function Gets the entity UUID (Universally Unique Identifier). 1
EntityPublishedTrait::isPublished public function
EntityPublishedTrait::publishedBaseFieldDefinitions public static function Returns an array of base field definitions for publishing status.
EntityPublishedTrait::setPublished public function
EntityPublishedTrait::setUnpublished public function
FieldableEntityInterface::get public function Gets a field item list.
FieldableEntityInterface::getFieldDefinition public function Gets the definition of a contained field.
FieldableEntityInterface::getFieldDefinitions public function Gets an array of field definitions of all contained fields.
FieldableEntityInterface::getFields public function Gets an array of all field item lists.
FieldableEntityInterface::getTranslatableFields public function Gets an array of field item lists for translatable fields.
FieldableEntityInterface::hasField public function Determines whether the entity has a field with the given name.
FieldableEntityInterface::isValidationRequired public function Checks whether entity validation is required before saving the entity.
FieldableEntityInterface::onChange public function Reacts to changes to a field.
FieldableEntityInterface::set public function Sets a field value.
FieldableEntityInterface::setValidationRequired public function Sets whether entity validation is required before saving the entity.
FieldableEntityInterface::validate public function Validates the currently set values. 1
RefinableCacheableDependencyInterface::addCacheableDependency public function Adds a dependency on an object: merges its cacheability metadata.
RefinableCacheableDependencyInterface::addCacheContexts public function Adds cache contexts.
RefinableCacheableDependencyInterface::addCacheTags public function Adds cache tags.
RefinableCacheableDependencyInterface::mergeCacheMaxAge public function Merges the maximum age (in seconds) with the existing maximum age.
RevisionableInterface::getLoadedRevisionId public function Gets the loaded Revision ID of the entity.
RevisionableInterface::getRevisionId public function Gets the revision identifier of the entity.
RevisionableInterface::isDefaultRevision public function Checks if this entity is the default revision.
RevisionableInterface::isLatestRevision public function Checks if this entity is the latest revision.
RevisionableInterface::isNewRevision public function Determines whether a new revision should be created on save.
RevisionableInterface::preSaveRevision public function Acts on a revision before it gets saved. 3
RevisionableInterface::setNewRevision public function Enforces an entity to be saved as a new revision.
RevisionableInterface::updateLoadedRevisionId public function Updates the loaded Revision ID with the revision ID.
RevisionableInterface::wasDefaultRevision public function Checks whether the entity object was a default revision when it was saved.
RevisionLogEntityTrait::getEntityType abstract public function Gets the entity type definition.
RevisionLogEntityTrait::getRevisionCreationTime public function Implements \Drupal\Core\Entity\RevisionLogInterface::getRevisionCreationTime().
RevisionLogEntityTrait::getRevisionLogMessage public function Implements \Drupal\Core\Entity\RevisionLogInterface::getRevisionLogMessage().
RevisionLogEntityTrait::getRevisionUser public function Implements \Drupal\Core\Entity\RevisionLogInterface::getRevisionUser().
RevisionLogEntityTrait::getRevisionUserId public function Implements \Drupal\Core\Entity\RevisionLogInterface::getRevisionUserId().
RevisionLogEntityTrait::revisionLogBaseFieldDefinitions public static function Provides revision-related base field definitions for an entity type.
RevisionLogEntityTrait::setRevisionCreationTime public function Implements \Drupal\Core\Entity\RevisionLogInterface::setRevisionCreationTime().
RevisionLogEntityTrait::setRevisionLogMessage public function Implements \Drupal\Core\Entity\RevisionLogInterface::setRevisionLogMessage().
RevisionLogEntityTrait::setRevisionUser public function Implements \Drupal\Core\Entity\RevisionLogInterface::setRevisionUser().
RevisionLogEntityTrait::setRevisionUserId public function Implements \Drupal\Core\Entity\RevisionLogInterface::setRevisionUserId().
SynchronizableInterface::isSyncing public function Returns whether this entity is being changed as part of a synchronization.
SynchronizableInterface::setSyncing public function Sets the status of the synchronization flag.
Term::baseFieldDefinitions public static function Provides base field definitions for an entity type. Overrides EditorialContentEntityBase::baseFieldDefinitions
Term::bundleFieldDefinitions public static function Provides field definitions for a specific bundle. Overrides FieldableEntityInterface::bundleFieldDefinitions
Term::getDescription public function Gets the term description. Overrides TermInterface::getDescription
Term::getFormat public function Gets the text format name for the term description. Overrides TermInterface::getFormat
Term::getName public function Gets the term name. Overrides TermInterface::getName
Term::getWeight public function Gets the term weight. Overrides TermInterface::getWeight
Term::postDelete public static function Acts on deleted entities before the delete hook is invoked. Overrides EntityInterface::postDelete
Term::preSave public function Acts on an entity before the presave hook is invoked. Overrides EntityInterface::preSave
Term::setDescription public function Sets the term description. Overrides TermInterface::setDescription
Term::setFormat public function Sets the text format name for the term description. Overrides TermInterface::setFormat
Term::setName public function Sets the term name. Overrides TermInterface::setName
Term::setWeight public function Sets the term weight. Overrides TermInterface::setWeight
TranslatableInterface::addTranslation public function Adds a new translation to the translatable object.
TranslatableInterface::getTranslation public function Gets a translation of the data.
TranslatableInterface::getTranslationLanguages public function Returns the languages the data is translated to.
TranslatableInterface::getUntranslated public function Returns the translatable object in the language it was created.
TranslatableInterface::hasTranslation public function Checks there is a translation for the given language code.
TranslatableInterface::hasTranslationChanges public function Determines if the current translation of the entity has unsaved changes.
TranslatableInterface::isDefaultTranslation public function Checks whether the translation is the default one.
TranslatableInterface::isNewTranslation public function Checks whether the translation is new.
TranslatableInterface::isTranslatable public function Returns the translation support status.
TranslatableInterface::removeTranslation public function Removes the translation identified by the given language code.
TranslatableRevisionableInterface::isDefaultTranslationAffectedOnly public function Checks if untranslatable fields should affect only the default translation.
TranslatableRevisionableInterface::isLatestTranslationAffectedRevision public function Checks whether this is the latest revision affecting this translation.
TranslatableRevisionableInterface::isRevisionTranslationAffected public function Checks whether the current translation is affected by the current revision.
TranslatableRevisionableInterface::isRevisionTranslationAffectedEnforced public function Checks if the revision translation affected flag value has been enforced.
TranslatableRevisionableInterface::setRevisionTranslationAffected public function Marks the current revision translation as affected.
TranslatableRevisionableInterface::setRevisionTranslationAffectedEnforced public function Enforces the revision translation affected flag value.