1

I'm working on enhancing a search form on a site I inherited. I'm trying to add a radio button set to allow the user to narrow potential search results, but I cannot for the life of me get the radio buttons to display. The label displays, but the buttons themselves do not.

Here's the function in template.php:

function ismiv3point1_search_block_form($form) {
    $form['search_block_form']['#title'] = ''; 
    $form['search_block_form']['#value'] = 'Search mysite.org';

    $form['submit']['#type'] = 'markup'; 
    $form['submit']['#value'] = '<input type="image" name="submit" value="Search the Site" src="/icons/search.png" alt="Search the Site" />';
    $form['submit']['#weight'] = 2;


    // this is the new field I'm trying to add  
    $form['image_switch'] = array(
        '#type' => 'radios',
        '#title' => t('Image Switch Radios'),
        '#prefix' => '<br /><span style="color:#fff">',
        '#suffix'   => '</span>',
        '#weight' => '3',
        '#options' => array(
            1 => t('Images'),
            2 => t('All'),
        ),  
        '#default_value' => 1       
    );

    return '    
    <div id="search-box" style="margin-top: 10px; margin-bottom: 0px;">'.drupal_render($form).'
        <div id="search-loading"></div>
        <div id="current-type"><a href="#" class="date">Sort by newest first</a></div>
        <ul class="content-types">
            <li><a href="#" class="date">Sort by newest first</a></li>
            <li><a href="#" class="relevance">Sort by relevance</a></li>
        </ul>
    </div>';
}

Here's the rendered source:

<form action="/"  accept-charset="UTF-8" method="post" id="search-block-form">
    <div id="search-box" style="margin-top: 10px; margin-bottom: 0px;">
   <div class="form-item form-text" id="edit-search-block-form-1-wrapper">
      <input type="text" maxlength="128" name="search_block_form" id="edit-search-block-form-1" size="15" value="Search mysite.org" title="Enter the terms you wish to search for." class="form-text" />
   </div>

   <input type="image" name="submit" value="Search the Site" src="/icons/search.png" alt="Search the Site" />
   <input type="hidden" name="form_build_id" id="form-17ad7cfea143f9acf10cca6602a02df0" value="form-17ad7cfea143f9acf10cca6602a02df0"  />
   <input type="hidden" name="form_token" id="edit-search-block-form-form-token" value="65a199ea9da3f7a52ed4c2693012997b"  />
   <input type="hidden" name="form_id" id="edit-search-block-form" value="search_block_form"  />

   <br />
   <span style="color:#fff"> <label>Image Switch Radios</label>
     <div class="form-radios"></div>
   </span>
        <div id="search-loading"></div>
        <div id="current-type"><a href="#" class="date">Sort by newest first</a></div>
        <ul class="content-types">
            <li><a href="#" class="date">Sort by newest first</a></li>
            <li><a href="#" class="relevance">Sort by relevance</a></li>
        </ul>
    </div>
</form>

Any ideas? Is it because the field is being added after the submit? The reason for that is because the submit is a graphic (the magnifying glass) at the end of the search box; we want the radio buttons to be positioned below that.

enter image description here

2

Form builder functions must return a form array, not a string, otherwise they won't work with drupal_get_form(). If you need to add markup before and after the form you you should use the #prefix and #suffix properties, or do it outside of the form function altogether.

Also the function signature for Drupal 6 should not contain the $form variable as an argument, but the $form_state variable instead. And you're currently setting the value (text) of the submit button to the HTML representation of a an <input> field which will never do what you want.

Try using code like this instead:

function ismiv3point1_search_block_form($form_state) {
  $form['search_block_form']['#title'] = ''; 
  $form['search_block_form']['#value'] = 'Search mysite.org';

  $form['submit']['#type'] = 'markup'; 
  $form['submit']['#value'] = 'Search the Site';
  $form['submit']['#weight'] = 2;


  // this is the new field I'm trying to add  
  $form['image_switch'] = array(
      '#type' => 'radios',
      '#title' => t('Image Switch Radios'),
      '#prefix' => '<br /><span style="color:#fff">',
      '#suffix'   => '</span>',
      '#weight' => '3',
      '#options' => array(
          1 => t('Images'),
          2 => t('All'),
      ),  
      '#default_value' => 1       
  );

  $form['#prefix'] = '<div id="search-box" style="margin-top: 10px; margin-bottom: 0px;">';
  $form['#suffix'] = '
      <div id="search-loading"></div>
      <div id="current-type"><a href="#" class="date">Sort by newest first</a></div>
      <ul class="content-types">
          <li><a href="#" class="date">Sort by newest first</a></li>
          <li><a href="#" class="relevance">Sort by relevance</a></li>
      </ul>
  </div>';

  return $form;
}

EDIT

Just a bit of extra info on how to do this with a form alter hook:

function ismiv3point1_form_search_form_alter(&$form, &$form_state, $form_id) {
  $form['image_switch'] = array(
    '#type' => 'radios',
    '#title' => t('Image Switch Radios'),
    '#prefix' => '<br /><span style="color:#fff">',
    '#suffix'   => '</span>',
    '#weight' => '3',
    '#options' => array(
        1 => t('Images'),
        2 => t('All'),
    ),  
    '#default_value' => 1       
  );
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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