2

I've got a content type with a dropdown with some states. And I've got another list (radio buttons) which should change the dropdown based on the selected item. Is that somehow possible?

I've tried Conditional Fields, but that didn't do what I want. Maybe rules? I'm kind of newbie with Drupal.

2
  • What do you mean by changing the dropdown ? Changing the options in it or displaying an other dropdown ?
    – pbonnefoi
    Jun 30, 2014 at 13:42
  • 1
    Lets say I have a dropdown with options "opened, closed, progress" and another one with "1, 2, 3". If I select any option on the second dropdown, the first should automatically change to "progress".
    – Andi
    Jul 1, 2014 at 7:26

2 Answers 2

3

If you want to do this hardcoded, you might try to use the #ajax property of the Form API

You can reload your second dropdown on change of the value of the first dropdown and change the value while reloading the form.

I made a little example to give you an idea :

$form['number'] = array(
  '#type' => 'select',
  '#title' => t('Select Number'),
  '#options' => array(
      0 => 1,
      1 => 2,
      2 => 3,
  ),
  '#default_value' => 0,
  '#ajax' => array(
    'callback' => 'ajax_replace_dropdown_default_value_callback',
    'wrapper' => 'wrapper-dropdown',
    'method' => 'replace',
    'effect' => 'fade',
  ),
);

$default_value = !empty($form_state['values']['number']) ? $form_state['values']['number'] : 0;

$form['open_closed'] = array(
  '#type' => 'select',
  '#title' => t('Open / Closed ?'),
  '#options' => array(
      0 => t('Open'),
      1 => t('Closed'),
      2 => t('Progress'),
  ),
  '#value' => $default_value,
  '#prefix' => '<div id="wrapper-dropdown">',
  '#suffix' => '</div>',
);

function ajax_replace_dropdown_default_value_callback($form, $form_state) {
  return $form['open_closed'];
}

In your case you will have to do it in a hook_form_alter and add the #ajax property to your first dropdown and add prefix and suffix to give the second dropdown the proper value.

You can also check this link about AJAX with the FORM API :

2
  • No problem, glad to know that this was what you were looking for ;-)
    – pbonnefoi
    Jul 1, 2014 at 12:51
  • Hello if this answer was good, can you mark it as one ? Thanks ;-)
    – pbonnefoi
    Sep 17, 2015 at 12:36
0

I've used a module before called Reference Option Limit. Basically you setup 2 fields as taxonomy term references (each with it's own taxonomy vocabulary), and then on each element of the second dropdown, you select which parent item it should show for. The only issue I ever had with it was it didn't always work for anonymous users if caching was enabled.

0

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.