function taxonomy_check_vocabulary_hierarchy

Same name in other branches
  1. 8.9.x 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 0. If any term has a single parent then the vocabulary will be given a hierarchy of 1. If any term has multiple parents then the vocabulary will be given a hierarchy of 2.

Parameters

$vocabulary: A vocabulary object.

$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_form_term_submit in modules/taxonomy/taxonomy.admin.inc
Submit handler to insert or update a term.
taxonomy_term_confirm_delete_submit in modules/taxonomy/taxonomy.admin.inc
Submit handler to delete a term after confirmation.

File

modules/taxonomy/taxonomy.module, line 571

Code

function taxonomy_check_vocabulary_hierarchy($vocabulary, $changed_term) {
    $tree = taxonomy_get_tree($vocabulary->vid);
    $hierarchy = 0;
    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 = 2;
            break;
        }
        elseif (count($term->parents) == 1 && !isset($term->parents[0])) {
            $hierarchy = 1;
        }
    }
    if ($hierarchy != $vocabulary->hierarchy) {
        $vocabulary->hierarchy = $hierarchy;
        taxonomy_vocabulary_save($vocabulary);
    }
    return $hierarchy;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.