View source
<?php
namespace Drupal\search_api\ParamConverter;
use Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException;
use Drupal\Component\Plugin\Exception\PluginNotFoundException;
use Drupal\Core\Config\Entity\ConfigEntityStorageInterface;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\ParamConverter\EntityConverter;
use Drupal\Core\ParamConverter\ParamConverterInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\TempStore\SharedTempStoreFactory;
use Drupal\search_api\UnsavedIndexConfiguration;
use Symfony\Component\Routing\Route;
class SearchApiConverter extends EntityConverter implements ParamConverterInterface {
protected $tempStoreFactory;
protected $currentUser;
public function __construct(EntityTypeManagerInterface $entity_type_manager, $entity_repository, $temp_store_factory, $user = NULL) {
if (!$user) {
@trigger_error('Constructing \\Drupal\\search_api\\ParamConverter\\SearchApiConverter with ($entity_manager, $temp_store_factory, $user) is deprecated in search_api 8.x-1.18 and will stop working in 2.0.0. Pass ($entity_type_manager, $entity_repository, $temp_store_factory, $user) instead. See https://www.drupal.org/node/3164248', E_USER_DEPRECATED);
$user = $temp_store_factory;
$temp_store_factory = $entity_repository;
$entity_repository = \Drupal::getContainer()
->get('entity.repository');
}
$type_checks = [
2 => [
$entity_repository,
EntityRepositoryInterface::class,
],
3 => [
$temp_store_factory,
SharedTempStoreFactory::class,
],
4 => [
$user,
AccountInterface::class,
],
];
foreach ($type_checks as $i => list($object, $expected)) {
if (!$object instanceof $expected) {
$actual = get_class($object);
throw new \TypeError("Argument {$i} passed to Drupal\\search_api\\ParamConverter\\SearchApiConverter::__construct() must implement interface {$expected}, instance of {$actual} given");
}
}
parent::__construct($entity_type_manager, $entity_repository);
$this->tempStoreFactory = $temp_store_factory;
$this->currentUser = $user;
}
public function convert($value, $definition, $name, array $defaults) {
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;
}
$store = $this->tempStoreFactory
->get('search_api_index');
$current_user_id = $this->currentUser
->id() ?: session_id();
$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;
}
public function applies($definition, $name, Route $route) {
if (parent::applies($definition, $name, $route)) {
return !empty($definition['tempstore']) && $definition['type'] === 'entity:search_api_index';
}
return FALSE;
}
}