1

Here, I am working in drupal Commons plugin. I already customized search form in that drupal header.Here i include modify search form in custom theme:

function mytheme_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'search_block_form') {
    $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']['#default_value'] = t('Search'); // Set a default value for the textfield
    $form['custom_search_types']['#attributes'] = array('style' => 'display:none'); // Set a default value for the textfield
    $form['search_block_form']['#attributes']['class'][] = 'form-control'; // Set a default value for the textfield
    $form['actions']['submit']['#value'] = 'OK!'; // Change the text on the submit button

    $form['actions']['submit'] = array('#type' => 'button','#value' => 'OK!');

    // 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; }";
    $form['actions']['submit']['#attributes']['class'][] = 'input-group-addon';

    // Alternative (HTML5) placeholder attribute instead of using the javascript
    $form['search_block_form']['#attributes']['placeholder'] = t('Search');
  }
}

And i need to redirect search result page using that above search form.But I type content and hit enter it remain same page.How to redirect into search result page with custom form values.I just customized form submit in custom theme

function mytheme_search_box_form_submit($form, &$form_state) {
print_r($form);//No results 
}

How to achieve it. kindly guide me..

1

You can use the redirect attribute in form_state as follows,

function mytheme_search_box_form_submit($form, &$form_state) {
   $form_state['#redirect'] = 'Your path to redirect';
}
0

Here's a complete example. First you add a custom form handler:

function yourmodule_form_alter(&$form, $form_state, $form_id) {

  switch ($form_id)  {

  case 'search_form':
    $form['#submit'][] = 'yourmodule_search_box_form_submit';
  break;

  }
}

Then you redirect. Here's an example adding the search terms to the URL:

function yourmodule_search_box_form_submit($form, &$form_state) {

   $search_terms = $form_state['input']['keys'];

   $form_state['redirect'] = 'some/path/'.check_url( $search_terms );

}

If it doesn't work, try adding $form_state['rebuild'] = FALSE; to the second function, and try toggling between $form_state['redirect'] and $form_state['#redirect']

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.