This is a continuation from this question,where I'm wanting to display a form for searching Apache Solr and displaying the results with a pager. I've modified my approach to have the search run from the page function if there are values in the form. As I have it now, the search is run and results are returned just fine, but for some reason I can't get the table to render when I pass a render array through ajax_render(). Here's the relevant code:
/**
* Page callback for imgsearch modal popup window.
*/
function imgsearch_page($ajax, $id, $a, $b) {
if ($ajax) {
//Load the modal library and add the modal javascript.
ctools_include('ajax');
ctools_include('modal');
$form_state = array(
'ajax' => TRUE,
'title' => t('Image Search Form'),
'next_field_id' => $id,
);
// Use ctools to generate ajax instructions for the browser to create
// a form in a modal popup.
$search_form = ctools_modal_form_wrapper('imgsearch_form', $form_state);
if ($search_form[0]) {
$output = $search_form;
}
else {
$build['search_form'] = $search_form;
}
// If the form has been submitted, there may be additional instructions
// such as dismissing the modal popup.
if (!empty($form_state['ajax_commands'])) {
$output = $form_state['ajax_commands'];
}
if($form_state['values']['search_terms'] != '') {
// See apachesolr_saved_searches_get_results() in ozmosys project for code to run solr search.
$results = nb_image_search_search($form_state['values']);
if (is_array($results['images']) && count($results['images'] > 0)) {
$next_field_id = $form_state['next_field_id'];
// Create object to store file and target field info. To be stored in ctools cache.
$file_info = new stdClass();
// Generate the field name. field_images is a multivalue field collection, so we just need the next available option
// in the $field_images['und'] array. The second number (for field_image) will always be 0 since it
// is a single value field.
$file_info->fieldname['url'] = '#edit-field-images-und-' . $next_field_id . '-field-image-und-0-imgsearch-file-url';
$file_info->fieldname['person'] = '#edit-field-images-und-' . $next_field_id . '-field-person-und-0-value';
$file_info->fieldname['organization'] = '#edit-field-images-und-' . $next_field_id . '-field-organization-und-0-value';
$file_info->fieldname['year'] = '#edit-field-images-und-' . $next_field_id . '-field-year-und-0-value';
$file_info->fids = array();
// Theme the results as a table.
$header = array(t('Image'), t('File Name'), t('Add to field'));
$rows = array();
foreach ($results['images'] as $image) {
// Create image style derivative for each image.
$imagestyle = array(
'style_name' => 'thumbnail',
'path' => $image['filepath'] . $image['filename'],
'width' => '',
'height' => '',
'alt' => '',
'title' => $image['filename'],
);
$styled_image = theme('image_style', $imagestyle);
$fid = $image['fid'];
$rows[] = array(
'image' => $styled_image,
'name' => $image['filename'],
'add' => ctools_ajax_text_button("select", "imgsearch/nojs/imgadd/" . $fid . '/' . $next_field_id, t('Select')),
);
$file_info->fids[$fid] = $image['filename'];
// Cache values for Person, Organization, and Year if they exist.
foreach (array('person', 'organization', 'year') as $field) {
if (isset($image[$field])) {
$file_info->meta[$fid][$field] = $image[$field];
}
}
}
//Cache image name in ctools object cache so it can be used later in nb_image_search_image_add()
ctools_include('object-cache');
ctools_object_cache_set('imgsearch', 'imgsearch_' . $next_field_id, $file_info);
// Create a render array ($build) which will be themed as a table with a
// pager.
$build['imgsearch_table'] = array(
'#theme' => 'table',
'#header' => $header,
'#rows' => $rows,
'#empty' => t('There were no matching results found'),
);
// Attach the pager theme.
/*pager_default_initialize($results['total_found'], $results['rows']);
$build['imgsearch_pager'] = array('#theme' => 'pager'); */
}
else {
$build['no_results'] = array(
'markup' => '<div class="no-results>No images found</div>',
);
}
}
if (!$output) {
$output = $build;
}
// Return the ajax instructions to the browser via ajax_render().
print ajax_render($output);
drupal_exit();
}
else {
return drupal_get_form('imgsearch_form', $id);
}
}
What happens is that if the search has been run, the search form is in $build as the $form array, and the second array in $build is imgsearch_table, which is a render array with #theme, #rows, and #header. However, I can't get the table to be rendered or displayed anywhere. I've been banging my head on this for way too long. What do I have to do to get my table to be rendered in my modal window, along with the form?