I have a custom widget form that I use on a node reference field. The form outputs a list of nodes with a pager (jQuery Infinite Scroll plugin to append next results using the pager links) which can be selected via checkboxes. The results can be filtered via ajax based on a taxonomy term selection and the old results get replaced with new ones using the ajax command $commands[] = ajax_command_replace
. So far so good.
The issue comes after the results are filtered using ajax and the infinite scroll (jQuery plugin) tries to call the next page of results (ie: page 2). Using firebug I can see the GET request returns a 200 ok status message but no results are appended to the dom. When I check the logs I see the following errors:
Undefined index: form_build_id in ajax_get_form() (line 320 of /var/www/html/includes/ajax.inc).
http://mywebsite.com/system/ajax?page=2|http://mywebsite.com/create/list|5372||Invalid form POST data.
This is the ajax callback when filtering the form:
function nodereference_selector_field_widget_form_ajax_callback(&$form, &$form_state) {
$commands = array();
// Replace the old results when filtering occurs.
$commands[] = ajax_command_replace('.form-item-field-list-und', drupal_render_children($form['field_list']));
return array('#type' => 'ajax', '#commands' => $commands);
}
This is the widget form function:
function nodereference_selector_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element, $values = array()) {
$all_categories = array(36, 38, 40, 41, 194, 327, 1308, 1335, 1336, 1337);
$category = nodereference_selector_get_categories();
$category = array('all' => t('Display All')) + $category;
$element['itemswrapper'] = array(
'#weight' => 0,
'#suffix' => '<div id="item-area">',
);
$form['selectedwrapper'] = array(
'#weight' => -10,
"#markup" => '<div id="selected-items"><span id="instructions">Select items from the list to create your list.</span></div>',
);
$form['tid'] = array(
'#type' => 'select',
'#options' => $category,
'#default_value' => isset($values['tid']) ? $values['tid'] : 'all',
'#ajax' => array(
'callback' => 'nodereference_selector_field_widget_form_ajax_callback',
'progress' => array('type' => 'throbber', 'message' => 'Loading...'),
'wrapper' => 'item-area',
),
'#weight' => 0,
);
$values = isset($form_state['values']) ? $form_state['values'] : array('tid' => NULL);
$radios = $field['cardinality'] == 1 && $element['#required'];
$options = array();
foreach ($field['settings']['referenceable_types'] as $type) {
if (is_numeric($type) || !isset($instance['widget']['settings']['image_field_' . $type])) {
continue;
}
$query = db_select('node', 'n');
$query->addTag('node_access');
$query->join('field_data_' . $instance['widget']['settings']['image_field_' . $type], 'fd', 'fd.entity_id = n.nid');
$query->join('taxonomy_index', 'ti', 'ti.nid = n.nid');
$query->fields('n', array('title', 'nid'));
$query->fields('fd', array($instance['widget']['settings']['image_field_' . $type] . '_fid'));
$query->condition('n.type', $type, '=');
$query->condition('fd.delta', 0, '=');
$query->condition('n.status', 1, '=');
$query->condition('tid', $all_categories);
$query->orderRandom();
nodereference_selector_filter_query($query, $values);
$result = $query->extend('PagerDefault')->limit(20)->execute();
foreach ($result as $node) {
$options[$node->nid] = theme('image_style', array(
'style_name' => $instance['widget']['settings']['image_style'],
'path' => file_load($node->{$instance['widget']['settings']['image_field_' . $type] . '_fid'})->uri,
'title' => check_plain($node->title),
)
);
}
}
$defaults = array();
if (isset($form_state['build_info']['args'][0]->{$instance['field_name']}[$langcode])) {
if (!$radios) {
foreach ($form_state['build_info']['args'][0]->{$instance['field_name']}[$langcode] as $value) {
$defaults[] = $value['nid'];
}
}
else {
$defaults = $form_state['build_info']['args'][0]->{$instance['field_name']}[$langcode][0]['nid'];
}
}
$element += array(
'#type' => $radios ? 'radios' : 'checkboxes',
'#options' => $options,
'#element_validate' => array('nodereference_selector_validate'),
'#attributes' => array(
'class' => array('clearfix'),
),
'#attached' => array(
'js' => array(
drupal_get_path('module', 'nodereference_selector') . '/js/nodereference_selector.js',
),
'css' => array(
drupal_get_path('module', 'nodereference_selector') . '/css/nodereference_selector.css',
),
),
'#default_value' => $defaults,
);
// Attach the pager theme
$element['pager_pager'] = array('#theme' => 'pager');
$element["itemswrapper"]['#prefix'] = "</div>";
return $element;
}