1 taxonomy.module taxonomy_check_vocabulary_hierarchy(TaxonomyVocabulary $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 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

TaxonomyVocabulary $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 676
Enables the organization of content into categories.

Code

function taxonomy_check_vocabulary_hierarchy(TaxonomyVocabulary $vocabulary, $changed_term) {
  $tree = taxonomy_get_tree($vocabulary->machine_name);
  $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;
}