Drupal 8.0.0 beta 12. More info: https://www.drupal.org/node/2514176
This commit is contained in:
commit
9921556621
13277 changed files with 1459781 additions and 0 deletions
264
core/modules/taxonomy/src/Entity/Term.php
Normal file
264
core/modules/taxonomy/src/Entity/Term.php
Normal file
|
@ -0,0 +1,264 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\taxonomy\Entity\Term.
|
||||
*/
|
||||
|
||||
namespace Drupal\taxonomy\Entity;
|
||||
|
||||
use Drupal\Core\Entity\ContentEntityBase;
|
||||
use Drupal\Core\Entity\EntityChangedTrait;
|
||||
use Drupal\Core\Entity\EntityStorageInterface;
|
||||
use Drupal\Core\Entity\EntityTypeInterface;
|
||||
use Drupal\Core\Field\BaseFieldDefinition;
|
||||
use Drupal\taxonomy\TermInterface;
|
||||
|
||||
/**
|
||||
* Defines the taxonomy term entity.
|
||||
*
|
||||
* @ContentEntityType(
|
||||
* id = "taxonomy_term",
|
||||
* label = @Translation("Taxonomy term"),
|
||||
* bundle_label = @Translation("Vocabulary"),
|
||||
* handlers = {
|
||||
* "storage" = "Drupal\taxonomy\TermStorage",
|
||||
* "storage_schema" = "Drupal\taxonomy\TermStorageSchema",
|
||||
* "view_builder" = "Drupal\taxonomy\TermViewBuilder",
|
||||
* "access" = "Drupal\taxonomy\TermAccessControlHandler",
|
||||
* "views_data" = "Drupal\taxonomy\TermViewsData",
|
||||
* "form" = {
|
||||
* "default" = "Drupal\taxonomy\TermForm",
|
||||
* "delete" = "Drupal\taxonomy\Form\TermDeleteForm"
|
||||
* },
|
||||
* "translation" = "Drupal\taxonomy\TermTranslationHandler"
|
||||
* },
|
||||
* base_table = "taxonomy_term_data",
|
||||
* data_table = "taxonomy_term_field_data",
|
||||
* uri_callback = "taxonomy_term_uri",
|
||||
* translatable = TRUE,
|
||||
* entity_keys = {
|
||||
* "id" = "tid",
|
||||
* "bundle" = "vid",
|
||||
* "label" = "name",
|
||||
* "langcode" = "langcode",
|
||||
* "uuid" = "uuid"
|
||||
* },
|
||||
* 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",
|
||||
* },
|
||||
* permission_granularity = "bundle"
|
||||
* )
|
||||
*/
|
||||
class Term extends ContentEntityBase implements TermInterface {
|
||||
|
||||
use EntityChangedTrait;
|
||||
|
||||
/**
|
||||
* {@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 = array();
|
||||
foreach (array_keys($entities) as $tid) {
|
||||
if ($children = $storage->loadChildren($tid)) {
|
||||
foreach ($children as $child) {
|
||||
// If the term has multiple parents, we don't delete it.
|
||||
$parents = $storage->loadParents($child->id());
|
||||
if (empty($parents)) {
|
||||
$orphans[] = $child->id();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Delete term hierarchy information after looking up orphans but before
|
||||
// deleting them so that their children/parent information is consistent.
|
||||
$storage->deleteTermHierarchy(array_keys($entities));
|
||||
|
||||
if (!empty($orphans)) {
|
||||
entity_delete_multiple('taxonomy_term', $orphans);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function postSave(EntityStorageInterface $storage, $update = TRUE) {
|
||||
parent::postSave($storage, $update);
|
||||
|
||||
// Only change the parents if a value is set, keep the existing values if
|
||||
// not.
|
||||
if (isset($this->parent->target_id)) {
|
||||
$storage->deleteTermHierarchy(array($this->id()));
|
||||
$storage->updateTermHierarchy($this);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
|
||||
$fields['tid'] = BaseFieldDefinition::create('integer')
|
||||
->setLabel(t('Term ID'))
|
||||
->setDescription(t('The term ID.'))
|
||||
->setReadOnly(TRUE)
|
||||
->setSetting('unsigned', TRUE);
|
||||
|
||||
$fields['uuid'] = BaseFieldDefinition::create('uuid')
|
||||
->setLabel(t('UUID'))
|
||||
->setDescription(t('The term UUID.'))
|
||||
->setReadOnly(TRUE);
|
||||
|
||||
$fields['vid'] = BaseFieldDefinition::create('entity_reference')
|
||||
->setLabel(t('Vocabulary'))
|
||||
->setDescription(t('The vocabulary to which the term is assigned.'))
|
||||
->setSetting('target_type', 'taxonomy_vocabulary');
|
||||
|
||||
$fields['langcode'] = BaseFieldDefinition::create('language')
|
||||
->setLabel(t('Language'))
|
||||
->setDescription(t('The term language code.'))
|
||||
->setTranslatable(TRUE)
|
||||
->setDisplayOptions('view', array(
|
||||
'type' => 'hidden',
|
||||
))
|
||||
->setDisplayOptions('form', array(
|
||||
'type' => 'language_select',
|
||||
'weight' => 2,
|
||||
));
|
||||
|
||||
$fields['name'] = BaseFieldDefinition::create('string')
|
||||
->setLabel(t('Name'))
|
||||
->setDescription(t('The term name.'))
|
||||
->setTranslatable(TRUE)
|
||||
->setRequired(TRUE)
|
||||
->setSetting('max_length', 255)
|
||||
->setDisplayOptions('view', array(
|
||||
'label' => 'hidden',
|
||||
'type' => 'string',
|
||||
'weight' => -5,
|
||||
))
|
||||
->setDisplayOptions('form', array(
|
||||
'type' => 'string_textfield',
|
||||
'weight' => -5,
|
||||
))
|
||||
->setDisplayConfigurable('form', TRUE);
|
||||
|
||||
$fields['description'] = BaseFieldDefinition::create('text_long')
|
||||
->setLabel(t('Description'))
|
||||
->setDescription(t('A description of the term.'))
|
||||
->setTranslatable(TRUE)
|
||||
->setDisplayOptions('view', array(
|
||||
'label' => 'hidden',
|
||||
'type' => 'text_default',
|
||||
'weight' => 0,
|
||||
))
|
||||
->setDisplayConfigurable('view', TRUE)
|
||||
->setDisplayOptions('form', array(
|
||||
'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)
|
||||
->setCustomStorage(TRUE);
|
||||
|
||||
$fields['changed'] = BaseFieldDefinition::create('changed')
|
||||
->setLabel(t('Changed'))
|
||||
->setDescription(t('The time that the term was last edited.'))
|
||||
->setTranslatable(TRUE);
|
||||
|
||||
return $fields;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getChangedTime() {
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
222
core/modules/taxonomy/src/Entity/Vocabulary.php
Normal file
222
core/modules/taxonomy/src/Entity/Vocabulary.php
Normal file
|
@ -0,0 +1,222 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\taxonomy\Entity\Vocabulary.
|
||||
*/
|
||||
|
||||
namespace Drupal\taxonomy\Entity;
|
||||
|
||||
use Drupal\Core\Config\Entity\ConfigEntityBundleBase;
|
||||
use Drupal\Core\Entity\EntityStorageInterface;
|
||||
use Drupal\taxonomy\VocabularyInterface;
|
||||
|
||||
/**
|
||||
* Defines the taxonomy vocabulary entity.
|
||||
*
|
||||
* @ConfigEntityType(
|
||||
* id = "taxonomy_vocabulary",
|
||||
* label = @Translation("Taxonomy vocabulary"),
|
||||
* handlers = {
|
||||
* "storage" = "Drupal\taxonomy\VocabularyStorage",
|
||||
* "list_builder" = "Drupal\taxonomy\VocabularyListBuilder",
|
||||
* "form" = {
|
||||
* "default" = "Drupal\taxonomy\VocabularyForm",
|
||||
* "reset" = "Drupal\taxonomy\Form\VocabularyResetForm",
|
||||
* "delete" = "Drupal\taxonomy\Form\VocabularyDeleteForm"
|
||||
* }
|
||||
* },
|
||||
* admin_permission = "administer taxonomy",
|
||||
* config_prefix = "vocabulary",
|
||||
* bundle_of = "taxonomy_term",
|
||||
* entity_keys = {
|
||||
* "id" = "vid",
|
||||
* "label" = "name",
|
||||
* "weight" = "weight"
|
||||
* },
|
||||
* links = {
|
||||
* "add-form" = "/admin/structure/taxonomy/manage/{taxonomy_vocabulary}/add",
|
||||
* "delete-form" = "/admin/structure/taxonomy/manage/{taxonomy_vocabulary}/delete",
|
||||
* "reset-form" = "/admin/structure/taxonomy/manage/{taxonomy_vocabulary}/reset",
|
||||
* "overview-form" = "/admin/structure/taxonomy/manage/{taxonomy_vocabulary}/overview",
|
||||
* "edit-form" = "/admin/structure/taxonomy/manage/{taxonomy_vocabulary}",
|
||||
* "collection" = "/admin/structure/taxonomy",
|
||||
* },
|
||||
* config_export = {
|
||||
* "name",
|
||||
* "vid",
|
||||
* "description",
|
||||
* "hierarchy",
|
||||
* "weight",
|
||||
* }
|
||||
* )
|
||||
*/
|
||||
class Vocabulary extends ConfigEntityBundleBase implements VocabularyInterface {
|
||||
|
||||
/**
|
||||
* The taxonomy vocabulary ID.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $vid;
|
||||
|
||||
/**
|
||||
* Name of the vocabulary.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $name;
|
||||
|
||||
/**
|
||||
* Description of the vocabulary.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description;
|
||||
|
||||
/**
|
||||
* The type of hierarchy allowed within the vocabulary.
|
||||
*
|
||||
* Possible values:
|
||||
* - TAXONOMY_HIERARCHY_DISABLED: No parents.
|
||||
* - TAXONOMY_HIERARCHY_SINGLE: Single parent.
|
||||
* - TAXONOMY_HIERARCHY_MULTIPLE: Multiple parents.
|
||||
*
|
||||
* @var integer
|
||||
*/
|
||||
protected $hierarchy = TAXONOMY_HIERARCHY_DISABLED;
|
||||
|
||||
/**
|
||||
* The weight of this vocabulary in relation to other vocabularies.
|
||||
*
|
||||
* @var integer
|
||||
*/
|
||||
protected $weight = 0;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getHierarchy() {
|
||||
return $this->hierarchy;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function setHierarchy($hierarchy) {
|
||||
$this->hierarchy = $hierarchy;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function id() {
|
||||
return $this->vid;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getDescription() {
|
||||
return $this->description;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function postSave(EntityStorageInterface $storage, $update = TRUE) {
|
||||
parent::postSave($storage, $update);
|
||||
|
||||
if ($update && $this->getOriginalId() != $this->id() && !$this->isSyncing()) {
|
||||
// Reflect machine name changes in the definitions of existing 'taxonomy'
|
||||
// fields.
|
||||
$field_ids = array();
|
||||
$field_map = \Drupal::entityManager()->getFieldMapByFieldType('entity_reference');
|
||||
foreach ($field_map as $entity_type => $field_storages) {
|
||||
foreach ($field_storages as $field_storage => $info) {
|
||||
$field_ids[] = $entity_type . '.' . $field_storage;
|
||||
}
|
||||
}
|
||||
|
||||
$field_storages = \Drupal::entityManager()->getStorage('field_storage_config')->loadMultiple($field_ids);
|
||||
$taxonomy_fields = array_filter($field_storages, function ($field_storage) {
|
||||
return $field_storage->getType() == 'entity_reference' && $field_storage->getSetting('target_type') == 'taxonomy_term';
|
||||
});
|
||||
|
||||
foreach ($taxonomy_fields as $field_storage) {
|
||||
$update_storage = FALSE;
|
||||
|
||||
$allowed_values = $field_storage->getSetting('allowed_values');
|
||||
foreach ($allowed_values as &$value) {
|
||||
if ($value['vocabulary'] == $this->getOriginalId()) {
|
||||
$value['vocabulary'] = $this->id();
|
||||
$update_storage = TRUE;
|
||||
}
|
||||
}
|
||||
$field_storage->setSetting('allowed_values', $allowed_values);
|
||||
|
||||
if ($update_storage) {
|
||||
$field_storage->save();
|
||||
}
|
||||
}
|
||||
}
|
||||
$storage->resetCache($update ? array($this->getOriginalId()) : array());
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function preDelete(EntityStorageInterface $storage, array $entities) {
|
||||
parent::preDelete($storage, $entities);
|
||||
|
||||
// Only load terms without a parent, child terms will get deleted too.
|
||||
entity_delete_multiple('taxonomy_term', $storage->getToplevelTids(array_keys($entities)));
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function postDelete(EntityStorageInterface $storage, array $entities) {
|
||||
parent::postDelete($storage, $entities);
|
||||
|
||||
// Reset caches.
|
||||
$storage->resetCache(array_keys($entities));
|
||||
|
||||
if (reset($entities)->isSyncing()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$vocabularies = array();
|
||||
foreach ($entities as $vocabulary) {
|
||||
$vocabularies[$vocabulary->id()] = $vocabulary->id();
|
||||
}
|
||||
// Load all Taxonomy module fields and delete those which use only this
|
||||
// vocabulary.
|
||||
$field_storages = entity_load_multiple_by_properties('field_storage_config', array('module' => 'taxonomy'));
|
||||
foreach ($field_storages as $field_storage) {
|
||||
$modified_storage = FALSE;
|
||||
// Term reference fields may reference terms from more than one
|
||||
// vocabulary.
|
||||
foreach ($field_storage->getSetting('allowed_values') as $key => $allowed_value) {
|
||||
if (isset($vocabularies[$allowed_value['vocabulary']])) {
|
||||
$allowed_values = $field_storage->getSetting('allowed_values');
|
||||
unset($allowed_values[$key]);
|
||||
$field_storage->setSetting('allowed_values', $allowed_values);
|
||||
$modified_storage = TRUE;
|
||||
}
|
||||
}
|
||||
if ($modified_storage) {
|
||||
$allowed_values = $field_storage->getSetting('allowed_values');
|
||||
if (empty($allowed_values)) {
|
||||
$field_storage->delete();
|
||||
}
|
||||
else {
|
||||
// Update the field definition with the new allowed values.
|
||||
$field_storage->save();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Reference in a new issue