You are here

public function CommandHelper::setIndexState in Search API 8

Changes the state of a single index.

Parameters

\Drupal\search_api\IndexInterface $index: The index to be enabled.

bool $enable: (optional) TRUE to enable, FALSE to disable the index.

2 calls to CommandHelper::setIndexState()
CommandHelper::disableIndexCommand in src/Utility/CommandHelper.php
Disables one or more enabled search indexes.
CommandHelper::enableIndexCommand in src/Utility/CommandHelper.php
Enables one or more disabled search indexes.

File

src/Utility/CommandHelper.php, line 697

Class

CommandHelper
Provides functionality to be used by CLI tools.

Namespace

Drupal\search_api\Utility

Code

public function setIndexState(IndexInterface $index, $enable = TRUE) {
  $state_label = $enable ? $this
    ->t('enabled') : $this
    ->t('disabled');
  $method = $enable ? 'enable' : 'disable';
  if ($index
    ->status() == $enable) {
    $this->logger
      ->info($this
      ->t("The index @index is already @desired_state.", [
      '@index' => $index
        ->label(),
      '@desired_state' => $state_label,
    ]));
    return;
  }
  if (!$index
    ->getServerId()) {
    $this->logger
      ->warning($this
      ->t("Index @index could not be @desired_state because it is not bound to any server.", [
      '@index' => $index
        ->label(),
      '@desired_state' => $state_label,
    ]));
    return;
  }
  $index = $this
    ->reloadEntityOverrideFree($index);
  $index
    ->{$method}()
    ->save();
  $this->logger
    ->info($this
    ->t("The index @index was successfully @desired_state.", [
    '@index' => $index
      ->label(),
    '@desired_state' => $state_label,
  ]));
}