class VocabularyStorageController

Defines a controller class for taxonomy vocabularies.

Hierarchy

Expanded class hierarchy of VocabularyStorageController

File

drupal/core/modules/taxonomy/lib/Drupal/taxonomy/VocabularyStorageController.php, line 16
Definition of Drupal\taxonomy\VocabularyStorageController.

Namespace

Drupal\taxonomy
View source
class VocabularyStorageController extends ConfigStorageController {

  /**
   * Overrides Drupal\Core\Config\Entity\ConfigStorageController::postSave().
   */
  protected function postSave(EntityInterface $entity, $update) {
    if (!$update) {
      entity_invoke_bundle_hook('create', 'taxonomy_term', $entity
        ->id());
    }
    elseif ($entity
      ->getOriginalID() != $entity
      ->id()) {

      // Reflect machine name changes in the definitions of existing 'taxonomy'
      // fields.
      $fields = field_read_fields();
      foreach ($fields as $field_name => $field) {
        $update_field = FALSE;
        if ($field['type'] == 'taxonomy_term_reference') {
          foreach ($field['settings']['allowed_values'] as $key => &$value) {
            if ($value['vocabulary'] == $entity
              ->getOriginalID()) {
              $value['vocabulary'] = $entity
                ->id();
              $update_field = TRUE;
            }
          }
          if ($update_field) {
            field_update_field($field);
          }
        }
      }

      // Update bundles.
      entity_invoke_bundle_hook('rename', 'taxonomy_term', $entity
        ->getOriginalID(), $entity
        ->id());
    }
    parent::postSave($entity, $update);
    $this
      ->resetCache($update ? array(
      $entity
        ->getOriginalID(),
    ) : array());
  }

  /**
   * Overrides Drupal\Core\Config\Entity\ConfigStorageController::preDelete().
   */
  protected function preDelete($entities) {
    parent::preDelete($entities);

    // Only load terms without a parent, child terms will get deleted too.
    $tids = db_query('SELECT t.tid FROM {taxonomy_term_data} t INNER JOIN {taxonomy_term_hierarchy} th ON th.tid = t.tid WHERE t.vid IN (:vids) AND th.parent = 0', array(
      ':vids' => array_keys($entities),
    ))
      ->fetchCol();
    entity_delete_multiple('taxonomy_term', $tids);
  }

  /**
   * Overrides Drupal\Core\Config\Entity\ConfigStorageController::postDelete().
   */
  protected function postDelete($entities) {
    parent::postDelete($entities);
    $vocabularies = array();
    foreach ($entities as $vocabulary) {
      $vocabularies[$vocabulary
        ->id()] = $vocabulary
        ->id();
    }

    // Load all Taxonomy module fields and delete those which use only this
    // vocabulary.
    $taxonomy_fields = field_read_fields(array(
      'module' => 'taxonomy',
    ));
    foreach ($taxonomy_fields as $field_name => $taxonomy_field) {
      $modified_field = FALSE;

      // Term reference fields may reference terms from more than one
      // vocabulary.
      foreach ($taxonomy_field['settings']['allowed_values'] as $key => $allowed_value) {
        if (isset($vocabularies[$allowed_value['vocabulary']])) {
          unset($taxonomy_field['settings']['allowed_values'][$key]);
          $modified_field = TRUE;
        }
      }
      if ($modified_field) {
        if (empty($taxonomy_field['settings']['allowed_values'])) {
          field_delete_field($field_name);
        }
        else {

          // Update the field definition with the new allowed values.
          field_update_field($taxonomy_field);
        }
      }
    }

    // Reset caches.
    $this
      ->resetCache(array_keys($vocabularies));
  }

  /**
   * Overrides Drupal\Core\Config\Entity\ConfigStorageController::resetCache().
   */
  public function resetCache(array $ids = NULL) {
    drupal_static_reset('taxonomy_vocabulary_get_names');
    parent::resetCache($ids);
    cache_invalidate_tags(array(
      'content' => TRUE,
    ));
    entity_info_cache_clear();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigStorageController::$configFactory protected property The config factory service.
ConfigStorageController::$configStorage protected property The config storage service.
ConfigStorageController::$statusKey protected property Name of the entity's status key or FALSE if a status is not supported.
ConfigStorageController::$uuidKey protected property Name of the entity's UUID property. Overrides EntityStorageControllerBase::$uuidKey
ConfigStorageController::attachLoad protected function Attaches data to entities upon loading. 4
ConfigStorageController::buildQuery protected function Builds the query to load the entity.
ConfigStorageController::create public function Implements Drupal\Core\Entity\EntityStorageControllerInterface::create(). Overrides EntityStorageControllerInterface::create 2
ConfigStorageController::createInstance public static function Instantiates a new instance of this entity controller. Overrides EntityControllerInterface::createInstance 2
ConfigStorageController::delete public function Implements Drupal\Core\Entity\EntityStorageControllerInterface::delete(). Overrides EntityStorageControllerInterface::delete
ConfigStorageController::deleteRevision public function Implements Drupal\Core\Entity\EntityStorageControllerInterface::deleteRevision(). Overrides EntityStorageControllerInterface::deleteRevision
ConfigStorageController::getConfigPrefix public function Returns the config prefix used by the configuration entity type.
ConfigStorageController::getFieldDefinitions public function Implements Drupal\Core\Entity\EntityStorageControllerInterface::getFieldDefinitions(). Overrides EntityStorageControllerInterface::getFieldDefinitions
ConfigStorageController::getIDFromConfigName public static function Extracts the configuration entity ID from the full configuration name.
ConfigStorageController::getQueryServicename public function Implements Drupal\Core\Entity\EntityStorageControllerInterface::getQueryServicename(). Overrides EntityStorageControllerInterface::getQueryServicename
ConfigStorageController::importCreate public function Create configuration upon synchronizing configuration changes. 1
ConfigStorageController::importDelete public function Delete configuration upon synchronizing configuration changes. 2
ConfigStorageController::importUpdate public function Updates configuration upon synchronizing configuration changes. 1
ConfigStorageController::invokeHook protected function Invokes a hook on behalf of the entity.
ConfigStorageController::load public function Implements Drupal\Core\Entity\EntityStorageControllerInterface::load(). Overrides EntityStorageControllerInterface::load 2
ConfigStorageController::loadByProperties public function Implements Drupal\Core\Entity\EntityStorageControllerInterface::loadByProperties(). Overrides EntityStorageControllerInterface::loadByProperties 2
ConfigStorageController::loadRevision public function Implements Drupal\Core\Entity\EntityStorageControllerInterface::loadRevision(). Overrides EntityStorageControllerInterface::loadRevision
ConfigStorageController::preSave protected function Acts on an entity before the presave hook is invoked. 6
ConfigStorageController::save public function Implements Drupal\Core\Entity\EntityStorageControllerInterface::save(). Overrides EntityStorageControllerInterface::save
ConfigStorageController::__construct public function Constructs a ConfigStorageController object. Overrides EntityStorageControllerBase::__construct 2
EntityStorageControllerBase::$cache protected property Whether this entity type should use the static cache. 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::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
VocabularyStorageController::postDelete protected function Overrides Drupal\Core\Config\Entity\ConfigStorageController::postDelete(). Overrides ConfigStorageController::postDelete
VocabularyStorageController::postSave protected function Overrides Drupal\Core\Config\Entity\ConfigStorageController::postSave(). Overrides ConfigStorageController::postSave
VocabularyStorageController::preDelete protected function Overrides Drupal\Core\Config\Entity\ConfigStorageController::preDelete(). Overrides ConfigStorageController::preDelete
VocabularyStorageController::resetCache public function Overrides Drupal\Core\Config\Entity\ConfigStorageController::resetCache(). Overrides EntityStorageControllerBase::resetCache