ConfigSubscriber.php in Search API Solr 8.3
File
src/EventSubscriber/ConfigSubscriber.php
View source
<?php
namespace Drupal\search_api_solr\EventSubscriber;
use Drupal\Core\Config\ConfigCrudEvent;
use Drupal\Core\Config\ConfigEvents;
use Drupal\Core\Config\ConfigInstallerInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ConfigSubscriber implements EventSubscriberInterface {
protected $configInstaller;
public function __construct(ConfigInstallerInterface $configInstaller) {
$this->configInstaller = $configInstaller;
}
public static function getSubscribedEvents() {
$events[ConfigEvents::SAVE][] = [
'onConfigSave',
];
return $events;
}
public function onConfigSave(ConfigCrudEvent $event) {
$saved_config = $event
->getConfig();
if (preg_match('@^language\\.entity\\.(.+)@', $saved_config
->getName(), $matches) && $matches[1] != LanguageInterface::LANGCODE_NOT_SPECIFIED) {
$restrict_by_dependency = [
'module' => 'search_api_solr',
];
$this->configInstaller
->installOptionalConfig(NULL, $restrict_by_dependency);
foreach (search_api_solr_get_servers() as $server) {
foreach ($server
->getIndexes() as $index) {
if ($index
->status() && !$index
->isReadOnly() && !$index
->isReindexing()) {
$index
->reindex();
}
}
}
}
elseif (preg_match('@^search_api_solr\\.solr_field_type\\..+@', $saved_config
->getName(), $matches)) {
\Drupal::messenger()
->addMessage(t('A new Solr field type has been installed due to configuration changes. It is advisable to download and deploy an updated config.zip to your Solr server.'), MessengerInterface::TYPE_WARNING);
}
elseif (preg_match('@^search_api_solr\\.solr_cache\\..+@', $saved_config
->getName(), $matches) || preg_match('@^search_api_solr\\.solr_request\\..+@', $saved_config
->getName(), $matches)) {
\Drupal::messenger()
->addMessage(t('There have been some configuration changes. It is advisable to download and deploy an updated config.zip to your Solr server.'), MessengerInterface::TYPE_WARNING);
}
}
}
Classes
Name |
Description |
ConfigSubscriber |
Provides a ConfigSubscriber that adds language-specific Solr Field Types. |