You are here

social_search.module in Open Social 8.4

The Social search module.

File

modules/social_features/social_search/social_search.module
View 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\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-groups-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;
        }
      }
    }
    if ($form['#id'] == 'views-exposed-form-search-groups-page') {
      $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_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_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);
      }
    }
  }
}