You are here

social_search.install in Open Social 8.8

Install, update and uninstall functions for the social_search module.

File

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

/**
 * @file
 * Install, update and uninstall functions for the social_search module.
 */
use Drupal\Core\Entity\EntityStorageException;
use Drupal\search_api\Entity\Index;
use Drupal\user\Entity\Role;

/**
 * Implements hook_update_dependencies().
 */
function social_search_update_dependencies() {

  // Necessary because we delete a view with geolocation filter.
  // Needs to be done before system_update_8200 and social_group_update_8005.
  $dependencies['social_group'][8005] = [
    'social_search' => 8102,
  ];
  return $dependencies;
}

/**
 * Implements hook_install().
 *
 * Perform actions related to the installation of social_search.
 */
function social_search_install() {

  // Set some default permissions.
  _social_search_set_permissions();
}

/**
 * Function to set permissions.
 */
function _social_search_set_permissions() {
  $roles = Role::loadMultiple();

  /** @var \Drupal\user\Entity\Role $role */
  foreach ($roles as $role) {
    if ($role
      ->id() === 'administrator') {
      continue;
    }
    $permissions = _social_search_get_permissions($role
      ->id());
    user_role_grant_permissions($role
      ->id(), $permissions);
  }
}

/**
 * Return the permissions per role.
 *
 * @param string $role
 *   The role to get the permissions for.
 *
 * @return array
 *   A list of permissions.
 */
function _social_search_get_permissions($role) {

  // Anonymous.
  $permissions['anonymous'] = [
    'access content search',
  ];

  // Authenticated.
  $permissions['authenticated'] = array_merge($permissions['anonymous'], [
    'access users search',
    'access group search',
  ]);

  // Content manager.
  $permissions['contentmanager'] = array_merge($permissions['authenticated'], []);

  // Site manager.
  $permissions['sitemanager'] = array_merge($permissions['contentmanager'], []);
  if (isset($permissions[$role])) {
    return $permissions[$role];
  }
  return [];
}

/**
 * Trigger a search_api re-index.
 */
function social_search_update_8601() {
  $index = Index::load('social_content');
  if ($index
    ->status()) {
    $index
      ->clear();
    $index
      ->reindex();
  }
}

/**
 * Install Better Exposed Filter module.
 */
function social_search_update_8701() {
  $modules = [
    'better_exposed_filters',
  ];
  \Drupal::service('module_installer')
    ->install($modules);
}

/**
 * Add expertise and interests fields as fulltext to search users.
 */
function social_search_update_8901() {

  /** @var \Drupal\update_helper\Updater $updateHelper */
  $updateHelper = \Drupal::service('update_helper.updater');

  // Execute configuration update definitions with logging of success.
  $updateHelper
    ->executeUpdate('social_search', 'social_search_update_8901');

  // Output logged messages to related channel of update execution.
  return $updateHelper
    ->logger()
    ->output();
}

/**
 * Update search users with expertise and interests.
 */
function social_search_update_8902() {
  try {
    $index = Index::load('social_users');
    if ($index !== NULL && $index
      ->status()) {
      $index
        ->save();
      $index
        ->clear();
      $index
        ->reindex();
    }
  } catch (EntityStorageException $e) {
    \Drupal::logger('social_search')
      ->info($e
      ->getMessage());
  }
}

/**
 * Change weight of search index processors().
 */
function social_search_update_8903() {

  // Config factory.
  $config_factory = Drupal::configFactory();

  // Get search API index configs.
  $search_all = $config_factory
    ->getEditable('search_api.index.social_all');
  $search_content = $config_factory
    ->getEditable('search_api.index.social_content');
  $search_groups = $config_factory
    ->getEditable('search_api.index.social_groups');
  $search_users = $config_factory
    ->getEditable('search_api.index.social_users');

  // Setting that need to be changed.
  $setting = 'processor_settings.html_filter.weights.preprocess_index';

  // Set new weight for the 'HTML filter' processors.
  $search_all
    ->set($setting, -25)
    ->save();
  $search_content
    ->set($setting, -15)
    ->save();
  $search_groups
    ->set($setting, -15)
    ->save();
  $search_users
    ->set($setting, -15)
    ->save();
}

Functions

Namesort descending Description
social_search_install Implements hook_install().
social_search_update_8601 Trigger a search_api re-index.
social_search_update_8701 Install Better Exposed Filter module.
social_search_update_8901 Add expertise and interests fields as fulltext to search users.
social_search_update_8902 Update search users with expertise and interests.
social_search_update_8903 Change weight of search index processors().
social_search_update_dependencies Implements hook_update_dependencies().
_social_search_get_permissions Return the permissions per role.
_social_search_set_permissions Function to set permissions.