You are here

class SearchApiMigrateSubscriber in Multiversion 8

SearchApiMigrateSubscriber class.

Makes the Search API indexes read only before the multiversion migration if they were writable and reverts back the changes after the migration since we don't need to reindex each entity during the migration process.

Hierarchy

  • class \Drupal\multiversion\EventSubscriber\SearchApiMigrateSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of SearchApiMigrateSubscriber

1 string reference to 'SearchApiMigrateSubscriber'
multiversion.services.yml in ./multiversion.services.yml
multiversion.services.yml
1 service uses SearchApiMigrateSubscriber
multiversion.search_api_migrate_subscriber in ./multiversion.services.yml
Drupal\multiversion\EventSubscriber\SearchApiMigrateSubscriber

File

src/EventSubscriber/SearchApiMigrateSubscriber.php, line 17

Namespace

Drupal\multiversion\EventSubscriber
View source
class SearchApiMigrateSubscriber implements EventSubscriberInterface {

  /**
   * An array of Search API Index entities.
   *
   * @var array $indexes
   */
  protected $indexes = [];

  /**
   * Constructs a new SearchApiMigrateSubscriber instance.
   *
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   The module handler.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   *   The invalid plugin definition exception.
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   *   The plugin not found exception.
   */
  public function __construct(ModuleHandlerInterface $module_handler, EntityTypeManagerInterface $entity_type_manager) {
    if ($module_handler
      ->moduleExists('search_api')) {
      $indexes = $entity_type_manager
        ->getStorage('search_api_index')
        ->loadMultiple();
      foreach ($indexes as $index_id => $index) {

        // We are interested only in enabled and writable indexes.
        if ($index
          ->status() && !$index
          ->isReadOnly()) {
          $this->indexes[$index_id] = $index;
        }
      }
    }
  }

  /**
   * Disable Search API node indexing.
   */
  public function onPreMigrate() {
    $this
      ->indexSetReadOnly(TRUE);
  }

  /**
   * Enable Search API node indexing.
   *
   * Note that this will not be triggered in case if the migration process will
   * fail with an exception.
   */
  public function onPostMigrate() {
    $this
      ->indexSetReadOnly(FALSE);
  }

  /**
   * Set the index read only property state.
   *
   * @param bool $state
   *  The read only state.
   */
  private function indexSetReadOnly($state) {
    foreach ($this->indexes as $index_key => $index) {
      $index
        ->set('read_only', $state);
      $index
        ->save();
    }
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    return [
      MultiversionManagerEvents::PRE_MIGRATE => [
        'onPreMigrate',
      ],
      MultiversionManagerEvents::POST_MIGRATE => [
        'onPostMigrate',
      ],
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
SearchApiMigrateSubscriber::$indexes protected property An array of Search API Index entities.
SearchApiMigrateSubscriber::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
SearchApiMigrateSubscriber::indexSetReadOnly private function Set the index read only property state.
SearchApiMigrateSubscriber::onPostMigrate public function Enable Search API node indexing.
SearchApiMigrateSubscriber::onPreMigrate public function Disable Search API node indexing.
SearchApiMigrateSubscriber::__construct public function Constructs a new SearchApiMigrateSubscriber instance.