diff --git a/src/Plugin/search_api/backend/SearchApiSolrBackend.php b/src/Plugin/search_api/backend/SearchApiSolrBackend.php index 7b7b8d37..6c9f686b 100644 --- a/src/Plugin/search_api/backend/SearchApiSolrBackend.php +++ b/src/Plugin/search_api/backend/SearchApiSolrBackend.php @@ -2,6 +2,7 @@ namespace Drupal\search_api_solr\Plugin\search_api\backend; +use Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher; use Drupal\Component\Utility\Html; use Drupal\Component\Utility\Unicode; use Drupal\Core\Config\Config; @@ -45,6 +46,7 @@ use Drupal\search_api_autocomplete\Suggestion\SuggestionFactory; use Drupal\search_api_solr\Entity\SolrFieldType; use Drupal\search_api_solr\SearchApiSolrException; use Drupal\search_api_solr\Solarium\Autocomplete\Query as AutocompleteQuery; +use Drupal\search_api_solr\Solarium\EventDispatcher\Psr14Bridge; use Drupal\search_api_solr\Solarium\Result\StreamDocument; use Drupal\search_api_solr\SolrAutocompleteInterface; use Drupal\search_api_solr\SolrBackendInterface; @@ -158,10 +160,17 @@ class SearchApiSolrBackend extends BackendPluginBase implements SolrBackendInter */ protected $entityTypeManager; + /** + * The event dispatcher. + * + * @var \Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher + */ + protected $eventDispatcher; + /** * {@inheritdoc} */ - public function __construct(array $configuration, $plugin_id, array $plugin_definition, ModuleHandlerInterface $module_handler, Config $search_api_solr_settings, LanguageManagerInterface $language_manager, SolrConnectorPluginManager $solr_connector_plugin_manager, FieldsHelperInterface $fields_helper, DataTypeHelperInterface $dataTypeHelper, Helper $query_helper, EntityTypeManagerInterface $entityTypeManager) { + public function __construct(array $configuration, $plugin_id, array $plugin_definition, ModuleHandlerInterface $module_handler, Config $search_api_solr_settings, LanguageManagerInterface $language_manager, SolrConnectorPluginManager $solr_connector_plugin_manager, FieldsHelperInterface $fields_helper, DataTypeHelperInterface $dataTypeHelper, Helper $query_helper, EntityTypeManagerInterface $entityTypeManager, ContainerAwareEventDispatcher $eventDispatcher) { $this->moduleHandler = $module_handler; $this->searchApiSolrSettings = $search_api_solr_settings; $this->languageManager = $language_manager; @@ -170,6 +179,14 @@ class SearchApiSolrBackend extends BackendPluginBase implements SolrBackendInter $this->dataTypeHelper = $dataTypeHelper; $this->queryHelper = $query_helper; $this->entityTypeManager = $entityTypeManager; + if (class_exists('\Drupal\Component\EventDispatcher\Event')) { + // Drupal >= 9.1. + $this->eventDispatcher = $eventDispatcher; + } + else { + // Drupal <= 9.0. + $this->eventDispatcher = new Psr14Bridge($eventDispatcher); + } parent::__construct($configuration, $plugin_id, $plugin_definition); } @@ -189,7 +206,8 @@ class SearchApiSolrBackend extends BackendPluginBase implements SolrBackendInter $container->get('search_api.fields_helper'), $container->get('search_api.data_type_helper'), $container->get('solarium.query_helper'), - $container->get('entity_type.manager') + $container->get('entity_type.manager'), + $container->get('event_dispatcher') ); } @@ -570,7 +588,8 @@ class SearchApiSolrBackend extends BackendPluginBase implements SolrBackendInter throw new SearchApiException("The Solr Connector with ID '$this->configuration['connector']' could not be retrieved."); } } - return $this->solrConnector; + + return $this->solrConnector->setEventDispatcher($this->eventDispatcher); } /** @@ -4845,14 +4864,33 @@ class SearchApiSolrBackend extends BackendPluginBase implements SolrBackendInter /** * Implements the magic __sleep() method. * - * Prevents the Solr connector from being serialized. There's no need for a - * corresponding __wakeup() because of getSolrConnector(). + * Prevents the Solr connector from being serialized. For Drupal >= 9.1 + * there's no need for a corresponding __wakeup() because of + * getSolrConnector(). But for Drupal <= 9.0. * @see getSolrConnector() */ public function __sleep() { $properties = array_flip(parent::__sleep()); + unset($properties['solrConnector']); + if (isset($properties['eventDispatcher'])) { + // Drupal <= 9.0. + unset($properties['eventDispatcher']); + } + return array_keys($properties); } + /** + * {@inheritdoc} + */ + public function __wakeup() { + parent::__wakeup(); + + if (!$this->eventDispatcher) { + // Drupal <= 9.0. + $this->eventDispatcher = new Psr14Bridge(\Drupal::service('event_dispatcher')); + } + } + } diff --git a/src/Solarium/EventDispatcher/Psr14Bridge.php b/src/Solarium/EventDispatcher/Psr14Bridge.php index b8c65319..1ec87b11 100644 --- a/src/Solarium/EventDispatcher/Psr14Bridge.php +++ b/src/Solarium/EventDispatcher/Psr14Bridge.php @@ -16,8 +16,8 @@ final class Psr14Bridge extends ContainerAwareEventDispatcher implements EventDi */ protected $dispatcher; - public function __construct() { - $this->dispatcher = \Drupal::service('event_dispatcher'); + public function __construct(ContainerAwareEventDispatcher $eventDispatcher) { + $this->dispatcher = $eventDispatcher; } public function dispatch($event, Event $null = NULL) { diff --git a/src/SolrConnector/SolrConnectorPluginBase.php b/src/SolrConnector/SolrConnectorPluginBase.php index eabc77b8..bfcfb4e3 100644 --- a/src/SolrConnector/SolrConnectorPluginBase.php +++ b/src/SolrConnector/SolrConnectorPluginBase.php @@ -2,6 +2,7 @@ namespace Drupal\search_api_solr\SolrConnector; +use Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher; use Drupal\Component\Serialization\Json; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Link; @@ -12,7 +13,6 @@ use Drupal\search_api\Plugin\ConfigurablePluginBase; use Drupal\search_api\Plugin\PluginFormTrait; use Drupal\search_api_solr\SearchApiSolrException; use Drupal\search_api_solr\Solarium\Autocomplete\Query as AutocompleteQuery; -use Drupal\search_api_solr\Solarium\EventDispatcher\Psr14Bridge; use Drupal\search_api_solr\SolrConnectorInterface; use Solarium\Client; use Solarium\Core\Client\Adapter\Curl; @@ -26,7 +26,6 @@ use Solarium\Exception\HttpException; use Solarium\QueryType\Extract\Result as ExtractResult; use Solarium\QueryType\Update\Query\Query as UpdateQuery; use Solarium\QueryType\Select\Query\Query; -use Symfony\Component\DependencyInjection\ContainerInterface; use ZipStream\ZipStream; /** @@ -67,7 +66,7 @@ abstract class SolrConnectorPluginBase extends ConfigurablePluginBase implements /** * The event dispatcher. * - * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface + * @var \Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher */ protected $eventDispatcher; @@ -81,12 +80,9 @@ abstract class SolrConnectorPluginBase extends ConfigurablePluginBase implements /** * {@inheritdoc} */ - public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { - $plugin = parent::create($container, $configuration, $plugin_id, $plugin_definition); - - $plugin->eventDispatcher = new Psr14Bridge(); - - return $plugin; + public function setEventDispatcher(ContainerAwareEventDispatcher $eventDispatcher) : SolrConnectorInterface { + $this->eventDispatcher = $eventDispatcher; + return $this; } /** diff --git a/src/SolrConnectorInterface.php b/src/SolrConnectorInterface.php index b31d5a05..f3727967 100644 --- a/src/SolrConnectorInterface.php +++ b/src/SolrConnectorInterface.php @@ -2,6 +2,7 @@ namespace Drupal\search_api_solr; +use Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher; use Drupal\Component\Plugin\ConfigurableInterface; use Drupal\search_api_solr\Solarium\Autocomplete\Query as AutocompleteQuery; use Solarium\Core\Client\Endpoint; @@ -23,6 +24,13 @@ interface SolrConnectorInterface extends ConfigurableInterface { const OPTIMIZE_TIMEOUT = 'optimize_timeout'; const FINALIZE_TIMEOUT = 'finalize_timeout'; + /** + * Sets the event dispatcher. + * + * @param \Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher $eventDispatcher + */ + public function setEventDispatcher(ContainerAwareEventDispatcher $eventDispatcher): SolrConnectorInterface; + /** * Returns TRUE for Cloud. * diff --git a/tests/src/Unit/SearchApiBackendUnitTest.php b/tests/src/Unit/SearchApiBackendUnitTest.php index 5350823e..8ae27c7f 100644 --- a/tests/src/Unit/SearchApiBackendUnitTest.php +++ b/tests/src/Unit/SearchApiBackendUnitTest.php @@ -2,6 +2,7 @@ namespace Drupal\Tests\search_api_solr\Unit; +use Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher; use Drupal\Core\Config\Config; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Extension\ModuleHandlerInterface; @@ -75,7 +76,8 @@ class SearchApiBackendUnitTest extends UnitTestCase { $this->prophesize(FieldsHelperInterface::class)->reveal(), $this->prophesize(DataTypeHelperInterface::class)->reveal(), $this->queryHelper, - $this->entityTypeManager->reveal()); + $this->entityTypeManager->reveal(), + $this->prophesize(ContainerAwareEventDispatcher::class)->reveal()); } /**