Skip to content
Snippets Groups Projects
Select Git revision
  • 5838ea9ab2ee30f1183e2a6e628e2f4089e3733d
  • 11.x default protected
  • 11.2.x protected
  • 10.6.x protected
  • 10.5.x protected
  • 11.1.x protected
  • 10.4.x protected
  • 11.0.x protected
  • 10.3.x protected
  • 7.x protected
  • 10.2.x protected
  • 10.1.x protected
  • 9.5.x protected
  • 10.0.x protected
  • 9.4.x protected
  • 9.3.x protected
  • 9.2.x protected
  • 9.1.x protected
  • 8.9.x protected
  • 9.0.x protected
  • 8.8.x protected
  • 10.5.1 protected
  • 11.2.2 protected
  • 11.2.1 protected
  • 11.2.0 protected
  • 10.5.0 protected
  • 11.2.0-rc2 protected
  • 10.5.0-rc1 protected
  • 11.2.0-rc1 protected
  • 10.4.8 protected
  • 11.1.8 protected
  • 10.5.0-beta1 protected
  • 11.2.0-beta1 protected
  • 11.2.0-alpha1 protected
  • 10.4.7 protected
  • 11.1.7 protected
  • 10.4.6 protected
  • 11.1.6 protected
  • 10.3.14 protected
  • 10.4.5 protected
  • 11.0.13 protected
41 results

TermStorageController.php

Blame
  • Alex Pott's avatar
    Issue #1980982 by andypost, aspilicious: Clean-up procedural wrappers for taxonomy.
    Alex Pott authored
    5838ea9a
    History
    Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    TermStorageController.php 5.20 KiB
    <?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;
      }
    }