0

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?

1 Answer 1

0

I finally got the table to render fine every time. Updated code:

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 (($form_state['executed'] && $form_state['ajax']) || isset($_GET['page'])) {
      // 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'] != '' || isset($_GET['search_terms'])) {
        $page_num = isset($_GET['page']) ? $_GET['page'] : 0;
        // See apachesolr_saved_searches_get_results() in ozmosys project for code to run solr search.
        if ($_GET['search_terms']) {
          $form_state['values']['search_terms'] = $_GET['search_terms'];
        }
        $results = nb_image_search_search($form_state['values'], $page_num);

        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['search_form'] = isset($search_form[0]) ? drupal_build_form('imgsearch_form', $form_state) : $search_form;
          $build['imgsearch_table'] = array(
            '#theme' => 'table',
            '#header' => $header,
            '#rows' => $rows,
            '#empty' => t('There were no matching results found'),
          );

          // Attach the pager theme.
          $pager = pager_default_initialize($results['total_found'], $results['rows']);
          $build['imgsearch_pager'] = array(
            '#theme' => 'pager',
            '#parameters' => array(
              'search_terms' => $form_state['values']['search_terms'],
              //TODO: add params for Person, Organization, and Year.
              ),
          );

          $form_state['values']['title'] = t('Search Results');
          $output = ctools_modal_form_render($form_state['values'], $build);

          print ajax_render($output);
          drupal_exit();
        }
        else {
          $build['no_results'] = array(
            'markup' => '<div class="no-results>No images found</div>',
          );
        }
      }
    }
    elseif (!isset($output)) {
      $output = ctools_modal_form_wrapper('imgsearch_form', $form_state);
      // Return the ajax instructions to the browser via ajax_render().
      print ajax_render($output);
      drupal_exit();
    }
  }
  else {
    return drupal_get_form('imgsearch_form', $id);
  }
}

To be honest, I didn't really change anything in regards to the table generation, so I'm suspecting another change I made fixed it.

Your Answer

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

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