function 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\Plugin\Core\Entity\Vocabulary $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()
taxonomy_term_confirm_delete_submit in drupal/core/modules/taxonomy/taxonomy.admin.inc
Submit handler to delete a term after confirmation.
TermFormController::save in drupal/core/modules/taxonomy/lib/Drupal/taxonomy/TermFormController.php
Overrides Drupal\Core\Entity\EntityFormController::save().

File

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

Code

function taxonomy_check_vocabulary_hierarchy(Vocabulary $vocabulary, $changed_term) {
  $tree = taxonomy_get_tree($vocabulary->vid);
  $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->hierarchy) {
    $vocabulary->hierarchy = $hierarchy;
    taxonomy_vocabulary_save($vocabulary);
  }
  return $hierarchy;
}