You are here

public function UnsavedIndexConfiguration::savePermanent in Search API 8

Saves the changes represented by this object permanently.

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException Thrown if the "search_api_index" storage handler couldn't be loaded.

\Drupal\Component\Plugin\Exception\PluginNotFoundException Thrown if the "search_api_index" entity type doesn't exist.

\Drupal\Core\Entity\EntityStorageException Thrown if saving the index failed.

\Drupal\Core\TempStore\TempStoreException Thrown when a lock for the temp storage could not be acquired.

Overrides UnsavedConfigurationInterface::savePermanent

File

src/UnsavedIndexConfiguration.php, line 164

Class

UnsavedIndexConfiguration
Represents a configuration of an index that was not yet permanently saved.

Namespace

Drupal\search_api

Code

public function savePermanent() {

  // Make sure to overwrite only the index's fields, not just all properties.
  // Unlike the Views UI, we have several separate pages for editing index
  // entities, and only one of them is locked. Therefore, this extra step is
  // necessary, we can't just call $this->entity->save().

  /** @var \Drupal\search_api\Entity\SearchApiConfigEntityStorage $storage */
  $storage = $this
    ->getEntityTypeManager()
    ->getStorage('search_api_index');
  $storage
    ->resetCache([
    $this->entity
      ->id(),
  ]);

  /** @var \Drupal\search_api\IndexInterface $original */
  $original = $storage
    ->loadOverrideFree($this->entity
    ->id());
  $fields = $this->entity
    ->getFields();

  // Set the correct index object on the field objects.
  foreach ($fields as $field) {
    $field
      ->setIndex($original);
  }
  $original
    ->setFields($fields);
  $original
    ->save();

  // Setting the saved entity as the wrapped one is important if methods like
  // isReindexing() are called on the object afterwards.
  $this->entity = $original;
  $this
    ->discardChanges();
}