You are here

opigno_search.module in Opigno Search 8

Same filename and directory in other branches
  1. 3.x opigno_search.module

File

opigno_search.module
View source
<?php

/**
 * @file
 * Contains opigno_search.module.
 */
use Drupal\Core\Form\FormState;
use Drupal\Core\Form\FormStateInterface;
use Drupal\views\Views;

/**
 * Get exposed filterers from search view.
 */
function opigno_search_block_form() {
  $view = Views::getView('search_content');
  if (!empty($view)) {
    $view
      ->initHandlers();
    $form_state = new FormState();
    $form_state
      ->setFormState([
      'view' => $view,
      'display' => [
        'id' => 'page_1',
      ],
      'exposed_form_plugin' => $view->display_handler
        ->getPlugin('exposed_form'),
      'method' => 'get',
      'rerender' => TRUE,
      'no_redirect' => FALSE,
      'always_process' => TRUE,
    ]);
    $form = \Drupal::formBuilder()
      ->buildForm('Drupal\\views\\Form\\ViewsExposedForm', $form_state);
    $form['#id'] = $form['#id'] . '_opigon';
    if (isset($form['keys'])) {
      $form['keys']['#title'] = t('Search keys');
    }
    $form['actions']['submit']['#value'] = t('Submit search keys');
    return \Drupal::service('renderer')
      ->render($form);
  }
  else {
    return 'Could not find view "Search Content"';
  }
}

/**
 * Implements hook_form_alter().
 */
function opigno_search_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  switch ($form['#id']) {
    case 'views-exposed-form-search-content-page-1':
      unset($form['#info']['filter-search_api_fulltext']['label']);
      $form['keys']['#type'] = 'search';
      $form['keys']['#title'] = t('Search');
      $form['keys']['#title_display'] = 'invisible';
      $form['#attached']['library'][] = 'opigno_search/search_form';
      $form['#method'] = 'get';
      break;
  }
}

Functions

Namesort descending Description
opigno_search_block_form Get exposed filterers from search view.
opigno_search_form_alter Implements hook_form_alter().