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'
1 service uses SearchApiMigrateSubscriber
File
- src/
EventSubscriber/ SearchApiMigrateSubscriber.php, line 17
Namespace
Drupal\multiversion\EventSubscriberView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
SearchApiMigrateSubscriber:: |
protected | property | An array of Search API Index entities. | |
SearchApiMigrateSubscriber:: |
public static | function | Returns an array of event names this subscriber wants to listen to. | |
SearchApiMigrateSubscriber:: |
private | function | Set the index read only property state. | |
SearchApiMigrateSubscriber:: |
public | function | Enable Search API node indexing. | |
SearchApiMigrateSubscriber:: |
public | function | Disable Search API node indexing. | |
SearchApiMigrateSubscriber:: |
public | function | Constructs a new SearchApiMigrateSubscriber instance. |