taxonomy_check_vocabulary_hierarchy

taxonomy_check_vocabulary_hierarchy(VocabularyInterface $vocabulary, $changed_term)

Checks and updates the hierarchy flag of a vocabulary.

Checks the current parents of all terms in a vocabulary and updates the vocabulary's hierarchy setting to the lowest possible level. If no term has parent terms then the vocabulary will be given a hierarchy of VocabularyInterface::HIERARCHY_DISABLED. If any term has a single parent then the vocabulary will be given a hierarchy of VocabularyInterface::HIERARCHY_SINGLE. If any term has multiple parents then the vocabulary will be given a hierarchy of VocabularyInterface::HIERARCHY_MULTIPLE.

Parameters

\Drupal\taxonomy\VocabularyInterface $vocabulary: A taxonomy vocabulary entity.

$changed_term: An array of the term structure that was updated.

Return value

An integer that represents the level of the vocabulary's hierarchy.

File

core/modules/taxonomy/taxonomy.module, line 160
Enables the organization of content into categories.

Code

function taxonomy_check_vocabulary_hierarchy(VocabularyInterface $vocabulary, $changed_term) {
  $tree = \Drupal::entityManager()->getStorage('taxonomy_term')->loadTree($vocabulary->id());
  $hierarchy = VocabularyInterface::HIERARCHY_DISABLED;
  foreach ($tree as $term) {
    // Update the changed term with the new parent value before comparison.
    if ($term->tid == $changed_term['tid']) {
      $term = (object) $changed_term;
      $term->parents = $term->parent;
    }
    // Check this term's parent count.
    if (count($term->parents) > 1) {
      $hierarchy = VocabularyInterface::HIERARCHY_MULTIPLE;
      break;
    }
    elseif (count($term->parents) == 1 && !isset($term->parents[0])) {
      $hierarchy = VocabularyInterface::HIERARCHY_SINGLE;
    }
  }
  if ($hierarchy != $vocabulary->getHierarchy()) {
    $vocabulary->setHierarchy($hierarchy);
    $vocabulary->save();
  }

  return $hierarchy;
}
doc_Drupal
2016-10-29 09:46:30
Comments
Leave a Comment

Please login to continue.