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 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;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DatabaseStorageController::$cache protected property Whether this entity type should use the static cache. Overrides EntityStorageControllerBase::$cache
DatabaseStorageController::$database protected property Active database connection.
DatabaseStorageController::$entityFieldInfo protected property An array of field information, i.e. containing definitions.
DatabaseStorageController::$fieldDefinitions protected property Static cache of field definitions per bundle.
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::createInstance public static function Instantiates a new instance of this entity controller. Overrides EntityControllerInterface::createInstance 2
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::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. 5
DatabaseStorageController::preSave protected function Acts on an entity before the presave hook is invoked. 6
DatabaseStorageController::preSaveRevision protected function Act on a revision before being saved. 3
DatabaseStorageControllerNG::$bundleKey protected property The entity bundle key.
DatabaseStorageControllerNG::$dataTable protected property The table that stores properties, if the entity has multilingual support.
DatabaseStorageControllerNG::$entityClass protected property The entity class to use.
DatabaseStorageControllerNG::attachLoad protected function Overrides DatabaseStorageController::attachLoad(). Overrides DatabaseStorageController::attachLoad 6
DatabaseStorageControllerNG::attachPropertyData protected function Attaches property data in all languages for translatable properties.
DatabaseStorageControllerNG::buildQuery protected function Builds the query to load the entity. Overrides DatabaseStorageController::buildQuery 1
DatabaseStorageControllerNG::delete public function Overwrites \Drupal\Core\Entity\DatabaseStorageController::delete(). Overrides DatabaseStorageController::delete
DatabaseStorageControllerNG::invokeHook protected function Overrides DatabaseStorageController::invokeHook(). Overrides DatabaseStorageController::invokeHook 2
DatabaseStorageControllerNG::mapFromStorageRecords protected function Maps from storage records to entity objects.
DatabaseStorageControllerNG::mapToDataStorageRecord protected function Maps from an entity object to the storage record of the data table. 1
DatabaseStorageControllerNG::mapToRevisionStorageRecord protected function Maps from an entity object to the storage record of the revision table.
DatabaseStorageControllerNG::mapToStorageRecord protected function Maps from an entity object to the storage record of the base table.
DatabaseStorageControllerNG::save public function Overrides DatabaseStorageController::save(). Overrides DatabaseStorageController::save 1
DatabaseStorageControllerNG::savePropertyData protected function Stores the entity property language-aware data.
DatabaseStorageControllerNG::saveRevision protected function Saves an entity revision. Overrides DatabaseStorageController::saveRevision
DatabaseStorageControllerNG::__construct public function Overrides DatabaseStorageController::__construct(). Overrides DatabaseStorageController::__construct 1
EntityStorageControllerBase::$entityCache protected property Static cache of entities.
EntityStorageControllerBase::$entityInfo protected property Array of information about the entity.
EntityStorageControllerBase::$entityType protected property Entity type for this controller instance.
EntityStorageControllerBase::$hookLoadArguments protected property Additional arguments to pass to hook_TYPE_load().
EntityStorageControllerBase::$idKey protected property Name of the entity's ID field in the entity database table.
EntityStorageControllerBase::$uuidKey protected property Name of entity's UUID database table field, if it supports UUIDs. 1
EntityStorageControllerBase::cacheGet protected function Gets entities from the static cache.
EntityStorageControllerBase::cacheSet protected function Stores entities in the static entity cache.
EntityStorageControllerBase::loadUnchanged public function Loads an unchanged entity from the database. Overrides EntityStorageControllerInterface::loadUnchanged
EntityStorageControllerInterface::getQueryServicename public function Gets the name of the service for the query for this entity storage. 1
TermStorageController::baseFieldDefinitions public function Overrides \Drupal\Core\Entity\DatabaseStorageControllerNG::baseFieldDefintions(). Overrides DatabaseStorageController::baseFieldDefinitions
TermStorageController::buildPropertyQuery protected function Overrides Drupal\Core\Entity\DatabaseStorageController::buildPropertyQuery(). Overrides DatabaseStorageControllerNG::buildPropertyQuery
TermStorageController::create public function Overrides Drupal\Core\Entity\DatabaseStorageController::create(). Overrides DatabaseStorageControllerNG::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 EntityStorageControllerBase::resetCache