0

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.

1 Answer 1

0

I read somewhere that I couldn't use $form_state in a form hook

Not quite. If hook_form_alter() is being executed for the first time, then yes, $form_state can't be used to get a field's value because $form_state['values'] will not be set.

However, if hook_form_alter() is being re-executed, like from an #ajax callback, then $form_state['values'] will be set with the current form values, just like it would be in #submit[] and #validate[] functions. By the way, the $form['#validate'][] functions are also executed during an #ajax callback.

In your case, I believe you would get the current value of field_type from $form_state['values']['field_type'][LANGUAGE_NONE][0] (best to use the constant LANGUAGE_NONE instead of the string 'und'). Wherever you build the #options array for field_target_select during the execution of hook_form_alter(), you would do something like this:

$selected = 'some_default_value';
if (!empty($form_state['values']['field_type'][LANGUAGE_NONE][0])) {
  $selected = $form_state['values']['field_type'][LANGUAGE_NONE][0];
}
$child_options = my_function_to_get_child_options($selected);
$form['field_target_select'][LANGUAGE_NONE]['#options'] = $child_options;

What you have as your ajax callback function in your question looks correct:

function ajax_nudge_taxonomy_selected_callback($form, $form_state) {
  return $form['field_target_select'];
}

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.