taxonomy_check_vocabulary_hierarchy

Versions
6 – 7
taxonomy_check_vocabulary_hierarchy($vocabulary, $changed_term)

Dynamically check and update the hierarachy flag of a vocabulary.

Checks the current parents of all terms in a vocabulary and updates the vocabularies hierarchy setting to the lowest possible level. A hierarchy with no parents in any of its terms will be given a hierarchy of 0. If terms contain at most a single parent, the vocabulary will be given a hierarchy of 1. If any term contain multiple parents, the vocabulary will be given a hieararchy of 2.

Parameters

$vocabulary An array of the vocabulary structure.

$changed_term An array of the term structure that was updated.

▾ 2 functions call 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.

Code

modules/taxonomy/taxonomy.module, line 269

<?php
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 comparision.
    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 && 0 !== array_shift($term->parents)) {
      $hierarchy = 1;
    }
  }
  if ($hierarchy != $vocabulary['hierarchy']) {
    $vocabulary['hierarchy'] = $hierarchy;
    taxonomy_save_vocabulary($vocabulary);
  }

  return $hierarchy;
}
?>
Login or register to post comments
 
 

All source code and documentation on this site is released under the terms of the GNU General Public License, version 2 and later. Drupal is a registered trademark of Dries Buytaert.