You are here

public function SearchApiConverter::convert in Search API 8

Same name in this branch
  1. 8 src/ParamConverter/SearchApiConverter.php \Drupal\search_api\ParamConverter\SearchApiConverter::convert()
  2. 8 src/ProxyClass/ParamConverter/SearchApiConverter.php \Drupal\search_api\ProxyClass\ParamConverter\SearchApiConverter::convert()

Converts path variables to their corresponding objects.

Parameters

mixed $value: The raw value.

mixed $definition: The parameter definition provided in the route options.

string $name: The name of the parameter.

array $defaults: The route defaults array.

Return value

mixed|null The converted parameter value.

Overrides EntityConverter::convert

File

src/ParamConverter/SearchApiConverter.php, line 87

Class

SearchApiConverter
Converts search indexes from path parameters to a temporary copy.

Namespace

Drupal\search_api\ParamConverter

Code

public function convert($value, $definition, $name, array $defaults) {

  /** @var \Drupal\search_api\IndexInterface $entity */
  try {
    $storage = $this->entityTypeManager
      ->getStorage('search_api_index');
  } catch (InvalidPluginDefinitionException $e) {
    return NULL;
  } catch (PluginNotFoundException $e) {
    return NULL;
  }
  if (!$storage instanceof ConfigEntityStorageInterface) {
    return NULL;
  }
  if (!($entity = $storage
    ->loadOverrideFree($value))) {
    return NULL;
  }

  // Get the temp store for this variable if it needs one. Attempt to load the
  // index from the temp store, update the currently logged-in user's ID and
  // store the lock metadata.
  $store = $this->tempStoreFactory
    ->get('search_api_index');
  $current_user_id = $this->currentUser
    ->id() ?: session_id();

  /** @var \Drupal\search_api\IndexInterface|\Drupal\search_api\UnsavedIndexConfiguration $index */
  $index = $store
    ->get($value);
  if ($index) {
    $index = new UnsavedIndexConfiguration($index, $store, $current_user_id);
    $index
      ->setLockInformation($store
      ->getMetadata($value));
    $index
      ->setEntityTypeManager($this->entityTypeManager);
  }
  else {
    $index = new UnsavedIndexConfiguration($entity, $store, $current_user_id);
  }
  return $index;
}