0

I want to make the options for a (secondary) term reference autocomplete field dependent on the values chosen in another (primary) autocomplete term reference field. How can I limit the options in the secondary term reference field?

I have two vocabularies called chapter and sub-chapters. In the sub-chapters vocabulary, I have an additional term reference field linking each sub-chapter to the chapter in the chapter vocabulary. (I wanted to use two separate vocabularies, rather than a single hierarchical vocabulary to make it easier to use separate exposed views filters).

On my main content form, I want to limit the sub-chapters available in the autocomplete field to the values chosen in the primary chapters autocomplete field.

I have looked at many different modules for achieving this and haven't found any that will work with my scenario so need to write some code to achieve this. I need to use autocomplete fields as there are so many terms to choose from for both the primary and secondary fields. I have been using the autocomplete deluxe widget for the primary field but I can just use the standard autocomplete widget if this makes it easier.

Can anyone give me a rough outline of the basic code I would need to write in order to achieve this?

I guess there will need to be an update of the options of the secondary term reference field whenever the values are altered in the primary term reference field using AJAX?

0

The basic process would be to make the 2nd field an Ajax (AHAH) created field. Then in this method, set the #autocomplete render element to a URL that uses the first field's value. Something like:

function simple_ahah_select($form_state) {
if ($form_state['storage']['content']['simple_select_parent']) {
  $element = array(
    '#type' => 'textfield',
    '#size' => '60',
   // Define a autocomplete_path depend on the value selected on the parent select field
    '#autocomplete_path' => 'simple/autocomplete/' . $form_state['storage']['content']['simple_select_parent']
  );
  }
}
return $element;

The Autocomplete URL can probably be a view if you don't want to write code. But code would be faster.

See this d.o page for some more details

  • Thank you so much for your answer. I forgot to mention that the first field wil hold more than one value. Will your answer still work with multiple values? – Ben Sep 27 '17 at 13:58
  • It will. VIews contextual filters can use multiple values separated by commas. If you are coding the lookup, you can just split the arg sent to the function. E.g. the path would be simple/autocomplete/tid1,tid2,tid3.. simple/autocomplete would either be a view or a menu option to a function. – CG Monroe Sep 27 '17 at 15:39
  • NOTE: I believe that the best way to return autocomplete info is to have the human label (e.g. title / term) followed by the id in parenthesis. E.g. Level 2 Term A (354) – CG Monroe Sep 27 '17 at 15:42

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.