You are here

public function DataTypeHelper::getDataTypeFallbackMapping in Search API 8

Retrieves the necessary type fallbacks for an index.

Parameters

\Drupal\search_api\IndexInterface $index: The index for which to return the type fallbacks.

Return value

string[] An array containing the IDs of all custom data types that are not supported by the index's current server, mapped to their fallback types.

Overrides DataTypeHelperInterface::getDataTypeFallbackMapping

File

src/Utility/DataTypeHelper.php, line 157

Class

DataTypeHelper
Provides helper methods for dealing with Search API data types.

Namespace

Drupal\search_api\Utility

Code

public function getDataTypeFallbackMapping(IndexInterface $index) {

  // Check the cache first.
  $indexId = $index
    ->id();
  if (empty($this->dataTypeFallbackMapping[$indexId])) {
    $server = NULL;
    try {
      $server = $index
        ->getServerInstance();
    } catch (SearchApiException $e) {

      // If the server isn't available, just ignore it here and return all
      // custom types.
    }
    $this->dataTypeFallbackMapping[$indexId] = [];
    $dataTypes = $this->dataTypeManager
      ->getInstances();
    foreach ($dataTypes as $typeId => $dataType) {

      // We know for sure that we do not need to fall back for the default
      // data types as they are always present and are required to be
      // supported by all backends.
      if (!$dataType
        ->isDefault() && (!$server || !$server
        ->supportsDataType($typeId))) {
        $this->dataTypeFallbackMapping[$indexId][$typeId] = $dataType
          ->getFallbackType();
      }
    }
  }
  return $this->dataTypeFallbackMapping[$indexId];
}