7

I have two different content types, Computer and Chemistry, and taxonomy terms for all articles (content types) which store the methodology of the article. The taxonomy hierarchy is the following.

  • Articles
    • Computer
      • Arithmetic (Term ID: 1)
      • DMA (Term ID: 2) // abbreviation for "Direct Memory Access"
    • Chemistry
      • NGE (Term ID: 3) // abbreviation for "Noble Gas Elements"
      • DMA (Term ID: 4) // abbreviation for "Dynamic Mechanical Analysis"

When users are creating or editing a node of type Chemistry, just NGE and DMA (whose ID is 4) should be allowed to select.

I only need one taxonomy vocabulary, since there are too many content types, and if I want to add a vocabulary for each of them, then it leads too many vocabularies; more importantly it removes the ability of freely navigating in different types using taxonomy hierarchy.

How do I limit the allowed taxonomy terms basing on the content type?

4
  • 1
    "Producer A" with two different ID is a really bad idea. I think this question is not useful because you have a design flaw that shouldn't be repeated, and hopefully this situation will not be recreated. Now, mobile vs laptop is good distinction. Producers is good, but orthogonal to it. And most laptop producers dabble in mobile. Some mobile ones dabble in laptop. So limits can hinder your site in future, instead of help.
    – Mołot
    Sep 11, 2015 at 22:31
  • @Mołot Actually that was just an example for better understanding , the problem is I have two different machine with same name "crawler tractor" , one machine is used in mining , the other is used in construction. however they both have the same name their specifications , properties and attributes are different so they both are saved using taxonomy "Crawler Tractor" but are two different families . now I think that should make sense why I need a term with two different ID. Sep 12, 2015 at 7:06
  • Nope. Presenting users the same name and different sets of results is never good, no matter what excuse you have. It's confusing and bad UX.
    – Mołot
    Sep 13, 2015 at 11:04
  • @Molot so you say same name and different sets of result is never good , and I must accept your opinion and talk to all people working in machinery field and tell them that your naming has made mistakes in my website rename all your devices because of my problem. Nice idea. I'll do that;) Sep 13, 2015 at 20:04

5 Answers 5

3
+100

According to my choice you should use hook_form_alter to bind taxonomy list based on article type.

function MY_MODULE_NAME_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'my_form_id') {
    $default_value = 0;
    $options = getArticles('Computer','Articles');
    $form['your_taxonomy_field_name'] = array(
      '#title' => t('Articlas'),
      '#type' => 'select',
      '#options' => $options,
      '#access' => 1,
      '#default_value' => $default_value,
      '#weight' => 1,
    );
  }
}
function getArticles($articleType,$vocabularyName) {
  //get the vocabulary
  $vocabulary = taxonomy_get_vocabularies();
  $vid = -1;
  foreach ($vocabulary as $item) {
    if ($item->name == $vocabularyName) {
      $vid = $item->vid;
    }
  }
  if ($vid != -1) {
    //get the parrent Term
    $query = new EntityFieldQuery;
    $result = $query
      ->entityCondition('entity_type', 'taxonomy_term')
      ->propertyCondition('name', $articleType)
      ->propertyCondition('vid', $vid)
      ->execute();
    $parrent_tid = array_shift($result['taxonomy_term'])->tid;
    //return children
    return taxonomy_get_children($parrent_tid);
  }
  return NULL;
}
2

According to this answer on the main Stackoverflow site, a submodule of Content Taxonomy called "Content Taxonomy Options"would be perfect for this:

You can do this with Content Taxonomy's Content Taxonomy Options module. Add a new Content Taxonomy field, and use the Select widget. In the field settings, under the Advanced settings for hierarchical vocabularies field group, select the Parent Term you want to use for that field.

0
2

The Term Reference Tree module might help. This module provides an expandable tree widget for the Taxonomy Term Reference field in Drupal 7.

Term Reference Tree:

This module provides an expandable tree widget for the Taxonomy Term Reference field in Drupal 7. This widget is intended to serve as a replacement for Drupal's core Taxonomy Term Reference widget, which is a flat list of radio buttons or checkboxes and not necessarily fit for medium to large taxonomy trees.

How to use this module

1) Enable the Term Reference Tree module.
2) Go to the Manage Fields tab of any fieldable entity (such as a content type, taxonomy term, or user.
3) Add a new field with a type of Term Reference.
4) In the widget select box, select Term reference tree.

enter image description here

5) Last step is important onc, as shown in the below image you have to give the taxonomy term id of which vocabulary you want to show for specific content type.

2
  • I tries it but couldn't realize how to set this widget up in a matter that it limits terms for a certain content type. any help on that? Sep 14, 2015 at 11:59
  • given a steps to use this module..let me know if you are facing any issue.
    – mks
    Sep 14, 2015 at 12:22
1

Mobile and Laptop should be separate Vocabularies.

Then for mobile content type, when you add a tax term field, select mobile as your vocabulary.

1
  • Not just what I'm looking for (I prefer to have just one vocab) , but thanks for a working solution , voted up. Sep 14, 2015 at 11:57
1

I would suggest avoding taxonomy altogether and define your own content types. I go into this in depth in another post. When you scroll down my post, you will find that you can do exactly what you are looking to do.

I am not a big fan of the "there's a module for that" philosophy because every time you add another module, you increase the footprint of your Drupal site; you load that module for ever page in ever session whether you need it or not.

By using Views, Entity, Entity-Reference, Entity-Connect (stuff you should be using) you can turn off taxonomy completely and get a better indexed alternative to taxonomy.

Say no to taxonomy

3
  • I read the article , nice job but how about the performance? Doesn't this solution make overhead? Sep 14, 2015 at 19:54
  • Not at all. I need those entities and entity references anyway; I may as well make use of them. In fact, I am using that same structure to create dynamic menus and will consequently get rid of the "menu" module thus saving more memory. :-) What you defined in the original question is a relationship of terms. What better utility to handle relationships than an entity and an entity reference. Here's a question...what if I wanted to attach a picture or logo to each term? Taxonomy can't do that.
    – Allan
    Sep 14, 2015 at 20:04
  • Two issues I faced using your solution : 1)How to achieve the ability of free tagging by user. 2)How to deal with hierarchical vocabularies . How these problems are going to be solved? have you faced these issues before Sep 16, 2015 at 6:06

Your Answer

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

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