This may just be a FAPI/render issue, but I'm only seeing this in an apachsolr hook, so I'm starting here. I'm having an issue trying to add some fieldsets for upfront filters to a custom search page using hook_apachesolr_search_page_alter(). For certain reasons, I need to add these form items to the search form, as compared to as a separate form. If I do something like

function mymodule_apachesolr_search_page_alter(&$build, $search_page) {
  // Add form to save searches for only our desired page and only if there are search results.
  if ($search_page->page_id == 'saved_search' && isset($build['search_results']['#results'])) { 
    $build['save_form'] = drupal_get_form('mymodule_filters_form', $build, $search_page);
  }
}

with this form function

function mymodule_filters_form(&$build, &$search_page) {

  $form['search_filters'] = array(
    '#type' => 'fieldset',
    '#title' => t('Filter Search'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#prefix' =>'<div id="search-filters">',
    '#suffix' => '</div>',
  );
  
  // Add All Sources checkbox.
  $form['search_filters']['all_sources'] = array(
    '#type' => 'checkbox',
    '#title' => t('All Sources'),
    '#default_value' => 1,
  );    
  
  // Get all taxonomy vocabs used for classfying feed sources and create a fieldset for each of them.
  $fields = field_info_instances('node', 'feed');
  $blocks = array();
  foreach ($fields as $field) {
    if ($field['bundle'] == 'feed' && $field['display']['default']['module'] == 'taxonomy') {
      $field_info = field_info_field($field['field_name']);
      $vocab_name = $field_info['settings']['allowed_values'][0]['vocabulary'];

      $result = db_query("SELECT vid, name, machine_name FROM {taxonomy_vocabulary} WHERE machine_name =:name", array(':name' => $vocab_name));
      $record = $result->fetchObject();
      $vid = $record->vid;
      $vocab_full_name = $record->name;
    
      $vocab = taxonomy_get_tree($vid);
      foreach($vocab as $term) {
        $terms[$term->tid] = $term->name;   
      } 
      
      // Get form values from ctools object cache for default values if they exist.
      $default_values = array();
      ctools_include('object-cache');
      $cache = ctools_object_cache_get('solr_filters', 'vocab_' . $vid);
      if ($cache) {
        foreach ($cache->tids as $tid) {
          if (in_array($tid, array_keys($terms))) {
            $default_values[] = $tid;
          }
        }   
      } 
      
      $form['search_filters']['vocab_' . $vid] = array(
        '#type' => 'fieldset',
        '#title' => t($vocab_full_name),
        '#collapsible' => TRUE,
        '#collapsed' => TRUE,
        '#tree' => TRUE,
      );
      
      $form['search_filters']['vocab_' . $vid]['vid_terms'] = array(
        '#type' => 'checkboxes',
        '#options' => $terms,
        '#default_value' => $default_values,
      );
      // Clear out $terms array for next vocab.
      $terms = NULL;
    }
  }
    
    return $form;
}

I get the form added just fine. However, it's as a separate form, which doesn't work for what I need. If I add the form values directly in this hook like so,

function mymodule_apachesolr_search_page_alter(&$build, &$search_page) {
  if ($search_page->page_id == 'saved_search') {  
    $build['search_form']['search_filters'] = array(
      '#type' => 'fieldset',
      '#title' => t('Filter Search'),
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
      '#prefix' =>'<div id="search-filters">',
      '#suffix' => '</div>',
    );
    
    // Add All Sources checkbox.
    $build['search_form']['search_filters']['all_sources'] = array(
      '#type' => 'checkbox',
      '#title' => t('All Sources'),
      '#default_value' => 1,
    );    
    
    // Get all taxonomy vocabs used for classifying feed sources and create a fieldset for each of them.
    $fields = field_info_instances('node', 'feed');
    $blocks = array();
    foreach ($fields as $field) {
      if ($field['bundle'] == 'feed' && $field['display']['default']['module'] == 'taxonomy') {
        $field_info = field_info_field($field['field_name']);
        $vocab_name = $field_info['settings']['allowed_values'][0]['vocabulary'];
  
        $result = db_query("SELECT vid, name, machine_name FROM {taxonomy_vocabulary} WHERE machine_name =:name", array(':name' => $vocab_name));
        $record = $result->fetchObject();
        $vid = $record->vid;
        $vocab_full_name = $record->name;
      
        $vocab = taxonomy_get_tree($vid);
        foreach($vocab as $term) {
          $terms[$term->tid] = $term->name;   
        } 
        
        $build['search_form']['search_filters']['vocab_' . $vid] = array(
          '#type' => 'fieldset',
          '#title' => t($vocab_full_name),
          '#collapsible' => TRUE,
          '#collapsed' => TRUE,
          '#tree' => TRUE,
        );
        
        $build['search_form']['search_filters']['vocab_' . $vid]['vid_terms'] = array(
          '#type' => 'checkboxes',
          '#options' => $terms,
        );    
      }
    }
  }
}

the tid values are not rendered in the checkboxes fields ($build['search_form']['search_filters']['vocab_' . $vid]['vid_terms']) the way they are when adding as a separate form. The fieldsets are there, but the checkboxes aren't. I've checked all the values at the end of this function, and everything is populated as it should be.

Can anyone figure why the field elements won't render this way, but they will if I call the form separately?

Thanks.

Comments

nick_vh’s picture

Status: Active » Fixed

wonder95 told me I could close this one and mark as fixed :-)

nick_vh’s picture

Status: Fixed » Closed (fixed)