Definition of Drupal\taxonomy\TermStorageController.
<?php
/**
* @file
* Definition of Drupal\taxonomy\TermStorageController.
*/
namespace Drupal\taxonomy;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\Query\QueryInterface;
use Drupal\Core\Entity\DatabaseStorageControllerNG;
/**
* Defines a Controller class for taxonomy terms.
*/
class TermStorageController extends DatabaseStorageControllerNG {
/**
* Overrides Drupal\Core\Entity\DatabaseStorageController::create().
*
* @param array $values
* An array of values to set, keyed by property name. A value for the
* vocabulary ID ('vid') is required.
*/
public function create(array $values) {
// Save new terms with no parents by default.
if (empty($values['parent'])) {
$values['parent'] = array(
0,
);
}
$entity = parent::create($values);
return $entity;
}
/**
* Overrides Drupal\Core\Entity\DatabaseStorageController::buildPropertyQuery().
*/
protected function buildPropertyQuery(QueryInterface $entity_query, array $values) {
if (isset($values['name'])) {
$entity_query
->condition('name', $values['name'], 'LIKE');
unset($values['name']);
}
parent::buildPropertyQuery($entity_query, $values);
}
/**
* Overrides Drupal\Core\Entity\DatabaseStorageController::postDelete().
*/
protected function postDelete($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 = 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());
// Because the parent has already been deleted, the parent count might
// be 0.
if (count($parents) <= 1) {
$orphans[] = $child
->id();
}
}
}
}
// Delete term hierarchy information after looking up orphans but before
// deleting them so that their children/parent information is consistent.
db_delete('taxonomy_term_hierarchy')
->condition('tid', array_keys($entities))
->execute();
if (!empty($orphans)) {
entity_delete_multiple('taxonomy_term', $orphans);
}
}
/**
* Overrides Drupal\Core\Entity\DatabaseStorageController::postSave().
*/
protected function postSave(EntityInterface $entity, $update) {
// Only change the parents if a value is set, keep the existing values if
// not.
if (isset($entity->parent->value)) {
db_delete('taxonomy_term_hierarchy')
->condition('tid', $entity
->id())
->execute();
$query = db_insert('taxonomy_term_hierarchy')
->fields(array(
'tid',
'parent',
));
foreach ($entity->parent as $parent) {
$query
->values(array(
'tid' => $entity
->id(),
'parent' => (int) $parent->value,
));
}
$query
->execute();
}
}
/**
* Overrides Drupal\Core\Entity\DatabaseStorageController::resetCache().
*/
public function resetCache(array $ids = NULL) {
drupal_static_reset('taxonomy_term_count_nodes');
drupal_static_reset('taxonomy_get_tree');
drupal_static_reset('taxonomy_get_tree:parents');
drupal_static_reset('taxonomy_get_tree:terms');
drupal_static_reset('taxonomy_term_load_parents');
drupal_static_reset('taxonomy_term_load_parents_all');
drupal_static_reset('taxonomy_term_load_children');
parent::resetCache($ids);
}
/**
* Overrides \Drupal\Core\Entity\DatabaseStorageControllerNG::baseFieldDefintions().
*/
public function baseFieldDefinitions() {
$properties['tid'] = array(
'label' => t('Term ID'),
'description' => t('The term ID.'),
'type' => 'integer_field',
'read-only' => TRUE,
);
$properties['uuid'] = array(
'label' => t('UUID'),
'description' => t('The term UUID.'),
'type' => 'string_field',
'read-only' => TRUE,
);
$properties['vid'] = array(
'label' => t('Vocabulary ID'),
'description' => t('The ID of the vocabulary to which the term is assigned.'),
'type' => 'string_field',
);
$properties['langcode'] = array(
'label' => t('Language code'),
'description' => t('The term language code.'),
'type' => 'language_field',
);
$properties['name'] = array(
'label' => t('Name'),
'description' => t('The term name.'),
'type' => 'string_field',
);
$properties['description'] = array(
'label' => t('Description'),
'description' => t('A description of the term'),
'type' => 'string_field',
);
// @todo Combine with description.
$properties['format'] = array(
'label' => t('Description format'),
'description' => t('The filter format ID of the description.'),
'type' => 'string_field',
);
$properties['weight'] = array(
'label' => t('Weight'),
'description' => t('The weight of this term in relation to other terms.'),
'type' => 'integer_field',
);
$properties['parent'] = array(
'label' => t('Term Parents'),
'description' => t('The parents of this term.'),
'type' => 'integer_field',
'computed' => TRUE,
);
return $properties;
}
}
Name | Description |
---|---|
TermStorageController | Defines a Controller class for taxonomy terms. |