You are here

class ConfigSubscriber in Search API Solr 4.x

Same name and namespace in other branches
  1. 8.3 src/EventSubscriber/ConfigSubscriber.php \Drupal\search_api_solr\EventSubscriber\ConfigSubscriber
  2. 8.2 src/EventSubscriber/ConfigSubscriber.php \Drupal\search_api_solr\EventSubscriber\ConfigSubscriber

Provides a ConfigSubscriber that adds language-specific Solr Field Types.

Whenever a new language is enabled this EventSubscriber installs all available Solr Field Types for that language.

Hierarchy

  • class \Drupal\search_api_solr\EventSubscriber\ConfigSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of ConfigSubscriber

1 string reference to 'ConfigSubscriber'
search_api_solr.services.yml in ./search_api_solr.services.yml
search_api_solr.services.yml
1 service uses ConfigSubscriber
search_api_solr.config_subscriber in ./search_api_solr.services.yml
Drupal\search_api_solr\EventSubscriber\ConfigSubscriber

File

src/EventSubscriber/ConfigSubscriber.php, line 18

Namespace

Drupal\search_api_solr\EventSubscriber
View source
class ConfigSubscriber implements EventSubscriberInterface {

  /**
   * The Config Installer.
   *
   * @var \Drupal\Core\Config\ConfigInstallerInterface
   */
  protected $configInstaller;

  /**
   * Constructs a ConfigSubscriber object.
   *
   * @param \Drupal\Core\Config\ConfigInstallerInterface $configInstaller
   *   The Config Installer.
   */
  public function __construct(ConfigInstallerInterface $configInstaller) {
    $this->configInstaller = $configInstaller;
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events[ConfigEvents::SAVE][] = [
      'onConfigSave',
    ];
    return $events;
  }

  /**
   * Installs all available Solr Field Types for a new language.
   *
   * @param \Drupal\Core\Config\ConfigCrudEvent $event
   *   The configuration event.
   *
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   * @throws \Drupal\search_api\SearchApiException
   */
  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',
      ];

      // installOptionalConfig will not replace existing configs and it contains
      // a dependency check so we need not perform any checks ourselves.
      $this->configInstaller
        ->installOptionalConfig(NULL, $restrict_by_dependency);

      // If a new language is added, the existing indexes must be re-indexed to
      // fill the language-specific sort fields for the new language.
      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);
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigSubscriber::$configInstaller protected property The Config Installer.
ConfigSubscriber::getSubscribedEvents public static function
ConfigSubscriber::onConfigSave public function Installs all available Solr Field Types for a new language.
ConfigSubscriber::__construct public function Constructs a ConfigSubscriber object.