class TermStorageController

Defines a Controller class for taxonomy terms.

Hierarchy

Expanded class hierarchy of TermStorageController

File

drupal/core/modules/taxonomy/lib/Drupal/taxonomy/TermStorageController.php, line 17
Definition of Drupal\taxonomy\TermStorageController.

Namespace

Drupal\taxonomy
View source
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);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DatabaseStorageController::$cache protected property Whether this entity type should use the static cache.
DatabaseStorageController::$entityCache protected property Static cache of entities.
DatabaseStorageController::$entityFieldInfo protected property An array of field information, i.e. containing definitions.
DatabaseStorageController::$entityInfo protected property Array of information about the entity.
DatabaseStorageController::$entityType protected property Entity type for this controller instance.
DatabaseStorageController::$hookLoadArguments protected property Additional arguments to pass to hook_TYPE_load().
DatabaseStorageController::$idKey protected property Name of the entity's ID field in the entity database table.
DatabaseStorageController::$revisionKey protected property Name of entity's revision database table field, if it supports revisions.
DatabaseStorageController::$revisionTable protected property The table that stores revisions, if the entity supports revisions.
DatabaseStorageController::$uuidKey protected property Name of entity's UUID database table field, if it supports UUIDs.
DatabaseStorageController::attachLoad protected function Attaches data to entities upon loading. 4
DatabaseStorageController::baseFieldDefinitions public function Defines the base properties of the entity type. 1
DatabaseStorageController::cacheGet protected function Gets entities from the static cache.
DatabaseStorageController::cacheSet protected function Stores entities in the static entity cache.
DatabaseStorageController::delete public function Implements Drupal\Core\Entity\EntityStorageControllerInterface::delete(). Overrides EntityStorageControllerInterface::delete
DatabaseStorageController::deleteRevision public function Implements Drupal\Core\Entity\EntityStorageControllerInterface::deleteRevision(). Overrides EntityStorageControllerInterface::deleteRevision
DatabaseStorageController::getFieldDefinitions public function Implements Drupal\Core\Entity\EntityStorageControllerInterface::getFieldDefinitions(). Overrides EntityStorageControllerInterface::getFieldDefinitions
DatabaseStorageController::getQueryServiceName public function Implements Drupal\Core\Entity\EntityStorageControllerInterface::getQueryServiceName().
DatabaseStorageController::invokeHook protected function Invokes a hook on behalf of the entity. 2
DatabaseStorageController::load public function Implements Drupal\Core\Entity\EntityStorageControllerInterface::load(). Overrides EntityStorageControllerInterface::load
DatabaseStorageController::loadByProperties public function Implements Drupal\Core\Entity\EntityStorageControllerInterface::loadByProperties(). Overrides EntityStorageControllerInterface::loadByProperties
DatabaseStorageController::loadRevision public function Implements Drupal\Core\Entity\EntityStorageControllerInterface::loadRevision(). Overrides EntityStorageControllerInterface::loadRevision
DatabaseStorageController::preDelete protected function Acts on entities before they are deleted. 3
DatabaseStorageController::preSave protected function Acts on an entity before the presave hook is invoked. 4
DatabaseStorageController::preSaveRevision protected function Act on a revision before being saved. 2
DatabaseStorageController::save public function Implements Drupal\Core\Entity\EntityStorageControllerInterface::save(). Overrides EntityStorageControllerInterface::save 2
DatabaseStorageController::saveRevision protected function Saves an entity revision. 1
DatabaseStorageController::__construct public function Constructs a DatabaseStorageController object. 1
EntityStorageControllerInterface::getQueryServicename public function Gets the name of the service for the query for this entity storage. 1
TermStorageController::buildPropertyQuery protected function Overrides Drupal\Core\Entity\DatabaseStorageController::buildPropertyQuery(). Overrides DatabaseStorageController::buildPropertyQuery
TermStorageController::buildQuery protected function Overrides Drupal\Core\Entity\DatabaseStorageController::buildQuery(). Overrides DatabaseStorageController::buildQuery
TermStorageController::create public function Overrides Drupal\Core\Entity\DatabaseStorageController::create(). Overrides DatabaseStorageController::create
TermStorageController::postDelete protected function Overrides Drupal\Core\Entity\DatabaseStorageController::postDelete(). Overrides DatabaseStorageController::postDelete
TermStorageController::postSave protected function Overrides Drupal\Core\Entity\DatabaseStorageController::postSave(). Overrides DatabaseStorageController::postSave
TermStorageController::resetCache public function Overrides Drupal\Core\Entity\DatabaseStorageController::resetCache(). Overrides DatabaseStorageController::resetCache