taxonomy_check_vocabulary_hierarchy

6 taxonomy.module taxonomy_check_vocabulary_hierarchy($vocabulary, $changed_term)
7 taxonomy.module taxonomy_check_vocabulary_hierarchy($vocabulary, $changed_term)
8 taxonomy.module taxonomy_check_vocabulary_hierarchy(TaxonomyVocabulary $vocabulary, $changed_term)

Dynamically check and update the hierarachy flag of a vocabulary. 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: An array of the vocabulary structure.

$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()

File

modules/taxonomy/taxonomy.module, line 273
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 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;
}

Comments

Maybe this function will be of use to someone.

I'm not sure this function works as expected - in fact there does seem to be a bug in the setting of the hierarchy value which I've reported as http://drupal.org/node/1309990

The line

$term->parents = $term->parent;

doesn't look right as taxonomy_get_tree() does not return such an attribute.

Maybe this function will be of use to someone who wants to set the hiearchy value for a vocabulary. Obviously, this could be extended easily enough to act as a function where a term id is the input parameter - code would need to be added to look up the vocabulary which the term belongs to.

function reset_taxonomy_vocabulary_hierarchy($vid) {

  /** 
   * This will take a vocabulary ID and will reset the hierarchy value to be correct
   * for the current terms in the vocabulary.
   */
  $tree = taxonomy_get_tree($vid);
  $vocabulary_object = taxonomy_vocabulary_load($vid);

  // Set the hierarchy to the lowest level and increase it if relevant terms are found.
  $hierarchy = '0';
  foreach ($tree as $term) {

    // Check this term's parent count.
    if (count($term->parents) > 1) {

      $hierarchy = '2';
     
      // Break out the foreach loop as the highest hierarchy value has been set.
      break;
    }
    // Look for a single parent - but do not set the value if there is a single parent value of '0' which is
    // normal for terms with no parents.
    elseif (count($term->parents) == 1 && '0' !== array_shift($term->parents)) {
      $hierarchy = '1';
    }
  }

  // If the hieracrchy value has changed then save the new value back to the database.
  if ($hierarchy != $vocabulary_object->hierarchy) {

    $vocabulary_object->hierarchy = $hierarchy;

    // Needs to be cast to an array as that is what the taxonomy_save_vocabulary function expects.
    $vocabulary_array = (array)$vocabulary_object;
    taxonomy_save_vocabulary($vocabulary_array);
  }
}

Login or register to post comments