diff --git a/modules/facets_rest/src/Plugin/views/style/FacetsSerializer.php b/modules/facets_rest/src/Plugin/views/style/FacetsSerializer.php index a9a178e..da35dfe 100644 --- a/modules/facets_rest/src/Plugin/views/style/FacetsSerializer.php +++ b/modules/facets_rest/src/Plugin/views/style/FacetsSerializer.php @@ -2,6 +2,7 @@ namespace Drupal\facets_rest\Plugin\views\style; +use Drupal\Core\Form\FormStateInterface; use Drupal\facets\FacetManager\DefaultFacetManager; use Drupal\rest\Plugin\views\style\Serializer; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -56,12 +57,24 @@ class FacetsSerializer extends Serializer { */ protected function defineOptions() { $options = parent::defineOptions(); - // Facets are served by json by default. - $options['formats'] = ['default' => ['json']]; + $options['show_facets'] = ['default' => TRUE]; return $options; } + /** + * {@inheritdoc} + */ + public function buildOptionsForm(&$form, FormStateInterface $form_state) { + parent::buildOptionsForm($form, $form_state); + + $form['show_facets'] = [ + '#type' => 'checkbox', + '#title' => $this->t('Show facets in the output'), + '#default_value' => $this->options['show_facets'], + ]; + } + /** * {@inheritdoc} */ @@ -101,7 +114,9 @@ class FacetsSerializer extends Serializer { } $rows['facets'] = array_values($processed_facets); - + if (!$this->options['show_facets']) { + $rows = $rows['search_results']; + } return $this->serializer->serialize($rows, $content_type, ['views_style_plugin' => $this]); } diff --git a/modules/facets_rest/tests/rest_view/config/install/views.view.search_api_rest_test_view.yml b/modules/facets_rest/tests/rest_view/config/install/views.view.search_api_rest_test_view.yml index 93e43f2..cc94a6a 100644 --- a/modules/facets_rest/tests/rest_view/config/install/views.view.search_api_rest_test_view.yml +++ b/modules/facets_rest/tests/rest_view/config/install/views.view.search_api_rest_test_view.yml @@ -320,6 +320,13 @@ display: type: facets_serializer row: type: data_field + cache_metadata: + max-age: -1 + contexts: + - 'languages:language_content' + - 'languages:language_interface' + - request_format + tags: { } page_1: display_plugin: page id: page_1 @@ -333,6 +340,8 @@ id: search_api_rest_test_view tag: '' langcode: en dependencies: + config: + - search_api.index.database_search_index module: - search_api - rest_view diff --git a/modules/facets_rest/tests/rest_view/rest_view.info.yml b/modules/facets_rest/tests/rest_view/rest_view.info.yml index 28a19ef..58f06bd 100644 --- a/modules/facets_rest/tests/rest_view/rest_view.info.yml +++ b/modules/facets_rest/tests/rest_view/rest_view.info.yml @@ -1,11 +1,13 @@ name: 'Rest view' type: module -description: 'Provides a Search API + rest dependecy.' +description: 'Provides a Search API + rest dependency to execute tests.' package: 'Search' core: 8.x hidden: true dependencies: - drupal:rest + - drupal:views + - drupal:serialization - search_api:search_api - search_api:search_api_test_db - - drupal:views + - facets:facets_rest diff --git a/modules/facets_rest/tests/src/Functional/RestIntegrationTest.php b/modules/facets_rest/tests/src/Functional/RestIntegrationTest.php index 1b4535e..e52f5d8 100644 --- a/modules/facets_rest/tests/src/Functional/RestIntegrationTest.php +++ b/modules/facets_rest/tests/src/Functional/RestIntegrationTest.php @@ -2,6 +2,7 @@ namespace Drupal\Tests\facets_rest\Functional; +use Drupal\facets\Entity\Facet; use Drupal\Tests\facets\Functional\FacetsTestBase; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -17,10 +18,6 @@ class RestIntegrationTest extends FacetsTestBase { */ public static $modules = [ 'rest_view', - 'facets_rest', - 'rest', - 'hal', - 'serialization', 'views_ui', ]; @@ -30,6 +27,17 @@ class RestIntegrationTest extends FacetsTestBase { public function setUp() { parent::setUp(); + // Create the users used for the tests. + $this->adminUser = $this->drupalCreateUser([ + 'administer search_api', + 'administer facets', + 'access administration pages', + 'administer nodes', + 'access content overview', + 'administer content types', + 'administer blocks', + 'administer views', + ]); $this->drupalLogin($this->adminUser); $this->setUpExampleStructure(); $this->insertExampleContent(); @@ -61,9 +69,9 @@ class RestIntegrationTest extends FacetsTestBase { public function testRestResults() { global $base_url; - $getOptions = ['query' => ['_format' => 'json']]; + $get_options = ['query' => ['_format' => 'json']]; - $result = $this->drupalGet('/facets-rest', $getOptions); + $result = $this->drupalGet('/facets-rest', $get_options); $this->assertSession()->responseHeaderEquals('content-type', 'application/json'); $this->assertSession()->statusCodeEquals(200); $json_decoded = json_decode($result, TRUE); @@ -112,7 +120,7 @@ class RestIntegrationTest extends FacetsTestBase { $this->drupalPostForm(NULL, $values, 'Save'); // Get the output from the rest view and decode it into an array. - $result = $this->drupalGet('/facets-rest', $getOptions); + $result = $this->drupalGet('/facets-rest', $get_options); $this->assertSession()->responseHeaderEquals('content-type', 'application/json'); $this->assertSession()->statusCodeEquals(200); $json_decoded = json_decode($result); @@ -207,7 +215,7 @@ class RestIntegrationTest extends FacetsTestBase { } // Filter and verify that the results are correct. - $json = $this->drupalGet($base_url . '/facets-rest?f%5B0%5D=type%3Aitem', $getOptions); + $json = $this->drupalGet($base_url . '/facets-rest?f%5B0%5D=type%3Aitem', $get_options); $this->assertSession()->statusCodeEquals(200); $this->assertSession()->responseHeaderEquals('content-type', 'application/json'); $json_decoded = json_decode($json); @@ -290,7 +298,6 @@ class RestIntegrationTest extends FacetsTestBase { $this->assertNotFalse(strpos($result->url, $url_part)); } } - } /** @@ -318,16 +325,12 @@ class RestIntegrationTest extends FacetsTestBase { * Tests urls on the same path. */ public function testSamePath() { - $getOptions = ['query' => ['_format' => 'json']]; - - $this->drupalGet('admin/config/search/facets/add-facet'); + $get_options = ['query' => ['_format' => 'json']]; $id = 'type'; $this->createFacet('Type', $id . '_rest', 'type', 'rest_export_1', 'views_rest__search_api_rest_test_view', FALSE); $this->createFacet('Type', $id, 'type', 'page_1', 'views_page__search_api_rest_test_view'); - $this->assertSession()->statusCodeEquals(200); - $this->drupalGet('/admin/config/search/facets/type/edit'); $values['widget'] = 'array'; $values['widget_config[show_numbers]'] = TRUE; $values['facet_settings[url_alias]'] = 'type'; @@ -341,7 +344,7 @@ class RestIntegrationTest extends FacetsTestBase { $pageUrl = $this->getSession()->getCurrentUrl(); $restUrl = str_replace('facets-page', 'facets-rest', $pageUrl); - $result = $this->drupalGet($restUrl, $getOptions); + $result = $this->drupalGet($restUrl, $get_options); $this->assertSession()->statusCodeEquals(200); $this->assertSession()->responseHeaderEquals('content-type', 'application/json'); $json_decoded = json_decode($result); @@ -349,4 +352,38 @@ class RestIntegrationTest extends FacetsTestBase { $this->assertEquals(3, count($json_decoded->search_results)); } + /** + * Tests hiding of facets from rest views. + */ + public function testHideFacets() { + $get_options = ['query' => ['_format' => 'json']]; + + $id = 'type_rest'; + $this->createFacet('Type', $id, 'type', 'rest_export_1', 'views_rest__search_api_rest_test_view', FALSE); + + $facet = Facet::load($id); + $facet->setWidget('array', ['show_numbers' => TRUE]); + $facet->save(); + + $result = $this->drupalGet('facets-rest', $get_options); + $this->assertSession()->responseHeaderEquals('content-type', 'application/json'); + $this->assertSession()->statusCodeEquals(200); + + $json_decoded = json_decode($result, TRUE); + $this->assertArrayHasKey('facets', $json_decoded); + $this->assertArrayHasKey('search_results', $json_decoded); + + $this->drupalGet('admin/structure/views/nojs/display/search_api_rest_test_view/rest_export_1/style_options'); + $this->drupalPostForm(NULL, ['style_options[show_facets]' => FALSE], 'Apply'); + $this->drupalPostForm(NULL, [], 'Save'); + + $result = $this->drupalGet('facets-rest', $get_options); + $this->assertSession()->responseHeaderEquals('content-type', 'application/json'); + $this->assertSession()->statusCodeEquals(200); + + $json_decoded = json_decode($result, TRUE); + $this->assertArrayNotHasKey('facets', $json_decoded); + $this->assertArrayNotHasKey('search_results', $json_decoded); + } + } diff --git a/src/Plugin/facets/url_processor/QueryString.php b/src/Plugin/facets/url_processor/QueryString.php index b641ec1..623a299 100644 --- a/src/Plugin/facets/url_processor/QueryString.php +++ b/src/Plugin/facets/url_processor/QueryString.php @@ -59,13 +59,14 @@ class QueryString extends UrlProcessorPluginBase { $request = $this->request; if ($facet->getFacetSource()->getPath()) { $request = Request::create($facet->getFacetSource()->getPath()); + $request->attributes->set('_format', $this->request->get('_format')); } // Grab any route params from the original request. $routeParameters = Url::createFromRequest($this->request) ->getRouteParameters(); - // Get request Url. + // Create a request url. $requestUrl = Url::createFromRequest($request); $requestUrl->setOption('attributes', ['rel' => 'nofollow']);