this question comes quite often but I couldn't make it work to that point. My problem is simple, I have a 2 level taxonomy, I have 2 select fields, one is populated with the parent taxonomy and the second should be populated by its children when one parent is selected.
But I'm a bit confused. I followed the developer example with the check-boxes, but still. Also, this happens in hook_form_alter()
and since I'm quite new to Drupal 7, I have to set the options with the ['und']
prefix, but that also troubles my mind because when I omit the ['und']
part when I try to save the node, an error says Illegal choice.
This is my first select field.
//Field TYPE
$form['field_type']['und']['#options'] = std_to_array($vocabulary_nudge_all);
$form['field_type']['#ajax'] = array(
'callback' => 'ajax_nudge_taxonomy_selected_callback',
'wrapper' => 'select-target-div',
'method' => 'replace',
'effect' => 'fade',
);
This is the AJAX callback function where I tried to get the field_type value, but I read somewhere that I couldn't use $form_state
in a form hook.
function ajax_nudge_taxonomy_selected_callback($form, $form_state) {
return $form['field_target_select'];
}
Using $form
, all I could think of was using #delta
.
$selected = $form['field_type']['und']['#delta']
In the second select with a prefix, a suffix, and a visible condition, I am using the following code.
//Field Target SELECT
$form['field_target_select']['#weight'] = '0';
$form['field_target_select']['#prefix'] = '<div id="select-target-div">';
$form['field_target_select']['#suffix'] = '</div>';
$form['field_target_select']['und']['#options'] = std_to_array($vocabulary_nudge_all);
$form['field_target_select']['#states'] = array(
'visible' => array(
':input[name="field_type[und]"]' => array(
array('value' => 'carroussel'),
array('value' => 'vitrine'),
),
),
);
I know there would be a solution by creating several select fields with visible conditions, but later more taxonomy fields could be added.
Right now it's not even doing the AJAX call anymore, and I can't manage to fix this, so any help would be appreciated.