You are here

protected function ContentEntityTrackingManager::filterValidItemIds in Search API 8

Filters a set of datasource-specific item IDs.

Returns only those item IDs that are valid for the given datasource and index. This method only checks the item language, though – whether an entity with that ID actually exists, or whether it has a bundle included for that datasource, is not verified.

Parameters

\Drupal\search_api\IndexInterface $index: The index for which to validate.

string $datasource_id: The ID of the datasource on the index for which to validate.

string[] $item_ids: The item IDs to be validated.

Return value

string[] All given item IDs that are valid for that index and datasource.

1 call to ContentEntityTrackingManager::filterValidItemIds()
ContentEntityTrackingManager::trackEntityChange in src/Plugin/search_api/datasource/ContentEntityTrackingManager.php
Queues an entity for indexing.

File

src/Plugin/search_api/datasource/ContentEntityTrackingManager.php, line 387

Class

ContentEntityTrackingManager
Provides hook implementations on behalf of the Content Entity datasource.

Namespace

Drupal\search_api\Plugin\search_api\datasource

Code

protected function filterValidItemIds(IndexInterface $index, string $datasource_id, array $item_ids) : array {
  if (!$index
    ->isValidDatasource($datasource_id)) {
    return $item_ids;
  }
  try {
    $config = $index
      ->getDatasource($datasource_id)
      ->getConfiguration();
  } catch (SearchApiException $e) {

    // Can't really happen, but play it safe to appease static code analysis.
    return $item_ids;
  }

  // If the entity type doesn't allow translations, we just accept all IDs.
  // (If the entity type were translatable, the config key would have been set
  // with the default configuration.)
  if (!isset($config['languages']['selected'])) {
    return $item_ids;
  }
  $always_valid = [
    LanguageInterface::LANGCODE_NOT_SPECIFIED,
    LanguageInterface::LANGCODE_NOT_APPLICABLE,
  ];
  $valid_ids = [];
  foreach ($item_ids as $item_id) {
    $pos = strrpos($item_id, ':');

    // Item IDs without colons are always invalid.
    if ($pos === FALSE) {
      continue;
    }
    $langcode = substr($item_id, $pos + 1);
    if (Utility::matches($langcode, $config['languages']) || in_array($langcode, $always_valid)) {
      $valid_ids[] = $item_id;
    }
  }
  return $valid_ids;
}