diff --git a/src/Form/FacetForm.php b/src/Form/FacetForm.php index 3870704..53c269c 100644 --- a/src/Form/FacetForm.php +++ b/src/Form/FacetForm.php @@ -336,6 +336,8 @@ class FacetForm extends EntityForm { $view->setDisplay($display); $view->display_handler->overrideOption('cache', ['type' => 'none']); $view->save(); + + $display_plugin = $view->getDisplay()->getPluginId(); } if ($is_new) { @@ -344,6 +346,10 @@ class FacetForm extends EntityForm { drupal_set_message($message); $form_state->setRedirect('entity.facets_facet.display_form', ['facets_facet' => $facet->id()]); } + + if (isset($view_id) && $display_plugin === 'block') { + $facet->setOnlyVisibleWhenFacetSourceIsVisible(FALSE); + } } else { drupal_set_message(t('Facet %name has been updated.', ['%name' => $facet->getName()])); diff --git a/src/Plugin/facets/facet_source/SearchApiViews.php b/src/Plugin/facets/facet_source/SearchApiViews.php index 1b075d2..8372daf 100644 --- a/src/Plugin/facets/facet_source/SearchApiViews.php +++ b/src/Plugin/facets/facet_source/SearchApiViews.php @@ -16,7 +16,10 @@ use Drupal\views\Views; /** - * Represents a facet source which represents the search api 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. * * @FacetsFacetSource( * id = "search_api_views", @@ -88,6 +91,7 @@ class SearchApiViews extends SearchApiBaseFacetSource implements SearchApiFacetS $view = Views::getView($this->pluginDefinition['view_id']); $view->setDisplay($this->pluginDefinition['view_display']); return '/' . $view->getDisplay()->getPath(); + case 'block': default: return \Drupal::service('path.current')->getPath(); @@ -151,8 +155,11 @@ class SearchApiViews extends SearchApiBaseFacetSource implements SearchApiFacetS return FALSE; case 'block': - // There is no way to know if a block is embedded on a page. - return TRUE; + // 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; } diff --git a/src/Plugin/facets/facet_source/SearchApiViewsDeriver.php b/src/Plugin/facets/facet_source/SearchApiViewsDeriver.php index cdb1aab..0157fbc 100644 --- a/src/Plugin/facets/facet_source/SearchApiViewsDeriver.php +++ b/src/Plugin/facets/facet_source/SearchApiViewsDeriver.php @@ -13,6 +13,9 @@ use Drupal\facets\FacetSource\FacetSourceDeriverBase; /** * Derives a facet source plugin definition for every search api view. * + * 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. + * * @see \Drupal\facets\Plugin\facets\facet_source\SearchApiViews */ class SearchApiViewsDeriver extends FacetSourceDeriverBase { @@ -34,16 +37,27 @@ class SearchApiViewsDeriver extends FacetSourceDeriverBase { // 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 $name => $display_info) { + foreach ($displays as $display_id => $display_info) { + + // We only support pages and blocks because those are the ones that + // we've tested. They are also the only ones that support for + // ::isRenderedInCurrentRequest() and ::getPath(). if (in_array($display_info['display_plugin'], ['page', 'block'])) { - $machine_name = $view->id() . PluginBase::DERIVATIVE_SEPARATOR . $name; + $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', ['%view_name' => $view->label(), '%display_title' => $display_info['display_title']]), + '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' => $name, + 'view_display' => $display_id, ] + $base_plugin_definition; $sources[] = $this->t( diff --git a/src/Tests/UrlIntegrationTest.php b/src/Tests/UrlIntegrationTest.php index 63f8da3..1584416 100644 --- a/src/Tests/UrlIntegrationTest.php +++ b/src/Tests/UrlIntegrationTest.php @@ -92,7 +92,7 @@ class UrlIntegrationTest extends FacetWebTestBase { // Go to the only enabled facet source's config and change the filter key. $this->drupalGet('admin/config/search/facets'); - $this->clickLink($this->t('Configure')); + $this->clickLink($this->t('Configure'), 1); $edit = [ 'filter_key' => 'y', @@ -115,7 +115,7 @@ class UrlIntegrationTest extends FacetWebTestBase { // Go to the only enabled facet source's config and change the url // processor. $this->drupalGet('admin/config/search/facets'); - $this->clickLink($this->t('Configure')); + $this->clickLink($this->t('Configure'), 1); $edit = [ 'filter_key' => 'y',