diff --git a/src/Plugin/facets/facet_source/SearchApiViews.php b/src/Plugin/facets/facet_source/SearchApiViews.php index 6cc7367..ba77bc7 100644 --- a/src/Plugin/facets/facet_source/SearchApiViews.php +++ b/src/Plugin/facets/facet_source/SearchApiViews.php @@ -51,45 +51,18 @@ class SearchApiViews extends SearchApiBaseFacetSource implements SearchApiFacetS */ public function __construct(array $configuration, $plugin_id, array $plugin_definition, $query_type_plugin_manager, $search_results_cache) { parent::__construct($configuration, $plugin_id, $plugin_definition, $query_type_plugin_manager, $search_results_cache); - - // Load facet plugin definition and depending on those settings; load the - // corresponding view with the correct view with the correct display set. - // Get that display's query so we can check if this is a Search API based - // view. - $view = Views::getView($plugin_definition['view_id']); - if (!empty($view)) { - $view->setDisplay($plugin_definition['view_display']); - $query = $view->getQuery(); - - // Only add the index if the $query is a Search API Query. - if ($query instanceof SearchApiQuery) { - // Set the Search API Index. - $this->index = $query->getIndex(); - } - } } /** * {@inheritdoc} */ public function getPath() { - $display = View::load($this->pluginDefinition['view_id'])->getDisplay($this->pluginDefinition['view_display']); - switch ($display['display_plugin']) { - case 'page': - $view = Views::getView($this->pluginDefinition['view_id']); - $view->setDisplay($this->pluginDefinition['view_display']); - return '/' . $view->getDisplay()->getPath(); - - case 'block': - default: - $current_path = \Drupal::service('path.current')->getPath(); - if (\Drupal::moduleHandler()->moduleExists('path')) { - return \Drupal::service('path.alias_manager')->getAliasByPath($current_path); - } - else { - return $current_path; - } - } + $display_id = $this->getPluginDefinition()['display_id']; + $displayPluginManager = \Drupal::service('plugin.manager.search_api.display'); + + /** @var \Drupal\search_api\Display\DisplayInterface $display */ + $display = $displayPluginManager->createInstance($display_id); + return $display->getUrl(); } /** @@ -99,15 +72,6 @@ class SearchApiViews extends SearchApiBaseFacetSource implements SearchApiFacetS // Check if there are results in the static cache. $results = $this->searchApiQueryHelper->getResults($this->pluginId); - // If our results are not there, execute the view to get the results. - if ($results === NULL) { - // If there are no results, execute the view. and check for results again! - $view = Views::getView($this->pluginDefinition['view_id']); - $view->setDisplay($this->pluginDefinition['view_display']); - $view->execute(); - $results = $this->searchApiQueryHelper->getResults($this->pluginId); - } - // Get the results from the cache. It is possible it still errored out. if ($results instanceof ResultSetInterface) { // Get our facet data. @@ -137,36 +101,24 @@ class SearchApiViews extends SearchApiBaseFacetSource implements SearchApiFacetS * {@inheritdoc} */ public function isRenderedInCurrentRequest() { - $display = View::load($this->pluginDefinition['view_id'])->getDisplay($this->pluginDefinition['view_display']); - switch ($display['display_plugin']) { - case 'rest_export': - case 'page': - $request = \Drupal::requestStack()->getMasterRequest(); - if ($request->attributes->get('_controller') === 'Drupal\views\Routing\ViewPageController::handle') { - list(, $view) = explode(':', $this->getPluginId()); - list($search_api_view_id, $search_api_view_display) = explode('__', $view); - - if ($request->attributes->get('view_id') == $search_api_view_id && $request->attributes->get('display_id') == $search_api_view_display) { - return TRUE; - } - } - return FALSE; - - case 'block': - // There is no way to know if a block is embedded on a page, because - // blocks can be rendered in isolation (see big_pipe, esi, ...). To be - // sure we're not disclosing information we're not sure about, we always - // return false. - return FALSE; - } - return FALSE; + $display_id = $this->getPluginDefinition()['display_id']; + $displayPluginManager = \Drupal::service('plugin.manager.search_api.display'); + + /** @var \Drupal\search_api\Display\DisplayInterface $display */ + $display = $displayPluginManager->createInstance($display_id); + return $display->isRenderedInCurrentRequest(); } /** * {@inheritdoc} */ public function getIndex() { - return $this->index; + $display_id = $this->getPluginDefinition()['display_id']; + $displayPluginManager = \Drupal::service('plugin.manager.search_api.display'); + + /** @var \Drupal\search_api\Display\DisplayInterface $display */ + $display = $displayPluginManager->createInstance($display_id); + return $display->getIndex(); } /** diff --git a/src/Plugin/facets/facet_source/SearchApiViewsDeriver.php b/src/Plugin/facets/facet_source/SearchApiViewsDeriver.php index 998f033..c9f8dd4 100644 --- a/src/Plugin/facets/facet_source/SearchApiViewsDeriver.php +++ b/src/Plugin/facets/facet_source/SearchApiViewsDeriver.php @@ -3,8 +3,6 @@ namespace Drupal\facets\Plugin\facets\facet_source; use Drupal\facets\FacetSource\FacetSourceDeriverBase; -use Drupal\search_api\Plugin\views\query\SearchApiQuery; -use Drupal\views\Entity\View; /** * Derives a facet source plugin definition for every Search API display plugin. @@ -22,15 +20,17 @@ class SearchApiViewsDeriver extends FacetSourceDeriverBase { $base_plugin_id = $base_plugin_definition['id']; $plugin_derivatives = array(); - $search_api_displays = $this->getSearchApiDisplayPluginManager(); - foreach ($search_api_displays->getDefinitions() as $display) { + $displayPluginManager = $this->getSearchApiDisplayPluginManager(); + foreach ($displayPluginManager->getDefinitions() as $display) { // Avoid providing corrupted displays. if (isset($display['view_id']) && isset($display['view_display']) && isset($display['label'])) { $machine_name = $display['view_id'] . '__' . $display['view_display']; - $view = View::load($display['view_id']); - $index = SearchApiQuery::getIndexFromTable($view->get('base_table')); - $supports_facets = $index->getServerInstance()->supportsFeature('search_api_facets'); + /** @var \Drupal\search_api\Display\DisplayInterface $display */ + $display = $displayPluginManager->createInstance($display['id']); + $supports_facets = $display->getIndex() + ->getServerInstance() + ->supportsFeature('search_api_facets'); // If facets are not supported by the server, don't actually add this to // the list of plugins. @@ -40,10 +40,9 @@ class SearchApiViewsDeriver extends FacetSourceDeriverBase { $plugin_derivatives[$machine_name] = [ 'id' => $base_plugin_id . ':' . $machine_name, + 'display_id' => $display['id'], 'label' => $display['label'], 'description' => $this->t('Provides a facet source.'), - 'view_id' => $display['view_id'], - 'view_display' => $display['view_display'], ] + $base_plugin_definition; $arguments = [