0

I'm using Drupal 8 and i've installed search API and Search API Solr Search.

I followed this great tutorial

How can I display a search box on all of the pages of my website ?

1

Since search api uses Views to create the page, you can use Views to also create a search block. There are many ways to do such a thing with views. The simplest is to output the exposed form as a block (advanced settings).

  • Thank you it worked but the search box disappeared in the search page ..do you know how can i make it work to have both ? – usethe23 Oct 17 '16 at 16:27
  • 1
    Add it twice on the search page – googletorp Oct 17 '16 at 16:50
3

Although you can expose the views form as a block and you can add it twice to a page, the issue is that you may just want a keyword search form in the header but on the actual form you want something more. An additional peculiarity was that the infinite scrolling plugin makes the form ajax, and when exposed as a block it fails to submit.

My solution was to create a new form + block and place the block it where I needed it: Folder structure

my_search.module:

<?php
use Drupal\Core\Form\FormStateInterface;

/**
 * Implements hook_form_FORM_ID_alter() for the search_block_form form.
 */
function my_search_form_header_search_alter(&$form, FormStateInterface $form_state) {
  // NB so that these are not added to the get query params
  $form['form_build_id']['#access'] = FALSE;
  $form['form_token']['#access'] = FALSE;
  $form['form_id']['#access'] = FALSE;
}

HeaderSearch.php:

<?php

namespace Drupal\my_search\Form;

use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;

/**
 * Class HeaderSearch.
 *
 * @package Drupal\my_search\Form
 */
class HeaderSearch extends FormBase {


  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'header_search';
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {

    $form['#action'] = '/search';
    $form['#method'] = 'get';
    $form['k'] = [
      '#type' => 'search',
      '#title' => $this->t('Keyword'),
      '#maxlength' => 64,
      '#size' => 15,
      '#title_display' => 'invisible',
    ];

    $form['submit'] = [
      '#type' => 'submit',
      '#value' => $this->t('Search'),
      // Prevent op from showing up in the query string.
      '#name' => '',
    ];

    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    // Form is redirected no need for anything here.
  }
}

MySearchBlock.php:

<?php

namespace Drupal\my_search\Plugin\Block;

use Drupal\Core\Block\BlockBase;

/**
 * Provides a 'MySearchBlock' block.
 */
class MySearchBlock extends BlockBase {

  /**
   * {@inheritdoc}
   */
  public function build() {
    return \Drupal::formBuilder()->getForm('Drupal\my_search\Form\HeaderSearch');
  }

}
  • You have to make sure to give it a @Block directive in the comments for MySearchBlock too, otherwise it won't show up on the blocks page. – Matt Fletcher Jul 26 '18 at 14:36
  • Can I also add that you may as well just use search_api_page rather than rolling out a custom module. It provides a block and also the results page too. – Matt Fletcher Jul 26 '18 at 15:02

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.