5

Trying to make a custom page to add taxonomy terms. The page contains one drop-down menu that lists all enabled vocabularies. Full taxonomy add form loaded after I select some from that menu.

This is menu callback:

    function add_term_form_callback($form, $form_state) {

      module_load_include('inc', 'taxonomy', 'taxonomy.admin');
      $vid = $form_state['values']['vocabulary'];
      $vocabulary = taxonomy_vocabulary_load($vid);

      $form_state['values'] = array();
      $form_state['build_info']['args'] = array(array(), $vocabulary);

      $form_state['rebuild'] = TRUE;
      $form_state['no_cache'] = TRUE;
      $form = drupal_build_form('taxonomy_form_term', $form_state);

      return $form;

    }

...and form

    function add_term_form($form, &$form_state) {

      $options = array();
      $options[0] = t('- Select -');
      $vocabularies = taxonomy_get_vocabularies();
      foreach ($vocabularies as $vocabulary) {
        $options[$vocabulary->vid] = $vocabulary->name;
      }

     $form['vocabulary'] = array(
       '#type' => 'select', 
       '#title' => t('Select vocabulary'), 
       '#options' => $options, 
       '#default_value' => $selected,
       '#ajax' => array(
         'callback' => 'add_term_form_callback', 
         'wrapper' => 'container',
         'event' => 'change',
       ),
     );

      $form['container'] = array(
       '#type' => 'container',
       '#prefix' => '<div id="container">', 
       '#suffix' => '</div>',
      );

      return $form;
    }

So the problem is that the form remains in the same state after first load. How to refresh the form properly?

2

According to the documentation:

Changes to the form must only be made in the form builder function (ajax_example_autocheckboxes() in the example here), or validation will fail. The callback function must not alter the form or any other state.

You have been changing the form_state and the form in the callback.

Based on how I understood the documentation, you should be just returning the whole form in your ajax callback and then do the change in your form in the form builder function. You can just check the form_state value of the drop down to know which form to render. I imagine it'll be something like this:

function add_term_form_callback($form, $form_state) {
  return $form;
}

 function add_term_form($form, &$form_state) {

 // If vocabulary has value
      // do stuff
      // $form = drupal_get_form('taxonomy_form_term');
 // else :
  $options = array();
  $options[0] = t('- Select -');
  $vocabularies = taxonomy_get_vocabularies();
  foreach ($vocabularies as $vocabulary) {
    $options[$vocabulary->vid] = $vocabulary->name;
  }

 $form['vocabulary'] = array(
   '#type' => 'select', 
   '#title' => t('Select vocabulary'), 
   '#options' => $options, 
   '#default_value' => $selected,
   '#ajax' => array(
     'callback' => 'add_term_form_callback', 
     'wrapper' => 'container',
     'event' => 'change',
   ),
 );

  $form['#prefix'] = '<div id="container">';
  $form['#suffix'] = '</div>';
  );
  // endif; 

  return $form;
}

EDIT: I just noticed that you weren't wrapping the form but a container in the form. you would have to wrap the form if you want the whole form to be reloaded.

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.