You are here

public function TermStorage::getVocabularyHierarchyType in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/taxonomy/src/TermStorage.php \Drupal\taxonomy\TermStorage::getVocabularyHierarchyType()

Returns the hierarchy type for a specific vocabulary ID.

Parameters

string $vid: Vocabulary ID to retrieve the hierarchy type for.

Return value

int The vocabulary hierarchy. Possible values:

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