function TermStorage::getVocabularyHierarchyType

Same name and namespace in other branches
  1. 8.9.x core/modules/taxonomy/src/TermStorage.php \Drupal\taxonomy\TermStorage::getVocabularyHierarchyType()
  2. 10 core/modules/taxonomy/src/TermStorage.php \Drupal\taxonomy\TermStorage::getVocabularyHierarchyType()
  3. 11.x core/modules/taxonomy/src/TermStorage.php \Drupal\taxonomy\TermStorage::getVocabularyHierarchyType()

Overrides TermStorageInterface::getVocabularyHierarchyType

File

core/modules/taxonomy/src/TermStorage.php, line 410

Class

TermStorage
Defines a Controller class for taxonomy terms.

Namespace

Drupal\taxonomy

Code

public function getVocabularyHierarchyType($vid) {
    // Return early if we already computed this value.
    if (isset($this->vocabularyHierarchyType[$vid])) {
        return $this->vocabularyHierarchyType[$vid];
    }
    $parent_field_storage = $this->entityFieldManager
        ->getFieldStorageDefinitions($this->entityTypeId)['parent'];
    $table_mapping = $this->getTableMapping();
    $target_id_column = $table_mapping->getFieldColumnName($parent_field_storage, 'target_id');
    $delta_column = $table_mapping->getFieldColumnName($parent_field_storage, TableMappingInterface::DELTA);
    $query = $this->database
        ->select($table_mapping->getFieldTableName('parent'), 'p');
    $query->addExpression("MAX([{$target_id_column}])", 'max_parent_id');
    $query->addExpression("MAX([{$delta_column}])", 'max_delta');
    $query->condition('bundle', $vid);
    $result = $query->execute()
        ->fetchAll();
    // If all the terms have the same parent, the parent can only be root (0).
    if ((int) $result[0]->max_parent_id === 0) {
        $this->vocabularyHierarchyType[$vid] = VocabularyInterface::HIERARCHY_DISABLED;
    }
    elseif ((int) $result[0]->max_delta === 0) {
        $this->vocabularyHierarchyType[$vid] = VocabularyInterface::HIERARCHY_SINGLE;
    }
    else {
        $this->vocabularyHierarchyType[$vid] = VocabularyInterface::HIERARCHY_MULTIPLE;
    }
    return $this->vocabularyHierarchyType[$vid];
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.