I was fiddling around with this module and realized that by adding a taxonomy term field to a nat node, will automatically make the nat term a child of the selected taxonomy term(s)*.

This makes it impossible to use the module to cross relate terms under the same vocabulary.
Any chance an option to disable the automatic hierarchy features could be implemented?
or change the code to disable it?

* If x taxonomy terms are selected the nat term is duplicated and becomes a child of each of those terms.

Thanks,
Spre3

Comments

michaellander’s picture

What if instead, a new field type was created called like "Node Parent Term" or "Parent Node Auto Term', or something that works essentially as the default taxonomy term works now. Then give the default taxonomy term it's functionality back as just a reference? Maybe I'm not thinking this through enough but it seems like it should work?

biwashingtonial’s picture

Title: Disable taxonomy hierarchy » Taxonomy term auto-hierarchy should be made optional
Version: 7.x-1.0-alpha4 » 7.x-1.x-dev
Priority: Normal » Major
StatusFileSize
new1.43 KB

Bump. There are legitimate use cases for NAT that this undocumented feature breaks completely. In fact, any application that uses terms' parent-child relationships as an independent variable from terms' NAT relationships is potentially broken. Is there a compelling reason why we're doing this?

It might be surprising and confusing for a user to find that tagging a node to a term will cause that node's NAT term to magically pop up as a child of the tagged term over in the Taxonomy console. So +1 to @michaellander's suggestion to make this opt-in on a field-by-field basis.

Meanwhile, for anyone else needing a quick and dirty way to disable the auto-parent behavior site-wide, here's a patch.

capellic’s picture

This patch is great for shutting off the hierarchical effect completely, but like @michaellander and @biwashingtonial say above, I'd love to be able to designate which taxonomy reference field defines the relationship.

capellic’s picture

I had to take further action to get this to work for my situation. In my case, I have a node/term relationship for an "item" content type. On that form, I am able to related the item to other items.

1. Parent
2. Related

In the first case, I want the default behavior, but not in the second case. Not only that, but if selecting both, the code gets thrown into an endless loop. This is the perfect use case for why we need to be able to determine which field is used to determine the hierarchy, if any at all.

I put a temporary hack in the code for now which allows me to explicitly define this field name. Of course, this is best on a configuration form, but exactly where that should go I am not sure. But I think by me identifying where that check should go, I'm thinking that there should be a checkbox on the "manage field" form. A hook_form_alter might be what's required.

The code is below and you can clearly see where I have hacked the code:

/**
 * Retrieve any parent terms for the NAT term from the node object.
 *
 * @param Object $node
 *   The node to parse.
 * @return Array $hierarchy
 *   An array containing the parent terms for the NAT term.
 */
function _nat_get_term_hierarchies($node) {
  $allowed_fields = array('field_g_parent_item');				/* PATCH to only allow certain fields to dictate hierarchy */
  // Collate taxonomy reference fields by their vocabulary IDs.
  $instances = field_info_instances('node', $node->type);
  $hierarchy = array();
  foreach ($instances as $name => $instance) {
    if (isset($node->$name)) {
      $field = field_info_field($name);
      if (in_array($field['field_name'], $allowed_fields)) {     /* PATCH to only allow certain fields to dictate hierarchy */
        if ($field['type'] == 'taxonomy_term_reference' && !empty($node->$name)) {
          $vocabulary = taxonomy_vocabulary_machine_name_load($field['settings']['allowed_values'][0]['vocabulary']);
          // Presumably the terms will have the same language as the node.
          $vocabulary_field = $node->$name;
          $terms = $vocabulary_field[$node->language];
          foreach ($terms as $term) {
            $hierarchy[$vocabulary->vid][] = $term['tid'];
          }
        }
      }   /* PATCH to only allow certain fields to dictate hierarchy */
    }
  }

  return $hierarchy;
}
brooke_heaton’s picture

Issue summary: View changes
Status: Active » Needs review
StatusFileSize
new7.46 KB

The crux of this issue is that _nat_get_term_hierarchies returns a non-configurable array of parent terms based on any term reference on a $node. I have created a patch that makes $hierarchy configurable via the UI - add a parent, add multiple parents, add no parents and remove $hierarchy from your nat configuration all together. This solution is flexible and accommodates most concerns above. Feedback is most welcome!

NOTE: I updated the module below to account for some issues I discovered in testing.

brooke_heaton’s picture

StatusFileSize
new8.8 KB

Updated and refactored patch to allow for multiple vocabs per bundle and to change from checkboxes to select to prevent php integrity issues in taxonomy_term_hierarchy after taxonomy_term_save.

brooke_heaton’s picture

StatusFileSize
new8.8 KB

Updated patch with correct comment number.