social_search.module in Open Social 8.3
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.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
- 10.2.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\profile\Entity\Profile;
use Drupal\search_api\Entity\Index;
use Drupal\search_api\Plugin\search_api\datasource\ContentEntity;
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-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');
}
/**
* Implements hook_ENTITY_TYPE_update().
*
* Update the profile index when user is updated or delete entry when blocked.
*/
function social_search_user_update(EntityInterface $entity) {
/** @var \Drupal\user\Entity\User $entity */
_social_search_retrigger_profile($entity);
}
/**
* Retriggers the profile in the search when a user has ben changed.
*
* @param \Drupal\user\Entity\User $user
* The user whose profile we want to retrigger.
*/
function _social_search_retrigger_profile(User $user) {
// These indices need to be updated.
$indexes = [
'social_all',
'social_users',
];
try {
/** @var \Drupal\profile\ProfileStorage $profile_storage */
$profile_storage = \Drupal::entityTypeManager()
->getStorage('profile');
} catch (InvalidPluginDefinitionException $e) {
return;
} catch (PluginNotFoundException $e) {
return;
}
/** @var \Drupal\profile\Entity\Profile $profile */
$profile = $profile_storage
->loadByUser($user, 'profile');
// Must be a profile, otherwise we can leave.
if (!$profile instanceof Profile) {
return;
}
// Loop over the indexes that need to be updated.
foreach ($indexes as $index_id) {
$index = Index::load($index_id);
if ($index instanceof Index) {
// Init.
$profile_ids = [];
/** @var \Drupal\user\Entity\User $original_user */
$original_user = $user->original;
foreach ($profile
->getTranslationLanguages() as $langcode => $language) {
$profile_ids[] = $profile
->id() . ':' . $langcode;
}
$datasource_id = 'entity:profile';
if ($user
->isBlocked() && $original_user
->isActive()) {
$index
->trackItemsDeleted($datasource_id, $profile_ids);
}
elseif ($user
->isActive() && $original_user
->isBlocked()) {
$filtered_item_ids = ContentEntity::filterValidItemIds($index, $datasource_id, $profile_ids);
$index
->trackItemsInserted($datasource_id, $filtered_item_ids);
}
else {
$index
->trackItemsUpdated($datasource_id, $profile_ids);
}
}
}
}
Functions
Name | Description |
---|---|
social_block_view_search_hero_block_alter | Implements hook_block_view_BASE_BLOCK_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_user_update | Implements hook_ENTITY_TYPE_update(). |
_social_search_retrigger_profile | Retriggers the profile in the search when a user has ben changed. |