social_search.module in Open Social 10.2.x
Same filename and directory in other branches
- 8.9 modules/social_features/social_search/social_search.module
- 8 modules/social_features/social_search/social_search.module
- 8.2 modules/social_features/social_search/social_search.module
- 8.3 modules/social_features/social_search/social_search.module
- 8.4 modules/social_features/social_search/social_search.module
- 8.5 modules/social_features/social_search/social_search.module
- 8.6 modules/social_features/social_search/social_search.module
- 8.7 modules/social_features/social_search/social_search.module
- 8.8 modules/social_features/social_search/social_search.module
- 10.3.x modules/social_features/social_search/social_search.module
- 10.0.x modules/social_features/social_search/social_search.module
- 10.1.x modules/social_features/social_search/social_search.module
The Social search module.
File
modules/social_features/social_search/social_search.moduleView source
<?php
/**
* @file
* The Social search module.
*/
use Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException;
use Drupal\Component\Plugin\Exception\PluginNotFoundException;
use Drupal\Core\Block\BlockPluginInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\group\Entity\GroupType;
use Drupal\profile\Entity\Profile;
use Drupal\search_api\Entity\Index;
use Drupal\user\Entity\User;
/**
* 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-groups-page',
'views-exposed-form-search-users-page',
];
if (in_array($form['#id'], $filter_forms, TRUE)) {
// Always enable the reset button.
$form['actions']['reset']['#access'] = TRUE;
$form['#attached']['library'][] = 'social_search/search_filters';
switch ($form['#id']) {
case 'views-exposed-form-search-users-page':
social_search_alter_users_exposed_filter_block($form, $form_state, $form_id);
break;
case 'views-exposed-form-search-groups-page':
social_search_alter_groups_exposed_filter_block($form, $form_state, $form_id);
break;
case 'views-exposed-form-search-content-page':
social_search_alter_content_exposed_filter_block($form, $form_state, $form_id);
break;
}
}
}
/**
* Implements hook_form_FORM_ID_alter().
*
* Makes changes for the filter block on the user search page.
*/
function social_search_alter_users_exposed_filter_block(&$form, FormStateInterface $form_state, $form_id) {
foreach ($form as &$element) {
if (is_array($element) && isset($element['#type']) && $element['#type'] === 'entity_autocomplete') {
$element['#selection_settings']['hide_id'] = TRUE;
}
}
if (!empty($form['created']) && !empty($form['created_op'])) {
$form['settings']['created_op'] = $form['created_op'];
$form['settings']['created'] = $form['created'];
unset($form['created_op'], $form['created']);
}
}
/**
* Implements hook_form_FORM_ID_alter().
*
* Makes changes for the filter block on the groups search page.
*/
function social_search_alter_groups_exposed_filter_block(&$form, FormStateInterface $form_state, $form_id) {
$options = [
'' => t('- Any -'),
];
$group_types = GroupType::loadMultiple();
/** @var \Drupal\group\Entity\GroupType $group_type */
foreach ($group_types as $group_type) {
$options[$group_type
->id()] = $group_type
->label();
}
$form['type'] = [
'#type' => 'select',
'#options' => $options,
];
}
/**
* Implements hook_form_FORM_ID_alter().
*
* Makes changes for the filter block on the content search page.
*/
function social_search_alter_content_exposed_filter_block(&$form, FormStateInterface $form_state, $form_id) {
if (!empty($form['type'])) {
$form['type']['#weight'] = '-100';
}
if (!empty($form['field_event_date']) && !empty($form['field_event_date_op'])) {
if (!empty($form['settings'])) {
$form['settings']['#states'] = [
'visible' => [
':input[name=type]' => [
'value' => 'event',
],
],
];
$form['settings']['#weight'] = '-99';
$form['settings']['#attributes']['class'] = [
'indent_filter',
];
}
$form['settings']['field_event_date_op'] = $form['field_event_date_op'];
$form['settings']['field_event_date'] = $form['field_event_date'];
unset($form['field_event_date'], $form['field_event_date_op']);
}
if (!empty($form['location_details'])) {
$form['location_details']['#weight'] = '-98';
$form['location_details']['#attributes']['class'] = [
'indent_filter',
];
}
// When combined sorting and filtering is used we make some changes to
// accommodate theming.
if (isset($form['sort_bef_combine'])) {
// Ensure the sort option shows up at the top.
$form['sort_bef_combine']['#weight'] = -9999;
// From a UX perspective it doesn't make sense to show the least relevant
// results first so we remove this sorting option.
if (isset($form['sort_bef_combine']['#options']['search_api_relevance ASC'])) {
unset($form['sort_bef_combine']['#options']['search_api_relevance ASC']);
}
// We attach custom CSS so that we can properly translate the filter title.
// This selector should match the value in sort_filter.css of this module.
// Use a useless variable to trick phpcs from giving us invalid warnings.
$translated_filter = t('Filter');
$form['social_search_translated_filter_string'] = [
'#type' => 'html_tag',
'#tag' => 'style',
'#attributes' => [
'type' => 'text/css',
],
'#value' => "#block-filter .form-item-sort-bef-combine:after { content: '{$translated_filter}'; }",
];
}
}
/**
* 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_groups-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_views_data_alter().
*/
function social_search_views_data_alter(array &$data) {
// Custom Event Date filters see SocialDate.php.
$data['node']['social_date_filter'] = [
'title' => t('Date Filter by Open Social'),
'filter' => [
'title' => t('Date Filter by Open Social'),
'help' => t('Filters for date used in Open Social'),
'id' => 'social_date_filter',
'field' => 'field_event_date_value',
],
];
$data['node']['social_created_filter'] = [
'title' => t('Created Date Filter by Open Social'),
'filter' => [
'title' => t('Date Filter by Open Social'),
'help' => t('Filters for date used in Open Social'),
'id' => 'social_date_filter',
'field' => 'created',
],
];
}
/**
* Implements hook_search_api_views_handler_mapping_alter().
*/
function social_search_search_api_views_handler_mapping_alter(array &$mapping) {
// Override the Search API views filter connected to date with
// SocialDate.php (Extends current one limits options).
$mapping['date']['filter']['id'] = 'social_date_filter';
}
/**
* 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');
}
/**
* Re-saves search indices.
*
* This triggers the save for all search indexes which triggers updates of
* fields and processors.
*
* @param array $index_ids
* An array of index IDs to re-save or NULL to re-save all indices.
*
* @throws \Drupal\Core\Entity\EntityStorageException
*/
function social_search_resave_search_indexes(array $index_ids = NULL) {
// We load all indexes, we assume there will never be hundreds of search
// indexes which would create its own problems for a site.
$indexes = Index::loadMultiple($index_ids);
/** @var \Drupal\search_api\IndexInterface $index */
foreach ($indexes as $index) {
// No need to disable and enable the index here, this worked around a
// validation of the Search Settings form that prevented the save from being
// triggered. However, this validation is not present in the entity save
// logic. So a simple call to `save()` will trigger the reprocessing of
// fields and processors.
$index
->save();
}
}
Functions
Name | Description |
---|---|
social_block_view_search_hero_block_alter | Implements hook_block_view_BASE_BLOCK_ID_alter(). |
social_search_alter_content_exposed_filter_block | Implements hook_form_FORM_ID_alter(). |
social_search_alter_groups_exposed_filter_block | Implements hook_form_FORM_ID_alter(). |
social_search_alter_users_exposed_filter_block | Implements hook_form_FORM_ID_alter(). |
social_search_block_view_views_exposed_filter_block_alter | Implements hook_block_view_BASE_BLOCK_ID_alter(). |
social_search_form_views_exposed_form_alter | Implements hook_form_FORM_ID_alter(). |
social_search_resave_search_indexes | Re-saves search indices. |
social_search_search_api_views_handler_mapping_alter | Implements hook_search_api_views_handler_mapping_alter(). |
social_search_views_data_alter | Implements hook_views_data_alter(). |