I have a term reference field in my content type. I'd like to be able to access the vocabulary items from that field in a custom module so I can pre-select one based on a users selection of an item from a field list. How is this possible?

An example might help illustrate the problem:

Content type: resource
field 1: Category (field list)
field 2: Group (term reference, hidden)
  1. User selects one of eight options in the 'field 1' drop down select box. Selections include things like 'paper', 'metal', 'stone', etc.

  2. The hidden 'Group' field detects which selection the user made above and auto-fills itself by selecting the vocabulary item in the associated taxonomy that maps to the selection they made. There are only three possibilities for 'Group' however so I'd also have to specify in the module WHICH selections from the field list map to which terms from the taxonomy to make sure the selections are correct.

For example, if a user selects 'paper' in the field list then Group should auto-select the term 'recyclable' before saving the record.

Anyone know how to do this?

I was going to approach it with hook_form_alter and hook_node_presave...but I'm not very comfortable with coding in modules yet.


  • it could probably be done using Rules but exactly how depends on your taxonomy term set up – Geoff May 1 '15 at 0:06
  • If you're trying to do this once the page has loaded based on user input, you'll need AJAX and/or JavaScript. – Darvanen May 1 '15 at 2:14

If the field is hidden and doesn't do anything client-side, you shouldn't touch it client-side. Do the Category -> Group translation server-side even if you're not very comfortable with it yet.

hook_node_presave should do the trick.

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.