View source
<?php
namespace Drupal\search_api;
use Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException;
use Drupal\Component\Plugin\Exception\PluginNotFoundException;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityStorageException;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\TempStore\TempStoreException;
use Drupal\search_api\Datasource\DatasourceInterface;
use Drupal\search_api\Entity\Index;
use Drupal\search_api\Item\FieldInterface;
use Drupal\search_api\Processor\ProcessorInterface;
use Drupal\search_api\Query\QueryInterface;
use Drupal\search_api\Query\ResultSetInterface;
use Drupal\search_api\Tracker\TrackerInterface;
use Drupal\Core\TempStore\SharedTempStore;
class UnsavedIndexConfiguration implements IndexInterface, UnsavedConfigurationInterface {
protected $entity;
protected $tempStore;
protected $currentUserId;
protected $lock;
protected $entityTypeManager;
public function __construct(IndexInterface $index, SharedTempStore $temp_store, $current_user_id) {
$this->entity = $index;
$this->tempStore = $temp_store;
$this->currentUserId = $current_user_id;
}
public function getEntityTypeManager() {
return $this->entityTypeManager ?: \Drupal::entityTypeManager();
}
public function setEntityTypeManager(EntityTypeManagerInterface $entity_type_manager) {
$this->entityTypeManager = $entity_type_manager;
return $this;
}
public function setCurrentUserId($current_user_id) {
$this->currentUserId = $current_user_id;
}
public function hasChanges() {
return (bool) $this->lock;
}
public function isLocked() {
if ($this->lock) {
return $this->lock
->getOwnerId() != $this->currentUserId;
}
return FALSE;
}
public function getLockOwner() {
if (!$this->lock) {
return NULL;
}
$owner_id = $this->lock
->getOwnerId();
$uid = is_numeric($owner_id) ? $owner_id : 0;
try {
return $this
->getEntityTypeManager()
->getStorage('user')
->load($uid);
} catch (InvalidPluginDefinitionException $e) {
return NULL;
} catch (PluginNotFoundException $e) {
return NULL;
}
}
public function getLastUpdated() {
return $this->lock ? $this->lock
->getUpdated() : NULL;
}
public function setLockInformation($lock = NULL) {
$this->lock = $lock;
return $this;
}
public function savePermanent() {
$storage = $this
->getEntityTypeManager()
->getStorage('search_api_index');
$storage
->resetCache([
$this->entity
->id(),
]);
$original = $storage
->loadOverrideFree($this->entity
->id());
$fields = $this->entity
->getFields();
foreach ($fields as $field) {
$field
->setIndex($original);
}
$original
->setFields($fields);
$original
->save();
$this->entity = $original;
$this
->discardChanges();
}
public function discardChanges() {
$this->tempStore
->delete($this->entity
->id());
}
public function getDescription() {
return $this->entity
->getDescription();
}
public function isReadOnly() {
return $this->entity
->isReadOnly();
}
public function getOption($name, $default = NULL) {
return $this->entity
->getOption($name, $default);
}
public function getOptions() {
return $this->entity
->getOptions();
}
public function setOption($name, $option) {
$this->entity
->setOption($name, $option);
return $this;
}
public function setOptions(array $options) {
$this->entity
->setOptions($options);
return $this;
}
public function getDatasources() {
return $this->entity
->getDatasources();
}
public function getDatasourceIds() {
return $this->entity
->getDatasourceIds();
}
public function isValidDatasource($datasource_id) {
return $this->entity
->isValidDatasource($datasource_id);
}
public function getDatasource($datasource_id) {
return $this->entity
->getDatasource($datasource_id);
}
public function addDatasource(DatasourceInterface $datasource) {
$this->entity
->addDatasource($datasource);
return $this;
}
public function removeDatasource($datasource_id) {
$this->entity
->removeDatasource($datasource_id);
return $this;
}
public function setDatasources(array $datasources) {
$this->entity
->setDatasources($datasources);
return $this;
}
public function getEntityTypes() {
return $this->entity
->getEntityTypes();
}
public function hasValidTracker() {
return $this->entity
->hasValidTracker();
}
public function getTrackerId() {
return $this->entity
->getTrackerId();
}
public function getTrackerInstance() {
return $this->entity
->getTrackerInstance();
}
public function setTracker(TrackerInterface $tracker) {
$this->entity
->setTracker($tracker);
return $this;
}
public function hasValidServer() {
return $this->entity
->hasValidServer();
}
public function isServerEnabled() {
return $this->entity
->isServerEnabled();
}
public function getServerId() {
return $this->entity
->getServerId();
}
public function getServerInstance() {
return $this->entity
->getServerInstance();
}
public function setServer(ServerInterface $server = NULL) {
$this->entity
->setServer($server);
return $this;
}
public function getProcessors() {
return $this->entity
->getProcessors();
}
public function getProcessorsByStage($stage, array $overrides = []) {
return $this->entity
->getProcessorsByStage($stage, $overrides);
}
public function isValidProcessor($processor_id) {
return $this->entity
->isValidProcessor($processor_id);
}
public function getProcessor($processor_id) {
return $this->entity
->getProcessor($processor_id);
}
public function addProcessor(ProcessorInterface $processor) {
$this->entity
->addProcessor($processor);
return $this;
}
public function removeProcessor($processor_id) {
$this->entity
->removeProcessor($processor_id);
return $this;
}
public function setProcessors(array $processors) {
$this->entity
->setProcessors($processors);
return $this;
}
public function alterIndexedItems(array &$items) {
$this->entity
->alterIndexedItems($items);
}
public function preprocessIndexItems(array $items) {
$this->entity
->preprocessIndexItems($items);
}
public function preprocessSearchQuery(QueryInterface $query) {
$this->entity
->preprocessSearchQuery($query);
}
public function postprocessSearchResults(ResultSetInterface $results) {
$this->entity
->postprocessSearchResults($results);
}
public function addField(FieldInterface $field) {
$this->entity
->addField($field);
return $this;
}
public function renameField($old_field_id, $new_field_id) {
$this->entity
->renameField($old_field_id, $new_field_id);
return $this;
}
public function removeField($field_id) {
$this->entity
->removeField($field_id);
return $this;
}
public function setFields(array $fields) {
$this->entity
->setFields($fields);
return $this;
}
public function getFields($include_server_defined = FALSE) {
return $this->entity
->getFields($include_server_defined);
}
public function getField($field_id) {
return $this->entity
->getField($field_id);
}
public function getFieldsByDatasource($datasource_id) {
return $this->entity
->getFieldsByDatasource($datasource_id);
}
public function getFulltextFields() {
return $this->entity
->getFulltextFields();
}
public function getFieldRenames() {
return $this->entity
->getFieldRenames();
}
public function discardFieldChanges() {
$this->entity
->discardFieldChanges();
return $this;
}
public function getPropertyDefinitions($datasource_id) {
return $this->entity
->getPropertyDefinitions($datasource_id);
}
public function loadItem($item_id) {
return $this->entity
->loadItem($item_id);
}
public function loadItemsMultiple(array $item_ids) {
return $this->entity
->loadItemsMultiple($item_ids);
}
public function indexItems($limit = -1, $datasource_id = NULL) {
return $this->entity
->indexItems($limit, $datasource_id);
}
public function indexSpecificItems(array $search_objects) {
return $this->entity
->indexSpecificItems($search_objects);
}
public function isBatchTracking() {
return $this->entity
->isBatchTracking();
}
public function startBatchTracking() {
$this->entity
->startBatchTracking();
return $this;
}
public function stopBatchTracking() {
$this->entity
->stopBatchTracking();
return $this;
}
public function trackItemsInserted($datasource_id, array $ids) {
$this->entity
->trackItemsInserted($datasource_id, $ids);
}
public function trackItemsUpdated($datasource_id, array $ids) {
$this->entity
->trackItemsUpdated($datasource_id, $ids);
}
public function trackItemsDeleted($datasource_id, array $ids) {
$this->entity
->trackItemsDeleted($datasource_id, $ids);
}
public function reindex() {
$this->entity
->reindex();
}
public function clear() {
$this->entity
->clear();
}
public function rebuildTracker() {
$this->entity
->rebuildTracker();
}
public function isReindexing() {
return $this->entity
->isReindexing();
}
public function query(array $options = []) {
return $this->entity
->query($options);
}
public function enable() {
$this->entity
->enable();
return $this;
}
public function disable() {
$this->entity
->disable();
return $this;
}
public function setStatus($status) {
$this->entity
->setStatus($status);
return $this;
}
public function setSyncing($status) {
$this->entity
->setSyncing($status);
return $this;
}
public function status() {
return $this->entity
->status();
}
public function isSyncing() {
return $this->entity
->isSyncing();
}
public function isUninstalling() {
return $this->entity
->isUninstalling();
}
public function get($property_name) {
return $this->entity
->get($property_name);
}
public function set($property_name, $value) {
$this->entity
->set($property_name, $value);
return $this;
}
public function calculateDependencies() {
$this->entity
->calculateDependencies();
return $this;
}
public function onDependencyRemoval(array $dependencies) {
return $this->entity
->onDependencyRemoval($dependencies);
}
public function getDependencies() {
return $this->entity
->getDependencies();
}
public function isInstallable() {
return $this->entity
->isInstallable();
}
public function trustData() {
$this->entity
->trustData();
return $this;
}
public function hasTrustedData() {
return $this->entity
->hasTrustedData();
}
public function uuid() {
return $this->entity
->uuid();
}
public function id() {
return $this->entity
->id();
}
public function language() {
return $this->entity
->language();
}
public function isNew() {
return $this->entity
->isNew();
}
public function enforceIsNew($value = TRUE) {
$this->entity
->enforceIsNew($value);
return $this;
}
public function getEntityTypeId() {
return $this->entity
->getEntityTypeId();
}
public function bundle() {
return $this->entity
->bundle();
}
public function label() {
return $this->entity
->label();
}
public function urlInfo($rel = 'canonical', array $options = []) {
return $this->entity
->toUrl($rel, $options);
}
public function toUrl($rel = 'canonical', array $options = []) {
return $this->entity
->toUrl($rel, $options);
}
public function url($rel = 'canonical', $options = []) {
return $this->entity
->toUrl($rel, $options)
->toString();
}
public function link($text = NULL, $rel = 'canonical', array $options = []) {
return $this->entity
->toLink($text, $rel, $options)
->toString();
}
public function toLink($text = NULL, $rel = 'canonical', array $options = []) {
return $this->entity
->toLink($text, $rel, $options);
}
public function hasLinkTemplate($key) {
return $this->entity
->hasLinkTemplate($key);
}
public function uriRelationships() {
return $this->entity
->uriRelationships();
}
public static function load($id) {
return Index::load($id);
}
public static function loadMultiple(array $ids = NULL) {
return Index::loadMultiple($ids);
}
public static function create(array $values = []) {
return Index::create($values);
}
public function save() {
try {
if ($this->tempStore
->setIfOwner($this->entity
->id(), $this->entity)) {
return SAVED_UPDATED;
}
} catch (TempStoreException $e) {
throw new EntityStorageException('Could not save temporary index configuration: ' . $e
->getMessage(), $e
->getCode(), $e);
}
throw new EntityStorageException('Cannot save temporary index configuration: currently being edited by someone else.');
}
public function delete() {
$this->entity
->delete();
}
public function preSave(EntityStorageInterface $storage) {
$this->entity
->preSave($storage);
}
public function postSave(EntityStorageInterface $storage, $update = TRUE) {
$this->entity
->postSave($storage, $update);
}
public static function preCreate(EntityStorageInterface $storage, array &$values) {
EntityInterface::preCreate($storage, $values);
}
public function postCreate(EntityStorageInterface $storage) {
$this->entity
->postCreate($storage);
}
public static function preDelete(EntityStorageInterface $storage, array $entities) {
EntityInterface::preDelete($storage, $entities);
}
public static function postDelete(EntityStorageInterface $storage, array $entities) {
EntityInterface::postDelete($storage, $entities);
}
public static function postLoad(EntityStorageInterface $storage, array &$entities) {
EntityInterface::postLoad($storage, $entities);
}
public function createDuplicate() {
return new UnsavedIndexConfiguration($this->entity
->createDuplicate(), $this->tempStore, $this->currentUserId);
}
public function getEntityType() {
return $this->entity
->getEntityType();
}
public function referencedEntities() {
return $this->entity
->referencedEntities();
}
public function getOriginalId() {
return $this->entity
->getOriginalId();
}
public function getCacheTagsToInvalidate() {
return $this->entity
->getCacheTagsToInvalidate();
}
public function setOriginalId($id) {
$this->entity
->setOriginalId($id);
return $this;
}
public function toArray() {
return $this->entity
->toArray();
}
public function getTypedData() {
return $this->entity
->getTypedData();
}
public function getConfigDependencyKey() {
return $this->entity
->getConfigDependencyKey();
}
public function getConfigDependencyName() {
return $this->entity
->getConfigDependencyName();
}
public function getConfigTarget() {
return $this->entity
->getConfigTarget();
}
public function access($operation, AccountInterface $account = NULL, $return_as_object = FALSE) {
return $this->entity
->access($operation, $account, $return_as_object);
}
public function getCacheContexts() {
return $this->entity
->getCacheContexts();
}
public function getCacheTags() {
return $this->entity
->getCacheTags();
}
public function getCacheMaxAge() {
return $this->entity
->getCacheMaxAge();
}
public function addCacheContexts(array $cache_contexts) {
$this->entity
->addCacheContexts($cache_contexts);
return $this;
}
public function addCacheTags(array $cache_tags) {
$this->entity
->addCacheTags($cache_tags);
return $this;
}
public function mergeCacheMaxAge($max_age) {
$this->entity
->mergeCacheMaxAge($max_age);
return $this;
}
public function addCacheableDependency($other_object) {
$this->entity
->addCacheableDependency($other_object);
return $this;
}
public function setThirdPartySetting($module, $key, $value) {
$this->entity
->setThirdPartySetting($module, $key, $value);
return $this;
}
public function getThirdPartySetting($module, $key, $default = NULL) {
return $this->entity
->getThirdPartySetting($module, $key, $default);
}
public function getThirdPartySettings($module) {
return $this->entity
->getThirdPartySettings($module);
}
public function unsetThirdPartySetting($module, $key) {
return $this->entity
->unsetThirdPartySetting($module, $key);
}
public function getThirdPartyProviders() {
return $this->entity
->getThirdPartyProviders();
}
}