While integrating AHAH functionality into a compound CCK field that I'm currently working on, I ran into an issue to where my "checkboxes" FAPI type were not rendering when the form was rebuilt via AHAH.
<?php function correl_field_search_results_js() { include_once 'modules/node/node.pages.inc'; $form_state = array('storage' => NULL, 'submitted' => FALSE); $form_build_id = $_POST['form_build_id']; $form = form_get_cache($form_build_id, $form_state); $args = $form['#parameters']; $form_id = array_shift($args); $form_state['post'] = $form['#post'] = $_POST; $form['#programmed'] = $form['#redirect'] = FALSE; drupal_process_form($form_id, $form, $form_state); $form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id); $search_links = correl_field_get_links_from_search(NULL, $form_state['values']['field_correl_poe'][0]['correl_search_keywords']); $i = 1; foreach ($search_links as $link) { $options_array[$i] = "<a target=\"parent\" href=\"" . $link['url'] . "\">" . $link['title'] . "</a>"; $i++; } if (isset($form_state['values']['nid'])) { variable_set('search_results_' . $form_state['values']['nid'], $options_array); } else { variable_set('search_results_' . session_id(), $options_array); } $search_form['field_correl_poe'][0]['correl_search_results'] = array( '#type' => 'checkboxes', '#title' => t('Search Results'), '#options' => $options_array, ); $output = theme('status_messages'); drupal_json(array('status' => TRUE, 'data' => $output . drupal_render($search_form))); } ?>
What I am doing is generating a list of checkboxed search_results from search keywords provided from the user.
The issue that occurred was that the checkboxes would not render when the form was rebuilt.
I tested this by going through each available FAPI type and returning a form element of that type. The types that I found would not render were both "checkboxes" and "radios". Every other type would render correctly.
Doing some research, I found that the same issue had reported as a bug for earlier versions of Drupal (6.12 to be exact) and those issues were still active.
After some brainstorming, I decided to approach the same problem from a slightly different angle. Instead of using the drupal_render() function to render my form elements, I decided to use drupal_get_form() which would also allow me to pass extra arguments. Also, the code that would generate the search results array and the form array, I would move into a separate function that I would reference in my drupal_get_form() call.
Here's the solution that I came up with:
<?php function correl_field_search_results_js() { include_once 'modules/node/node.pages.inc'; $form_state = array('storage' => NULL, 'submitted' => FALSE); $form_build_id = $_POST['form_build_id']; $form = form_get_cache($form_build_id, $form_state); $args = $form['#parameters']; $form_id = array_shift($args); $form_state['post'] = $form['#post'] = $_POST; $form['#programmed'] = $form['#redirect'] = FALSE; drupal_process_form($form_id, $form, $form_state); $form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id); $output = theme('status_messages'); drupal_json(array('status' => TRUE, 'data' => $output . drupal_get_form('correl_field_search_results_form', $form_state))); } /** * build our form to render when the form is rebuilt **/ function correl_field_search_results_form(&$form_state) { $search_links = correl_field_get_links_from_search(NULL, $form_state['values']['field_correl_poe'][0]['correl_search_keywords']); $i = 1; foreach ($search_links as $link) { $options_array[$i] = "<a target=\"parent\" href=\"" . $link['url'] . "\">" . $link['title'] . "</a>"; $i++; } if (isset($form_state['values']['nid'])) { variable_set('search_results_' . $form_state['values']['nid'], $options_array); } else { variable_set('search_results_' . session_id(), $options_array); } $search_form['field_correl_poe'][0]['correl_search_results'] = array( '#type' => 'checkboxes', '#title' => t('Search Results'), '#options' => $options_array, ); return $search_form; } ?>
Mind you, this is not complete as there is still some work to be done but, I wanted to demonstrate my solution for getting the "checkboxes" to render properly (This will also work with the "radios" FAPI type as well).
Feel free to leave comments or questions.
Post new comment