diff --git a/src/FacetSource/FacetSourceDeriverBase.php b/src/FacetSource/FacetSourceDeriverBase.php index 0ca5819..0504c6b 100644 --- a/src/FacetSource/FacetSourceDeriverBase.php +++ b/src/FacetSource/FacetSourceDeriverBase.php @@ -31,11 +31,22 @@ abstract class FacetSourceDeriverBase implements ContainerDeriverInterface { protected $entityTypeManager; /** + * The search api display plugin manager. + * + * @var \Drupal\search_api\Display\DisplayPluginManager + */ + protected $searchApiDisplayPluginManager; + + /** * {@inheritdoc} */ public static function create(ContainerInterface $container, $base_plugin_id) { $deriver = new static(); + if (!in_array('search_api', array_keys($container->get('module_handler')->getModuleList()))) { + return; + } + /** @var \Drupal\Core\Entity\EntityTypeManager $entity_type_manager */ $entity_type_manager = $container->get('entity_type.manager'); $deriver->setEntityTypeManager($entity_type_manager); @@ -44,6 +55,9 @@ abstract class FacetSourceDeriverBase implements ContainerDeriverInterface { $translation = $container->get('string_translation'); $deriver->setStringTranslation($translation); + $search_api_display_plugin_manager = $container->get('plugin.manager.search_api.display'); + $deriver->setSearchApiDisplayPluginManager($search_api_display_plugin_manager); + return $deriver; } @@ -95,4 +109,12 @@ abstract class FacetSourceDeriverBase implements ContainerDeriverInterface { return strnatcasecmp($a['label'], $b['label']); } + public function setSearchApiDisplayPluginManager($search_api_display_plugin_manager) { + $this->searchApiDisplayPluginManager = $search_api_display_plugin_manager; + } + + public function getSearchApiDisplayPluginManager() { + return $this->searchApiDisplayPluginManager; + } + } diff --git a/src/Plugin/facets/facet_source/SearchApiViews.php b/src/Plugin/facets/facet_source/SearchApiViews.php index 917f195..2a21751 100644 --- a/src/Plugin/facets/facet_source/SearchApiViews.php +++ b/src/Plugin/facets/facet_source/SearchApiViews.php @@ -9,13 +9,10 @@ use Drupal\views\Entity\View; use Drupal\views\Views; /** - * A facet source to support search api views. - * - * This facet source only supports views that have a search api index as a base, - * and only those displays that are a block or a page. + * A facet source to support search api display plugins. * * @FacetsFacetSource( - * id = "search_api_views", + * id = "views_page", * deriver = "Drupal\facets\Plugin\facets\facet_source\SearchApiViewsDeriver" * ) */ @@ -97,7 +94,7 @@ class SearchApiViews extends SearchApiBaseFacetSource implements SearchApiFacetS $results = $this->searchApiQueryHelper->getResults($this->pluginId); // If our results are not there, execute the view to get the results. - if (!$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']); @@ -106,10 +103,12 @@ class SearchApiViews extends SearchApiBaseFacetSource implements SearchApiFacetS } // Get the results from the cache. It is possible it still errored out. - // @todo figure out what to do when this errors out. if ($results instanceof ResultSetInterface) { // Get our facet data. $facet_results = $results->getExtraData('search_api_facets'); + if ($facet_results === []) { + return; + } // Loop over each facet and execute the build method from the given // query type. @@ -138,7 +137,8 @@ class SearchApiViews extends SearchApiBaseFacetSource implements SearchApiFacetS case 'page': $request = \Drupal::requestStack()->getMasterRequest(); if ($request->attributes->get('_controller') === 'Drupal\views\Routing\ViewPageController::handle') { - list(, $search_api_view_id, $search_api_view_display) = explode(':', $this->getPluginId()); + 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; diff --git a/src/Plugin/facets/facet_source/SearchApiViewsDeriver.php b/src/Plugin/facets/facet_source/SearchApiViewsDeriver.php index 6c11383..ea3961b 100644 --- a/src/Plugin/facets/facet_source/SearchApiViewsDeriver.php +++ b/src/Plugin/facets/facet_source/SearchApiViewsDeriver.php @@ -7,12 +7,11 @@ use Drupal\Component\Plugin\PluginBase; use Drupal\facets\FacetSource\FacetSourceDeriverBase; /** - * Derives a facet source plugin definition for every Search API view. + * Derives a facet source plugin definition for every Search API display plugin. * - * This facet source only supports views that have a search api index as a base, - * and only those displays that are a block or a page. + * This facet source supports all search api display sources. * - * @see \Drupal\facets\Plugin\facets\facet_source\SearchApiViews + * @see \Drupal\facets\Plugin\facets\facet_source\SearchApi */ class SearchApiViewsDeriver extends FacetSourceDeriverBase { @@ -22,54 +21,27 @@ class SearchApiViewsDeriver extends FacetSourceDeriverBase { public function getDerivativeDefinitions($base_plugin_definition) { $base_plugin_id = $base_plugin_definition['id']; - try { - /** @var \Drupal\Core\Entity\EntityStorageInterface $views_storage */ - $views_storage = $this->entityTypeManager->getStorage('view'); - $all_views = $views_storage->loadMultiple(); - } - catch (PluginNotFoundException $e) { - return []; - } - $plugin_derivatives = array(); - - /** @var \Drupal\views\Entity\View $view */ - foreach ($all_views as $view) { - // Hardcoded usage of Search API views, for now. - if (strpos($view->get('base_table'), 'search_api_index') !== FALSE) { - $displays = $view->get('display'); - foreach ($displays as $display_id => $display_info) { + $search_api_displays = $this->getSearchApiDisplayPluginManager(); - // We only support page, block and REST export displays for views. - $allowed_plugins = ['page', 'block', 'rest_export']; - if (in_array($display_info['display_plugin'], $allowed_plugins)) { - $machine_name = $view->id() . PluginBase::DERIVATIVE_SEPARATOR . $display_id; - - $label_arguments = [ - '%view_name' => $view->label(), - '%display_title' => $display_info['display_title'], - '%display_type' => $display_info['display_plugin'], - ]; - $plugin_derivatives[$machine_name] = [ - 'id' => $base_plugin_id . PluginBase::DERIVATIVE_SEPARATOR . $machine_name, - 'label' => $this->t('Search API view: %view_name, display: %display_title (%display_type)', - $label_arguments - ), - 'description' => $this->t('Provides a facet source.'), - 'view_id' => $view->id(), - 'view_display' => $display_id, - ] + $base_plugin_definition; - - $sources[] = $this->t( - 'Search API view: %view, display: %display', - [ - '%view' => $view->label(), - '%display' => $display_info['display_title'], - ] - ); - } - } - } + $plugin_derivatives = array(); + foreach ($search_api_displays->getDefinitions() as $display) { + $machine_name = $display['view_id'] . '__' . $display['view_display']; + + $plugin_derivatives[$machine_name] = [ + 'id' => $base_plugin_id . ':' . $machine_name, + 'label' => $display['label'], + 'description' => $this->t('Provides a facet source.'), + 'view_id' => $display['view_id'], + 'view_display' => $display['view_display'], + ] + $base_plugin_definition; + + $arguments = [ + '%view' => $display['label'], + '%display' => $display['view_display'], + ]; + $sources[] = $this->t('Search API view: %view, display: %display', $arguments); } + uasort($plugin_derivatives, array($this, 'compareDerivatives')); $this->derivatives[$base_plugin_id] = $plugin_derivatives; diff --git a/src/Plugin/facets/query_type/SearchApiString.php b/src/Plugin/facets/query_type/SearchApiString.php index 63a6f05..03504e6 100644 --- a/src/Plugin/facets/query_type/SearchApiString.php +++ b/src/Plugin/facets/query_type/SearchApiString.php @@ -82,6 +82,7 @@ class SearchApiString extends QueryTypePluginBase { } $this->facet->setResults($facet_results); } + return $this->facet; }