Same filename in this branch
- 10 core/modules/taxonomy/src/Entity/Term.php
- 10 core/modules/taxonomy/src/Plugin/migrate/source/d6/Term.php
- 10 core/modules/taxonomy/src/Plugin/migrate/source/d7/Term.php
Same filename and directory in other branches
- 8.9.x core/modules/taxonomy/src/Entity/Term.php
- 9 core/modules/taxonomy/src/Entity/Term.php
Namespace
Drupal\taxonomy\Entity
File
core/modules/taxonomy/src/Entity/Term.php
View source
<?php
namespace Drupal\taxonomy\Entity;
use Drupal\Core\Entity\EditorialContentEntityBase;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\taxonomy\TermInterface;
use Drupal\user\StatusItem;
class Term extends EditorialContentEntityBase implements TermInterface {
public static function postDelete(EntityStorageInterface $storage, array $entities) {
parent::postDelete($storage, $entities);
$orphans = [];
foreach ($entities as $tid => $term) {
if ($children = $storage
->getChildren($term)) {
foreach ($children as $child) {
$parent = $child
->get('parent');
$parent
->filter(function ($item) use ($tid) {
return $item->target_id != $tid;
});
if ($parent
->count()) {
$child
->save();
}
else {
$orphans[] = $child;
}
}
}
}
if (!empty($orphans)) {
$storage
->delete($orphans);
}
}
public function preSave(EntityStorageInterface $storage) {
parent::preSave($storage);
if (!$this
->get('parent')
->count()) {
$this->parent->target_id = 0;
}
}
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields = parent::baseFieldDefinitions($entity_type);
$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);
$fields['revision_log_message']
->setDisplayOptions('form', [
'region' => 'hidden',
]);
return $fields;
}
public static function bundleFieldDefinitions(EntityTypeInterface $entity_type, $bundle, array $base_field_definitions) {
$fields['parent'] = clone $base_field_definitions['parent'];
$fields['parent']
->setSetting('handler_settings', [
'target_bundles' => [
$bundle => $bundle,
],
]);
return $fields;
}
public function getDescription() {
return $this
->get('description')->value;
}
public function setDescription($description) {
$this
->set('description', $description);
return $this;
}
public function getFormat() {
return $this
->get('description')->format;
}
public function setFormat($format) {
$this
->get('description')->format = $format;
return $this;
}
public function getName() {
return $this
->label() ?? '';
}
public function setName($name) {
$this
->set('name', $name);
return $this;
}
public function getWeight() {
return (int) $this
->get('weight')->value;
}
public function setWeight($weight) {
$this
->set('weight', $weight);
return $this;
}
}
Classes
Name |
Description |
Term |
Defines the taxonomy term entity. |