You are here

function _social_tagging_index_toggle in Open Social 8.7

Same name and namespace in other branches
  1. 8.4 modules/social_features/social_tagging/social_tagging.install \_social_tagging_index_toggle()
  2. 8.5 modules/social_features/social_tagging/social_tagging.install \_social_tagging_index_toggle()
  3. 8.6 modules/social_features/social_tagging/social_tagging.install \_social_tagging_index_toggle()

Turn an index off and on.

Parameters

string $index_id: The index that needs to be disabled and enabled.

Throws

\Drupal\Core\Entity\EntityStorageException

2 calls to _social_tagging_index_toggle()
social_tagging_install in modules/social_features/social_tagging/social_tagging.install
Install the module.
social_tagging_update_8002 in modules/social_features/social_tagging/social_tagging.install
Toggle group index.

File

modules/social_features/social_tagging/social_tagging.install, line 89
Installation file for Social Tagging.

Code

function _social_tagging_index_toggle($index_id) {

  /* @var Drupal\search_api\Entity\Index $index */
  $index = Index::load($index_id);
  if (!$index instanceof Index) {
    \Drupal::logger('social_tagging')
      ->info('Invalid search index');
    return;
  }
  \Drupal::logger('social_tagging')
    ->info('Loaded search index');

  // If currently enabled we will first disabled and enable the index.
  if ($index !== NULL && $index
    ->status()) {
    \Drupal::logger('social_tagging')
      ->info('Search index exists');

    // Elevate permissions so we can index *all* the items.
    $accountSwitcher = Drupal::service('account_switcher');
    $account = User::load(1);
    $accountSwitcher
      ->switchTo($account);

    // Disable and enable the index so the tagging field is properly added.
    $index
      ->disable()
      ->save();
    \Drupal::logger('social_tagging')
      ->info('Search index disabled');
    $index
      ->enable()
      ->save();
    \Drupal::logger('social_tagging')
      ->info('Search index enabled');

    // Restore user account.
    $accountSwitcher
      ->switchBack();
  }
}