0

I have created a custom search block with Views 3 in Drupal 7. I would like the results of that search be displayed in a page (like the default drupal search block) and not in the block itself (which seems to be the default behaviour for views). How do I accomplish this? I think I am missing a setting somewhere but can't find it.

  • you will probably have to duplicate the search block as a page, and then override the redirect for the submit in the block to point to the page – Geoff Feb 27 '15 at 17:16
  • I do have a search page defined already. How do I override the submit of the block to point to the page? – user5013 Feb 27 '15 at 17:23
0

This is from a project I worked on a while ago. It uses Search Api and has a full search page at product-search/% (a Search Api view). After the redirect in the block's form submit, it places the search term into the search box text field for the search page. It's not a direct answer to your question as you will have to adapt the code if you want to use it.

function MODULE_block_info() {
  $blocks = array();
  $blocks['product_search'] = array(
    'info' => 'Search Products',
    'cache' => DRUPAL_CACHE_GLOBAL,
  );
  return $blocks;
}

function MODULE_block_view($delta) {
  $block =  array();
  switch($delta) {
    case 'product_search':
      $block['subject'] = t('Search Products');
      $block['content'] = drupal_get_form('MODULE_search_products_form');
      break;
  }
  return $block;
}

function MODULE_search_products_form($form, &$form_state) {
  $form['search'] = array(
    '#type' => 'textfield',
    '#title' => t('Terms to search for'),
    '#size' => 28,
    '#maxlength' => 128,
    '#required' => TRUE,
  );

  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Search'));

  return $form;
}

function MODULE_search_products_form_submit($form, &$form_state) {
  drupal_goto('product-search/' . $form_state['values']['search']);
}

function MODULE_form_alter(&$form, &$form_state, $form_id) {
  switch ($form_id) {
    case 'views_exposed_form':
      if ($form_state['view']->name=='product_search_api_view' && $form_state['view']->current_display=='page') {
        // get the view argument if defined
        if (isset($form_state['view']->args[0])) {
          // Set form filter input value to term tid
          if (!isset($form_state['input']['search_api_views_fulltext']))
            $form_state['input']['search_api_views_fulltext'] = $form_state['view']->args[0];
        }
      }
      break;
  }
}

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.