You are here

public function SearchApiSortsManager::cleanupSortFields in Search API sorts 8

Remove all deleted sorts for the given index.

Parameters

\Drupal\search_api\IndexInterface $index: The search api index.

Overrides SearchApiSortsManagerInterface::cleanupSortFields

File

src/SearchApiSortsManager.php, line 127

Class

SearchApiSortsManager
Manages search api sorts.

Namespace

Drupal\search_api_sorts

Code

public function cleanupSortFields(IndexInterface $index) {
  foreach ($this->searchApiDisplayManager
    ->getInstances() as $display) {
    if ($display
      ->getIndex() instanceof IndexInterface && $index
      ->id() === $display
      ->getIndex()
      ->id()) {
      foreach ($this
        ->getSorts($display) as $search_api_sorts_field) {

        // Dummy field therefore the index has no field.
        if ($search_api_sorts_field
          ->getFieldIdentifier() === 'search_api_relevance') {
          continue;
        }
        $field = $index
          ->getField($search_api_sorts_field
          ->getFieldIdentifier());
        if ($field === NULL) {
          $search_api_sorts_field
            ->delete();
        }
      }
    }
  }
}