You are here

function taxonomy_check_vocabulary_hierarchy in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/taxonomy/taxonomy.module \taxonomy_check_vocabulary_hierarchy()

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 TAXONOMY_HIERARCHY_DISABLED. If any term has a single parent then the vocabulary will be given a hierarchy of TAXONOMY_HIERARCHY_SINGLE. If any term has multiple parents then the vocabulary will be given a hierarchy of TAXONOMY_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.

2 calls to taxonomy_check_vocabulary_hierarchy()
TermDeleteForm::submitForm in core/modules/taxonomy/src/Form/TermDeleteForm.php
This is the default entity object builder function. It is called before any other submit handler to build the new entity object to be used by the following submit handlers. At this point of the form workflow the entity is validated and the form state…
TermForm::save in core/modules/taxonomy/src/TermForm.php
Form submission handler for the 'save' action.

File

core/modules/taxonomy/taxonomy.module, line 150
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 = TAXONOMY_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 = TAXONOMY_HIERARCHY_MULTIPLE;
      break;
    }
    elseif (count($term->parents) == 1 && !isset($term->parents[0])) {
      $hierarchy = TAXONOMY_HIERARCHY_SINGLE;
    }
  }
  if ($hierarchy != $vocabulary
    ->getHierarchy()) {
    $vocabulary
      ->setHierarchy($hierarchy);
    $vocabulary
      ->save();
  }
  return $hierarchy;
}