0

How can I just show front page when no letters were printed in search input, but it was submitted?

And optionally maybe show message 'please enter the terms of the search'

I thought of doing this with Rules, but no rules considering searches are present(

  • u have to look into form alter hook. – Ali Nouman Dec 13 '13 at 8:31
3

Here is a solution:

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

  if($form_id == 'search_block_form' || $form_id == 'search_form') {
    array_unshift($form['#validate'], 'custom_search_validate');
  }
}

function custom_search_validate($form, &$form_state) {
  dpm($form_state);
  if(isset($form_state['values']['search_block_form']) && $form_state['values']['search_block_form'] == FALSE) {
    drupal_set_message('please enter the terms of the search', 'error');
    drupal_goto();
  }

  if(isset($form_state['values']['keys']) && $form_state['values']['keys']
    == FALSE) {
    drupal_set_message('please enter the terms of the search', 'error');
    drupal_goto();
  }
}
  • 1
    Hello. your answer is pretty OK, but few words of explanation for the readers not fluent with hooks, form api and module writing could significantly improve it. – Mołot Dec 13 '13 at 8:54
  • @Mołot Sorry. I'll consider it. I edited my answer. Thanks. – xurshid29 Dec 13 '13 at 8:57
  • @Lambrusco.Newbie I'm glad to help you. – xurshid29 Dec 13 '13 at 9:09

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.