I have used form_alter to add a ajax callback to an element to my form. When the user chooses an option, it creates dropdown options for the other field. However, while this works fine for changing the form, it doesn't actually save the value in the field when you click save. I checked the database and its not saved, value is 0. Not exactly sure what is happening.
Here is my code:
/** * Implements hook_form_alter(). */ function chapter_sponsor_levels_form_alter(&$form, &$form_state, $form_id) { if ($form_id === 'sponsor_node_form') { //we need to add an ajax call to the OG Group field on the Sponsor node page, so that it populates the sponsor level dropdown $form['og_group_ref']['und'][0]['default']['#ajax'] = array( 'callback' => 'chapter_sponsor_levels_update_levels', 'wrapper' => 'edit-field-sponsor-level', 'method' => 'replace', ); if(!empty($form['og_group_ref']['und'][0]['default']['#default_value']) && empty($form_state['values'])) { //if we are viewing the form not from an ajax request dpm($form); dpm($form_state); $gid = $form['og_group_ref']['und'][0]['default']['#default_value']; chapter_sponsors_levels_set_levels($gid, $form); dpm($form_state); } elseif (!empty($form_state['values']['og_group_ref']['und'])) { //if we have chosen a group //$gid = 8; //new york $gid = $form_state['values']['og_group_ref']['und'][0]['target_id']; chapter_sponsors_levels_set_levels($gid, $form); } $form['#submit'][] = 'chapter_sponsor_levels_save_level'; } }
function chapter_sponsors_levels_set_levels($gid, &$form) { $query = 'Select * from {og_vocab_relation} where gid = :gid'; $results = db_query($query, array(':gid' => $gid)); foreach ($results as $result) { $vid = $result->vid; } $chapter_vocab = taxonomy_vocabulary_load($vid); //get the vocabulary associated with the group if(!empty($chapter_vocab)) { $terms = entity_load('taxonomy_term', FALSE, array('vid' => $chapter_vocab->vid)); $form['field_sponsor_level']['und']['#options'] = array('_none' => '- None -'); foreach($terms as $term) { $term_names[$term->weight] = $term->name; } ksort($term_names); //sort the terms by their weight and set them as the dropdown values foreach($term_names as $name) { $form['field_sponsor_level']['und']['#options'][] = $name; } $form['field_sponsor_level']['und']['#title'] = $chapter_vocab->name; } }
EDIT: Forgot to add this in to the post
function chapter_sponsor_levels_save_level(&$form, &$form_state) { dpm($form_state); } /* * AJAX callback when click on OG Group field on Sponsor page */ function chapter_sponsor_levels_update_levels($form, $form_state) { return $form['field_sponsor_level']; //update the sponsor_level field with the new dropdown values }
EDIT: I may be getting closer, but not yet there. I realized that if its not saving the value, its proabably because it doesn't match a list of allowed values. While I didn't see such a constraint in the database, I found a 'allowed values' list in the $form_state array. I modified my code above:
foreach($term_names as $name) { $form['field_sponsor_level']['und']['#options'][$name] = $name; $form_state['field']['field_sponsor_level']['und']['field']['settings']['allowed_values'][$name] = $name; }
However, now when I try to save the form when choosing a value, it says that it is an illegal value. I found that it was throwing the error from line 397 in the list module - for some reason, its not passing the 'allowed values' test.
chapter_sponsor_levels_update_levels
is defined.und
in your form item?$form['field_sponsor_level']['und']['#title']