You are here

social_search.module in Open Social 8

The Social search module.

File

modules/social_features/social_search/social_search.module
View source
<?php

/**
 * @file
 * The Social search module.
 */
use Drupal\Core\Block\BlockPluginInterface;
use Drupal\Core\Form\FormStateInterface;

/**
 * Implements hook_form_FORM_ID_alter().
 *
 * Enhance the Views exposed filter blocks forms.
 */
function social_search_form_views_exposed_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  $filter_forms = [
    'views-exposed-form-search-content-page',
    'views-exposed-form-search-users-page',
  ];
  if (in_array($form['#id'], $filter_forms)) {

    // Set current path as form action, in order to keep search input.
    $form['#action'] = \Drupal::service('path.current')
      ->getPath();

    // Always enable the reset button.
    $form['actions']['reset']['#access'] = TRUE;
    if ($form['#id'] == 'views-exposed-form-search-users-page') {
      foreach ($form as &$element) {
        if (is_array($element) && isset($element['#type']) && $element['#type'] == 'entity_autocomplete') {
          $element['#selection_settings']['hide_id'] = TRUE;
        }
      }
    }
  }
}

/**
 * Implements hook_block_view_BASE_BLOCK_ID_alter().
 *
 * Enhance the Views exposed filter blocks.
 */
function social_search_block_view_views_exposed_filter_block_alter(array &$build, BlockPluginInterface $block) {
  $filter_blocks = [
    'search_content-page',
    'search_users-page',
  ];
  if (in_array($build['#derivative_plugin_id'], $filter_blocks)) {

    // Disable cache for exposed filter block to get correct current path,
    // which is used in $form['#action'].
    $build['#cache'] = [
      'max-age' => 0,
    ];
  }
}

/**
 * Implements hook_block_view_BASE_BLOCK_ID_alter().
 *
 * Make the label/title translatable.
 */
function social_block_view_search_hero_block_alter(array &$build, BlockPluginInterface $block) {
  $build['#configuration']['label'] = t('Search');
}