Defines a Controller class for taxonomy terms.
Expanded class hierarchy of TermStorageController
class TermStorageController extends DatabaseStorageController {
/**
* 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) {
$entity = parent::create($values);
// Ensure the vocabulary machine name is initialized as it is used as the
// bundle key. Only attempt to do this if a vocabulary ID is available,
// which might not be the case when creating partial entity structures.
// @todo Move to Term::bundle() once field API has been converted
// to make use of it.
if (!isset($entity->vocabulary_machine_name) && isset($entity->vid)) {
$vocabulary = taxonomy_vocabulary_load($entity->vid);
$entity->vocabulary_machine_name = $vocabulary->machine_name;
}
// Save new terms with no parents by default.
if (!isset($entity->parent)) {
$entity->parent = array(
0,
);
}
return $entity;
}
/**
* Overrides Drupal\Core\Entity\DatabaseStorageController::buildQuery().
*/
protected function buildQuery($ids, $revision_id = FALSE) {
$query = parent::buildQuery($ids, $revision_id);
$query
->addTag('translatable');
$query
->addTag('term_access');
// Add the machine name field from the {taxonomy_vocabulary} table.
$query
->innerJoin('taxonomy_vocabulary', 'v', 'base.vid = v.vid');
$query
->addField('v', 'machine_name', 'vocabulary_machine_name');
return $query;
}
/**
* 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->tid);
// Because the parent has already been deleted, the parent count might
// be 0.
if (count($parents) <= 1) {
$orphans[] = $child->tid;
}
}
}
}
// 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)) {
taxonomy_term_delete_multiple($orphans);
}
}
/**
* Overrides Drupal\Core\Entity\DatabaseStorageController::postSave().
*/
protected function postSave(EntityInterface $entity, $update) {
if (isset($entity->parent)) {
db_delete('taxonomy_term_hierarchy')
->condition('tid', $entity->tid)
->execute();
$query = db_insert('taxonomy_term_hierarchy')
->fields(array(
'tid',
'parent',
));
foreach ($entity->parent as $parent) {
$query
->values(array(
'tid' => $entity->tid,
'parent' => $parent,
));
}
$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);
}
}