You are here

public function Index::clear in Search API 8

Clears all indexed data from this index and marks it for reindexing.

Throws

\Drupal\search_api\SearchApiException Thrown if the server couldn't be loaded, for example.

Overrides IndexInterface::clear

File

src/Entity/Index.php, line 1135

Class

Index
Defines the search index configuration entity.

Namespace

Drupal\search_api\Entity

Code

public function clear() {
  if (!$this
    ->status()) {
    return;
  }

  // Only invoke the hook if we actually did something.
  $invoke_hook = FALSE;
  if (!$this
    ->isReindexing()) {
    $invoke_hook = TRUE;
    $this
      ->setHasReindexed();
    $this
      ->getTrackerInstance()
      ->trackAllItemsUpdated();
  }
  if (!$this
    ->isReadOnly()) {
    $invoke_hook = TRUE;
    $this
      ->getServerInstance()
      ->deleteAllIndexItems($this);
  }
  if ($invoke_hook) {
    $description = 'This hook is deprecated in search_api:8.x-1.14 and is removed from search_api:2.0.0. Please use the "search_api.reindex_scheduled" event instead. See https://www.drupal.org/node/3059866';
    \Drupal::moduleHandler()
      ->invokeAllDeprecated($description, 'search_api_index_reindex', [
      $this,
      !$this
        ->isReadOnly(),
    ]);

    /** @var \Symfony\Component\EventDispatcher\EventDispatcherInterface $dispatcher */
    $dispatcher = \Drupal::getContainer()
      ->get('event_dispatcher');
    $dispatcher
      ->dispatch(SearchApiEvents::REINDEX_SCHEDULED, new ReindexScheduledEvent($this, !$this
      ->isReadOnly()));
  }
}