2

I have a question about taxonomy term search and I hope someone can help. What I need to do is be able to perform parent taxonomy term search and pull all children terms so that the resulting page shows content tagged with children terms to the searched parent term. There might be already a solution out there but I can’t seem to find it, so if someone knows where to get to it, please let me know. I have already created a vocabulary, which contains about 100 terms; some terms have with sub-terms (children terms). When I search for a parent term, I don’t get the content tagged with its children terms related to the parent. When I click the parent term the only thing I get is: “There is currently no content classified with this term”. Thanks for help.

1

Add these helper functions to your module - Call function get_all_nodes_belong_to_taxonomy_hierarchy(), with argument as parent term id, to get all nodes related to parent term and child terms both

/**
 * function to get all term ids inside a vocab in a flat array
 * No matter if vocab consists of hierarichal order of terms,you will get all term ids in a flat array.
 * @param
 *   $vocab_machine_name takes vocabulary machine name as parameter.
 * @return
 *   $tids return array of tids in a flat array format
 */
function get_taxonomy_term_ids_inside_vocab($vocab_machine_name) {
  $vocab = taxonomy_vocabulary_machine_name_load($vocab_machine_name);
  if (is_object($vocab)) {
    $term_tree = taxonomy_get_tree($vocab->vid);
    if (is_array($term_tree) && !empty($term_tree)) {
      foreach ($term_tree as $key => $value) {
        $tids[] = $value->tid;
      }
    } if (empty($term_tree)) {
      $tids = array();
    }
  } else
    return "No Vocab Found with the given name";

  return ($tids);
}

/**
 * function to get all the node ids directly mapped to the given taxonomy terms.
 * Returns Flat array of node ids belong to the given term ids.
 * @param
 *   $tid takes term id can be a single term or array of terms.
 * @return
 *   $nid/$nids returns node ids array or sinly node id directly mapped to the given taxonomy term.
 */
function get_nodes_directly_mapped_to_term($tid) {
  if (is_array($tid) && !empty($tid)) {
    foreach ($tid as $key => $value) {
      $nids[] = taxonomy_select_nodes($value);
      $flat_nids = multitosingle($nids);
    }
    return $flat_nids;
  }
  else {
    $nid = taxonomy_select_nodes($tid);
    return $nid;
  }
}

/**
 * function to convert mutlidimension arrays to flat arrays.
 * Returns Flat arrays.
 * @param
 *   $input takes input array as multudimension array.
 *   $output takes output array as paramter in the recursive call.
 * @return
 *   $output return flat array format for the input multidimenional array
 */
function multitosingle($input, $output = array()) {
  ksort($input);
  foreach ($input as $value) {
    $key = count($output);
    if (is_array($value)) {
      $output = multitosingle($value, $output);
    }
    else {
      $output[$key] = $value;
    }
  }
  return $output;
}

/**
 * function to get array/single nids which are attached with any of the taxonomy term of the given vocab.
 * Returns Flat array of nids.
 * @param
 *   $vocab_machine_name takes the machine name for the vocabulary.
 * @return
 *   $nids_inside_vocab returns array of nids/single nid attached to any taxonomy term under the given vocab.
 */
function get_nodes_belong_to_vocab($vocab_machine_name) {
  $vocab_tids = get_taxonomy_term_ids_inside_vocab($vocab_machine_name);
  if (is_array($vocab_tids) && count($vocab_tids) > 0) {
    $nids_inside_vocab = get_nodes_directly_mapped_to_term($vocab_tids);
    return $nids_inside_vocab;
  }
  elseif (is_array($vocab_tids) && count($vocab_tids) == 0) {
    return array();
  }
  else {
    return "There is no vocab found with the given machine name";
  }
}

/**
 * function to get array/single nids inside a single taxonomy term/array of taxonomy terms
 * function to get all node ids recursively inside a taxonomy term.
 * if parent term is passed as parameter then the function returns nid belongs to the parent term and nids related to its child terms.
 * Returns Flat array of nids.
 * @param
 *   $tids takes array/single tids for which you need nids recursively.
 * @return
 *   $nids flat array of nids
 */
function get_all_nodes_belong_to_taxonomy_hierarchy($tids) {
  if (is_array($tids)) {
    foreach ($tids as $key => $value) {
      if ($value <= 0) {
        return "Please Enter a valid taxonomy term id set";
      }
    }
  }
  if (is_array($tids) && !empty($tids)) {
    foreach ($tids as $key => $value) {
      $term = taxonomy_term_load($value);
      $hierarchical_terms_object[] = taxonomy_get_tree($term->vid, $term->tid);
    }
    $flat_terms_object = multitosingle($hierarchical_terms_object);
    if (is_array($flat_terms_object) && !empty($flat_terms_object)) {
      foreach ($flat_terms_object as $key => $value) {
        $flat_tids[] = $value->tid;
      }
      $flat_tids[] = $tids;
      return array_unique(get_nodes_directly_mapped_to_term($flat_tids));
    }
    else {
      $flat_tids = $tids;
      return array_unique(get_nodes_directly_mapped_to_term($flat_tids));
    }
  }
  elseif (!is_array($tids) && $tids > 0) {
    $flat_tids = $tids;
    get_all_nodes_belong_to_taxonomy_hierarchy(array($flat_tids));
  }
  else {
    return "Please Enter a valid term id";
  }
}
0

The reason being when a tag is attached to a content the parent tag does not get attached by default. The parent lineage is not saved by default. I would suggest you use something like hierarchical select.

Hierarchical Select has the ability to save the entire lineage of a selection or only the "deepest" selection. You can configure it to force the user to make a selection as deep as possible in the tree, or allow the user to select an item anywhere in the tree.

  • Thanks for your quick reply junedkazi, I have already installed Hierarchical Select but I could not make work. Is there any tutorial for the module that I can refer to? Or can you give a brief configuration example for me to follow? Thanks again. – EBamba Oct 1 '12 at 17:02
  • I am not aware of any tutorial as such but u can check out the UI demo at wimleers.com/demo/hierarchical-select/config-ui and you can see the "Save Lineage" radio button. So you will have to select this one. – junedkazi Oct 1 '12 at 17:39
  • Thanks again for your reply and Drupal 7.15 with Views 7.x-3.5. When I go to ./hierarchical-select/config-ui I only get the following top options: Site-wide settings --> Animation delay/Level labels style Configurations-->Hierarchy type Hierarchy Entity type Bundle Context type Context Actions Implementations-->Implementation (module) Hierarchy type Entity type Create new items Entity count and Menu-->Resizable/Disabled/Enabled I could not find the location you renferenced in your reply. – EBamba Oct 1 '12 at 19:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.