You are here

function taxonomy_check_vocabulary_hierarchy in Drupal 7

Same name and namespace in other branches
  1. 8 core/modules/taxonomy/taxonomy.module \taxonomy_check_vocabulary_hierarchy()
  2. 6 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
Enables the organization of content into categories.

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;
}