You are here

social_search.install in Open Social 8.5

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\block\Entity\Block;
use Drupal\Core\Site\Settings;
use Drupal\views\Entity\View;
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 [];
}

/**
 * Install new submodule from search_api, to fix our exposed filters.
 *
 * See https://www.drupal.org/node/2777483.
 */
function social_search_update_8101(array &$sandbox) {
  $modules = [
    'search_api_views_taxonomy',
  ];
  \Drupal::service('module_installer')
    ->install($modules);
}

/**
 * Remove deprecated bloks and views from Social Search module.
 */
function social_search_update_8102(array &$sandbox) {

  // Remove exposed filter blocks from Social Search Views.
  $block_ids = [
    'search_content_block_title',
    'search_users_block_title',
    'searchgroupsblock',
  ];
  foreach ($block_ids as $block_id) {
    $block = Block::load($block_id);
    if ($block instanceof Block) {
      $block
        ->delete();
    }
  }

  // Delete Search Groups (proximity) Views.
  $view = View::load('search_groups_proximity');
  if ($view) {
    $view
      ->delete();
  }
}

/**
 * Trigger a search_api re-index.
 */
function social_search_update_8103() {
  $indexes = [
    'social_all',
    'social_groups',
  ];
  foreach ($indexes as $index_id) {
    $index = Index::load($index_id);
    if ($index
      ->status()) {
      $index
        ->clear();
      $index
        ->reindex();
    }
  }
}

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

/**
 * Trigger a search_api re-index for indexes 'all' and 'users'.
 */
function social_search_update_8105() {
  $indexes = [
    'social_all',
    'social_users',
  ];
  foreach ($indexes as $index_id) {
    $index = Index::load($index_id);
    if ($index
      ->status()) {
      $index
        ->clear();
      $index
        ->reindex();
    }
  }
}

/**
 * Revert the Social Search feature so the 'Skip user 1' processor is used.
 */
function social_search_update_8106() {

  // Import the Social Search feature.
  \Drupal::service('features.manager')
    ->import([
    'social_search',
  ]);

  /** @var \Drupal\search_api\Entity\Index $index */
  $index = Index::load('social_users');

  // Reindex the user index to ensure for the 'Skip user 1' is also taken care
  // of for updates.
  if ($index !== NULL && $index
    ->status()) {
    $index
      ->clear();
    $index
      ->reindex();
  }
}

/**
 * Set all existing profiles as default to fix search indexing.
 */
function social_search_update_8107(&$sandbox) {
  if (!isset($sandbox['progress'])) {

    // This must be the first run. Initialize the sandbox.
    $sandbox['progress'] = 0;
    $sandbox['profiles_updated'] = 0;

    // We check for any profiles that do not have is_default set.
    $sandbox['pids'] = array_values(\Drupal::entityQuery('profile')
      ->notExists('is_default')
      ->execute());
    $sandbox['profile_count'] = count($sandbox['pids']);
    \Drupal::logger('sdgc_search')
      ->info('Checking profile status for @count profiles', [
      '@count' => $sandbox['profile_count'],
    ]);
  }
  $batch_size = Settings::get('entity_update_batch_size', 50);

  // Try to do 50 each cycle. Never do more than are available.
  for ($target = $sandbox['progress'] + $batch_size; $sandbox['progress'] < $target && $sandbox['progress'] < $sandbox['profile_count']; $sandbox['progress']++) {
    $pid = $sandbox['pids'][$sandbox['progress']];
    $profileStorage = \Drupal::entityTypeManager()
      ->getStorage('profile');

    // Check if the user has a profile already.

    /** @var \Drupal\profile\Entity\Profile $profile */
    $profile = $profileStorage
      ->load($pid);
    if ($profile) {
      try {
        $profile
          ->setDefault(TRUE);
        $profile
          ->save();
        $sandbox['profiles_updated']++;
      } catch (Exception $e) {
        \Drupal::logger('sdgc_search')
          ->error('Could not update profile for @profile_id', [
          '@profile_id' => $pid,
        ]);
      }
    }
  }
  $sandbox['#finished'] = empty($sandbox['profile_count']) ? 1 : $sandbox['progress'] / $sandbox['profile_count'];

  // We ran through all of them.
  if ($sandbox['#finished'] === 1) {
    \Drupal::logger('social_profile')
      ->info('Updated profiles for @count profiles', [
      '@count' => $sandbox['profiles_updated'],
    ]);

    // If any profiles were updated we might also need to disable and enable the
    // user search index.
    if ($sandbox['profiles_updated']) {

      /** @var \Drupal\search_api\Entity\Index $index */
      $index = Index::load('social_users');

      // If currently enabled we will first disabled and enable the index.
      if ($index !== NULL && $index
        ->status()) {
        $index
          ->disable()
          ->save();
        $index
          ->enable()
          ->save();
        \Drupal::logger('social_profile')
          ->info('Disabled and enabled the user search index');

        // Clear and reindex.
        $index
          ->clear();
        $index
          ->reindex();
        \Drupal::logger('social_profile')
          ->info('Reindexed the user search index');
      }
    }
  }
}

/**
 * Uninstall depricated submodule from search_api.
 *
 * See https://www.drupal.org/project/search_api/issues/2917399
 */
function social_search_update_8108(array &$sandbox) {
  $modules = [
    'search_api_views_taxonomy',
  ];
  \Drupal::service('module_installer')
    ->uninstall($modules);
}

/**
 * Update social_all and social_users indices.
 */
function social_search_update_8109() {

  // First revert the feature.
  \Drupal::service('features.manager')
    ->import([
    'social_search',
  ]);
  $indexes = [
    'social_all',
    'social_users',
  ];
  foreach ($indexes as $index_id) {
    $index = Index::load($index_id);
    if ($index
      ->status()) {
      $index
        ->clear();
      $index
        ->reindex();
    }
  }
}

/**
 * Trigger a search_api re-index.
 */
function social_search_update_8110() {
  $indexes = [
    'social_all',
    'social_users',
  ];
  foreach ($indexes as $index_id) {
    $index = Index::load($index_id);
    if ($index
      ->status()) {
      $index
        ->clear();
      $index
        ->reindex();
    }
  }
}

Functions

Namesort descending Description
social_search_install Implements hook_install().
social_search_update_8101 Install new submodule from search_api, to fix our exposed filters.
social_search_update_8102 Remove deprecated bloks and views from Social Search module.
social_search_update_8103 Trigger a search_api re-index.
social_search_update_8104 Trigger a search_api re-index for social_all.
social_search_update_8105 Trigger a search_api re-index for indexes 'all' and 'users'.
social_search_update_8106 Revert the Social Search feature so the 'Skip user 1' processor is used.
social_search_update_8107 Set all existing profiles as default to fix search indexing.
social_search_update_8108 Uninstall depricated submodule from search_api.
social_search_update_8109 Update social_all and social_users indices.
social_search_update_8110 Trigger a search_api re-index.
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.