3

How can I have the parent taxonomy be automatically selected when one of its children gets selected? It would be a convenience if the parent is selected automatically in the backend for every child selected.

Right now, if I select a child and not the parent, then search or browse for the parent term, child terms under that parent are not brought back. This doesn't seem right.

3
  • Are you using Views ?
    – yvan
    Commented Sep 8, 2011 at 18:51
  • Not in this case. Display is routed to page--taxonomy.tpl.php for this instance.
    – enchance
    Commented Sep 8, 2011 at 19:30
  • This is about "term reference" fields and widgets. I think this will be useful to mention in the question.
    – donquixote
    Commented Oct 16, 2013 at 13:40

5 Answers 5

1

similar question here: Check parent term when checking subterm

but if you need check all child when parent checked, you need some changes in JavaScript file

1

You could use the Hierarchical Term Reference Autocomplete module and can accomplish the first half of the task.

For detail of how to use this module, go through the README.txt of the module.

1

In Drupal 7 I recommend using Term Reference Tree. It creates a hierarchical tree of taxonomy terms and allows you to toggle settings such as:

  • Allow user to select only one term / allow multiple terms.
  • Allow user to select only lowest-level terms / allow selection of parent terms.
  • Record only the term lowest selection(s) / record all parent terms of selection(s).

This module is similar to Taxonomy Tree Select module of Drupal 6.

0

I needed this to only copy the root term over to another node. The rules module only have the parent value but how to drill down? This is what I did with Drupal 7.

/**
 * Implements hook_rules_action_info().
 */
function my_custom_rules_action_info() {
  return array(
    'my_custom_get_root_term' => array(
      'label' => t('Get root term'),
      'parameter' => array(
        'tid' => array(
          'type' => 'taxonomy_term',
          'label' => t('Term'),
        ),
      ),
      'provides' => array(
        'rootterm' => array(
          'type' => 'taxonomy_term',
          'label' => t('Root term'),
        )
      ),
      'group' => t('My custom rules'),
      'base' => 'my_custom_rules_action_get_root_term',
    ),
  );
}

/**
 * Find the root term for the given $term.
 *
 * @param $term
 */
function my_custom_rules_action_get_root_term($term) {
  $results = drupal_static(__FUNCTION__, array());
  if (!is_object($term)) {
    $term = taxonomy_term_load($term);
  }
  if (empty($term)) {
    return;
  }

  // Build hash [$child] = $parent
  if (!isset($result[$term->tid])) {
    $tree = taxonomy_get_tree($term->vid);
    foreach ($tree as $tree_term) {
      // HUH: parents array always has one element on our vocabulary sets
      // No parent == 0
      $result = array_shift($tree_term->parents);
      echo $result . PHP_EOL;
      $results[$tree_term->tid] = $result;
    }
  }
  // follow the parent refs until $parent == 0
  $tid = $term->tid;
  while ($results[$tid] != 0) {
    $tid = $results[$tid];
  }
  return array('rootterm' => taxonomy_term_load($tid));
}
-1

Easiest way to do that is to use rules module.

1
  • Could you please provide an explanation (or a link) on how to do that with Rules ?
    – Benj
    Commented Aug 12, 2014 at 21:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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