0

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.

4
  • I am not seeing where the function chapter_sponsor_levels_update_levels is defined.
    – Coleman
    Oct 31, 2014 at 18:48
  • @Coleman - I just added it into the post Oct 31, 2014 at 19:13
  • why have you got und in your form item? $form['field_sponsor_level']['und']['#title']
    – 2pha
    Nov 1, 2014 at 3:53
  • @2pha - I have an und because thats the arrary structure I see in the dpm of $form. It clearly is correct because the array is modified by the ajax request. My issue is that its not saving the form data. Nov 3, 2014 at 13:58

1 Answer 1

0

Ok, I finally got it right, here's the code of the function which does the work:


function chapter_sponsors_levels_set_levels($gid, &$form, &$form_state) {
  $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_state['field']['field_sponsor_level']['und']['field']['settings']['allowed_values'][$name] = $name;
    }
    $form['field_sponsor_level']['und']['#default_value'] = $form_state['build_info']['args'][0]->field_sponsor_level['und'][0]['value'];
    $form['field_sponsor_level']['und']['#title'] = $chapter_vocab->name;
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.