Drupal AHAH: Issues with drupal_render()

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.

0 comments on Drupal AHAH: Issues with drupal_render()

    Post new comment

    The content of this field is kept private and will not be shown publicly.
    • Web page addresses and e-mail addresses turn into links automatically.
    • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
    • Lines and paragraphs break automatically.
    • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <c>, <cpp>, <drupal5>, <drupal6>, <java>, <javascript>, <php>, <python>, <ruby>. PHP source code can also be enclosed in <?php ... ?> or <% ... %>.

    More information about formatting options

    CAPTCHA
    If you are if fact human, you may post comments.
    Image CAPTCHA
    Enter the characters shown in the image.