2

I'm working on a site which uses Apache Solr for the site search which is on the URL /search/site/.

What I want to do is add a form to every page on the site which allows the user to entered text in & submit it. The form will then take the user to the /search/site/ page displaying the results for their search term.

This sounds like it should be simple but I cannot see how to do it, I cannot find a way of creating a block for it & when I try to use the code below to programmatically display the form it is presented to the user but when submitted they are taken to the correct page (/search/site/) but the results of their search are not displayed.

$search_form = drupal_get_form('search_form');
$search_form_box = drupal_render($search_form);
print $search_form_box;
print render(drupal_get_form('search_form'));

I wasn't 100% sure that "search_form" was correct but when submitted it does take you to the correct search results page (/search/site/) but no terms or results are displayed.

I'm quite new to using Drupal & Solr so if there's any information you need that I missed out please ask.

1

I managed to work this one out, to get the block ready to be displayed I used preproccess_region (see code below)

function THEMENAME_preprocess_region(&$variables){
    $site_search_block = module_invoke('search', 'block_view', 'search');
    $variables["site_search_block"] = render($site_search_block);
}

Then in in my region .tpl file (region--menu.tpl.php for me) I put the following code to display it.

<?php if($site_search_block){ print $site_search_block; } ?>

As an additional bit of information if you want to edit the output of the form as I did, although I didn't ask about it here, this function should help you:

function THEMENAME_form_search_block_form_alter(&$form, &$form_state, $form_id) {
    $form['#id'] = 'siteSearchForm'; //Set form element ID

    //The form elements (I think)
    $form['search_block_form']['#title'] = t('Search'); // Change the text on the label element
    $form['search_block_form']['#title_display'] = 'invisible'; // Toggle label visibilty
    $form['search_block_form']['#size'] = 10;  // define size of the textfield
    //$form['search_block_form']['#default_value'] = t('SEARCH'); // Set a default value for the textfield

    //Submit button element values
    $form['actions']['submit']['#value'] = t('GO!'); // Change the text on the submit button
    $form['actions']['submit'] = array('#type' => 'image_button', '#src' => base_path() . path_to_theme() . '/img/search-icn.png');

    //Add extra attributes to the text box
    //$form['search_block_form']['#attributes']['onblur'] = "if (this.value == '') {this.value = 'SEARCH';}";
    //$form['search_block_form']['#attributes']['onfocus'] = "if (this.value == 'SEARCH') {this.value = '';}";

    //Prevent user from searching the default text
    //$form['#attributes']['onsubmit'] = "if(this.search_block_form.value=='SEARCH'){ alert('Please enter a search'); return false; }";

    //HTML5 placeholder attribute instead of using the javascript
    $form['search_block_form']['#attributes']['placeholder'] = t('SEARCH');
}
| improve this answer | |

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.