You are here

class SearchApiConverter in Search API 8

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

Converts search indexes from path parameters to a temporary copy.

This is done so that certain pages (like the "Fields" tab) can modify indexes over several page requests without permanently saving the index in between.

The code for this is largely taken from the views_ui module.

Hierarchy

Expanded class hierarchy of SearchApiConverter

1 string reference to 'SearchApiConverter'
search_api.services.yml in ./search_api.services.yml
search_api.services.yml
1 service uses SearchApiConverter
paramconverter.search_api in ./search_api.services.yml
Drupal\search_api\ParamConverter\SearchApiConverter

File

src/ParamConverter/SearchApiConverter.php, line 25

Namespace

Drupal\search_api\ParamConverter
View source
class SearchApiConverter extends EntityConverter implements ParamConverterInterface {

  /**
   * The shared temporary storage factory.
   *
   * @var \Drupal\Core\TempStore\SharedTempStoreFactory
   */
  protected $tempStoreFactory;

  /**
   * The currently logged-in user.
   *
   * @var \Drupal\Core\Session\AccountInterface
   */
  protected $currentUser;

  // phpcs:disable Drupal.Commenting.FunctionComment.TypeHintMissing

  /**
   * Constructs a new SearchApiConverter.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
   *   The entity repository.
   * @param \Drupal\Core\TempStore\SharedTempStoreFactory $temp_store_factory
   *   The factory for the temp store object.
   * @param \Drupal\Core\Session\AccountInterface $user
   *   The current user.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager, $entity_repository, $temp_store_factory, $user = NULL) {

    // For backwards-compatibility, we still support passing just
    // ($entity_manager, $temp_store_factory, $user).
    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;
  }

  // phpcs:enable Drupal.Commenting.FunctionComment.TypeHintMissing

  /**
   * {@inheritdoc}
   */
  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;
  }

  /**
   * {@inheritdoc}
   */
  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;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DeprecatedServicePropertyTrait::__get public function Allows to access deprecated/removed properties.
DynamicEntityTypeParamConverterTrait::getEntityTypeFromDefaults protected function Determines the entity type ID given a route definition and route defaults.
EntityConverter::$deprecatedProperties protected property
EntityConverter::$entityRepository protected property Entity repository.
EntityConverter::$entityTypeManager protected property Entity type manager which performs the upcasting in the end.
EntityConverter::getLatestTranslationAffectedRevision Deprecated protected function Returns the latest revision translation of the specified entity.
EntityConverter::languageManager protected function Returns a language manager instance.
EntityConverter::loadRevision Deprecated protected function Loads the specified entity revision.
SearchApiConverter::$currentUser protected property The currently logged-in user.
SearchApiConverter::$tempStoreFactory protected property The shared temporary storage factory.
SearchApiConverter::applies public function Determines if the converter applies to a specific route and variable. Overrides EntityConverter::applies
SearchApiConverter::convert public function Converts path variables to their corresponding objects. Overrides EntityConverter::convert
SearchApiConverter::__construct public function Constructs a new SearchApiConverter. Overrides EntityConverter::__construct