You are here

function better_search_form_alter in Better Search Block 8

Implements hook_form_alter().

File

./better_search.module, line 30
This module enables basic Better Search functionality.

Code

function better_search_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  if ($form_id == 'search_block_form' || $form_id == 'search_form') {
    $theme = \Drupal::config('better_search.settings')
      ->get('theme');
    switch ($theme) {
      case 0:
        $form['#attached']['library'][] = 'better_search/background_fade';
        break;
      case 1:
        $form['#attached']['library'][] = 'better_search/expand_on_hover';
        break;
      case 2:
        $form['#attached']['library'][] = 'better_search/increase_icon_size';
        break;
      case 3:
        $form['#attached']['library'][] = 'better_search/on_hover_button';
        break;
    }
    $key = $form_id == 'search_block_form' ? 'actions' : 'basic';
    if ($form_id == 'search_block_form') {
      if ($theme == 3) {
        $form['keys']['#suffix'] = '<div class="icon"><i class="better_search"></i></div>';
      }
      else {
        $form['keys']['#prefix'] = '<div class="icon"><i class="better_search"></i></div>';
      }
      $placeholder_text = \Drupal::config('better_search.settings')
        ->get('placeholder_text');
      $form['keys']['#attributes']['placeholder'] = t('@placeholder', [
        '@placeholder' => $placeholder_text,
      ]);
      $form['actions']['#attributes']['class'][] = 'visually-hidden';
      $form['keys']['#size'] = \Drupal::config('better_search.settings')
        ->get('size');
    }
    $altersearchpage = \Drupal::config('better_search.settings')
      ->get('searchpage_enable');
    if ($altersearchpage && $form_id == 'search_form') {
      if ($theme == 3) {
        $form[$key]['keys']['#suffix'] = '<div class="icon"><i class="better_search"></i></div>';
      }
      else {
        $form[$key]['keys']['#prefix'] = '<div class="icon"><i class="better_search"></i></div>';
      }
      $placeholder_text = \Drupal::config('better_search.settings')
        ->get('placeholder_text');
      $form[$key]['keys']['#attributes']['placeholder'] = t('@placeholder', [
        '@placeholder' => $placeholder_text,
      ]);
      $submit_not_visible = \Drupal::config('better_search.settings')
        ->get('searchpage_submit_not_visible');
      if ($submit_not_visible) {

        // Works with Bartik.

        /** @var \Drupal\Core\Theme\ActiveTheme $theme */
        $theme = \Drupal::service('theme.manager')
          ->getActiveTheme();
        $main_theme = $theme
          ->getName();
        $base_themes = $theme
          ->getBaseThemeExtensions();
        if ($main_theme == "bootstrap" || array_key_exists('bootstrap', $base_themes)) {

          /* Works with Bootstrap */
          $form[$key]['submit']['#attributes'] = [
            'class' => [
              'visually-hidden',
            ],
          ];
        }
        else {

          /* Works with other themes */
          if (!isset($form[$key]['submit']['#attributes'])) {
            $form[$key]['submit']['#attributes'] = new Attribute();
          }
          $form[$key]['submit']['#attributes']
            ->addClass('visually-hidden');
        }
      }
      $form[$key]['keys']['#size'] = \Drupal::config('better_search.settings')
        ->get('size');
    }
  }
}