summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorborisson_2015-12-20 10:53:00 (GMT)
committerJoris Vercammen2015-12-20 10:53:00 (GMT)
commit1ffa3d3bf110ef8a48969f9d8c9d4a8c099df993 (patch)
treeb4b303f5256e070202ba52132af53c24f77ed8b2
parent6f1aa3aeec898795707def9fdb7782da00769dd7 (diff)
Issue #2624410 by borisson_: Create settings for the url processor to change the facet's alias
-rw-r--r--config/schema/facets.facet.schema.yml3
-rw-r--r--src/Entity/Facet.php22
-rw-r--r--src/FacetInterface.php14
-rw-r--r--src/Form/FacetForm.php8
-rw-r--r--src/Plugin/facets/processor/QueryStringUrlProcessor.php19
-rw-r--r--src/Tests/IntegrationTest.php55
-rw-r--r--tests/src/Unit/Plugin/processor/QueryStringUrlProcessorTest.php6
7 files changed, 124 insertions, 3 deletions
diff --git a/config/schema/facets.facet.schema.yml b/config/schema/facets.facet.schema.yml
index 3a17744..5dbd43f 100644
--- a/config/schema/facets.facet.schema.yml
+++ b/config/schema/facets.facet.schema.yml
@@ -14,6 +14,9 @@ facets.facet.*:
status:
type: boolean
label: 'Status'
+ url_alias:
+ type: string
+ label: 'Name of facet as used in the URL'
facet_source_id:
type: string
label: 'Facet source id'
diff --git a/src/Entity/Facet.php b/src/Entity/Facet.php
index 88c71bd..a6495fb 100644
--- a/src/Entity/Facet.php
+++ b/src/Entity/Facet.php
@@ -37,6 +37,7 @@ use Drupal\facets\FacetInterface;
* config_export = {
* "id",
* "name",
+ * "url_alias",
* "field_identifier",
* "query_type_name",
* "facet_source_id",
@@ -71,6 +72,13 @@ class Facet extends ConfigEntityBase implements FacetInterface {
protected $name;
/**
+ * The name for the parameter when used in the URL.
+ *
+ * @var string
+ */
+ protected $url_alias;
+
+ /**
* A string describing the facet.
*
* @var string
@@ -352,6 +360,20 @@ class Facet extends ConfigEntityBase implements FacetInterface {
/**
* {@inheritdoc}
*/
+ public function getUrlAlias() {
+ return $this->url_alias;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function setUrlAlias($url_alias) {
+ $this->url_alias = $url_alias;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
public function setFacetSourceId($facet_source_id) {
$this->facet_source_id = $facet_source_id;
return $this;
diff --git a/src/FacetInterface.php b/src/FacetInterface.php
index 58c1cd1..c01c6ba 100644
--- a/src/FacetInterface.php
+++ b/src/FacetInterface.php
@@ -70,6 +70,20 @@ interface FacetInterface extends ConfigEntityInterface {
public function getName();
/**
+ * Gets the name of the facet for use in the URL.
+ *
+ * @param string $url_parameter
+ */
+ public function getUrlAlias();
+
+ /**
+ * Sets the name of the facet for use in the URL.
+ *
+ * @param string $url_alias
+ */
+ public function setUrlAlias($url_alias);
+
+ /**
* Sets an item with value to active.
*
* @param string $value
diff --git a/src/Form/FacetForm.php b/src/Form/FacetForm.php
index d16a815..844fe07 100644
--- a/src/Form/FacetForm.php
+++ b/src/Form/FacetForm.php
@@ -173,6 +173,14 @@ class FacetForm extends EntityForm {
],
];
+ $form['url_alias'] = [
+ '#type' => 'textfield',
+ '#title' => $this->t('The name of the facet for usage in URLs'),
+ '#default_value' => $facet->getUrlAlias(),
+ '#maxlength' => 50,
+ '#required' => TRUE,
+ ];
+
$facet_sources = [];
foreach ($this->getFacetSourcePluginManager()->getDefinitions() as $facet_source_id => $definition) {
$facet_sources[$definition['id']] = !empty($definition['label']) ? $definition['label'] : $facet_source_id;
diff --git a/src/Plugin/facets/processor/QueryStringUrlProcessor.php b/src/Plugin/facets/processor/QueryStringUrlProcessor.php
index 1c7bbb5..3c53a32 100644
--- a/src/Plugin/facets/processor/QueryStringUrlProcessor.php
+++ b/src/Plugin/facets/processor/QueryStringUrlProcessor.php
@@ -34,6 +34,13 @@ class QueryStringUrlProcessor extends UrlProcessorPluginBase {
const SEPARATOR = ':';
/**
+ * A string of how to represent the facet in the url.
+ *
+ * @var string
+ */
+ protected $url_alias;
+
+ /**
* An array of active filters.
*
* @var string[]
@@ -57,6 +64,9 @@ class QueryStringUrlProcessor extends UrlProcessorPluginBase {
// First get the current list of get parameters.
$get_params = $this->request->query;
+ // Set the url alias from the the facet object.
+ $this->url_alias = $facet->getUrlAlias();
+
// No results are found for this facet, so don't try to create urls.
if (empty($results)) {
return [];
@@ -64,7 +74,7 @@ class QueryStringUrlProcessor extends UrlProcessorPluginBase {
/** @var \Drupal\facets\Result\ResultInterface $result */
foreach ($results as &$result) {
- $filter_string = $facet->getFieldAlias() . ':' . $result->getRawValue();
+ $filter_string = $this->url_alias . ':' . $result->getRawValue();
$result_get_params = clone $get_params;
$filter_params = $result_get_params->get($this->filterKey, [], TRUE);
@@ -99,9 +109,12 @@ class QueryStringUrlProcessor extends UrlProcessorPluginBase {
* {@inheritdoc}
*/
public function preQuery(FacetInterface $facet) {
+ // Set the url alias from the the facet object.
+ $this->url_alias = $facet->getUrlAlias();
+
// Get the filter key of the facet.
- if (isset($this->activeFilters[$facet->getFieldAlias()])) {
- foreach ($this->activeFilters[$facet->getFieldAlias()] as $value) {
+ if (isset($this->activeFilters[$this->url_alias])) {
+ foreach ($this->activeFilters[$this->url_alias] as $value) {
$facet->setActiveItem(trim($value, '"'));
}
}
diff --git a/src/Tests/IntegrationTest.php b/src/Tests/IntegrationTest.php
index c2b6ecf..808d856 100644
--- a/src/Tests/IntegrationTest.php
+++ b/src/Tests/IntegrationTest.php
@@ -149,6 +149,60 @@ class IntegrationTest extends FacetWebTestBase {
}
/**
+ * Tests that an url alias works correctly.
+ */
+ public function testUrlAlias() {
+ $facet_id = 'ab_facet';
+ $facet_name = 'ab>Facet';
+
+ // Make sure we're logged in with a user that has sufficient permissions.
+ $this->drupalLogin($this->adminUser);
+
+ $facet_add_page = $this->urlGenerator->generateFromRoute('entity.facets_facet.add_form', [], ['absolute' => TRUE]);
+ $facet_edit_page = $this->urlGenerator->generateFromRoute('entity.facets_facet.edit_form', ['facets_facet' => $facet_id], ['absolute' => TRUE]);
+
+ $this->drupalGet($facet_add_page);
+ $this->assertResponse(200);
+
+ $form_values = [
+ 'name' => $facet_name,
+ 'id' => $facet_id,
+ 'status' => 1,
+ 'facet_source_id' => 'search_api_views:search_api_test_views_fulltext:page_1',
+ 'facet_source_configs[search_api_views:search_api_test_views_fulltext:page_1][field_identifier]' => 'entity:entity_test/type',
+ ];
+ $this->drupalPostForm(NULL, ['facet_source_id' => 'search_api_views:search_api_test_views_fulltext:page_1'], $this->t('Configure facet source'));
+ $this->drupalPostForm(NULL, $form_values, $this->t('Save'));
+ $this->assertText($this->t('The name of the facet for usage in URLs field is required.'));
+
+ $form_values['url_alias'] = 'test';
+ $this->drupalPostForm(NULL, $form_values, $this->t('Save'));
+ $this->assertRaw(t('Facet %name has been created.', ['%name' => $facet_name]));
+
+ $this->createFacetBlock($facet_id);
+
+ $this->insertExampleContent();
+ $this->assertEqual($this->indexItems($this->indexId), 5, '5 items were indexed.');
+
+ $this->drupalGet('search-api-test-fulltext');
+ $this->assertLink('item');
+ $this->assertLink('article');
+
+ $this->clickLink('item');
+ $this->assertUrl('search-api-test-fulltext?f[0]=test:item');
+
+ $this->drupalGet($facet_edit_page);
+ $this->drupalPostForm(NULL, ['url_alias' => 'llama'], $this->t('Save'));
+
+ $this->drupalGet('search-api-test-fulltext');
+ $this->assertLink('item');
+ $this->assertLink('article');
+
+ $this->clickLink('item');
+ $this->assertUrl('search-api-test-fulltext?f[0]=llama:item');
+ }
+
+ /**
* Deletes a facet block by id.
*
* @param string $id
@@ -285,6 +339,7 @@ class IntegrationTest extends FacetWebTestBase {
'name' => '',
'id' => $facet_id,
'status' => 1,
+ 'url_alias' => $facet_id,
];
// Try filling out the form, but without having filled in a name for the
diff --git a/tests/src/Unit/Plugin/processor/QueryStringUrlProcessorTest.php b/tests/src/Unit/Plugin/processor/QueryStringUrlProcessorTest.php
index d23054b..1b4c58f 100644
--- a/tests/src/Unit/Plugin/processor/QueryStringUrlProcessorTest.php
+++ b/tests/src/Unit/Plugin/processor/QueryStringUrlProcessorTest.php
@@ -60,6 +60,7 @@ class QueryStringUrlProcessorTest extends UnitTestCase {
public function testSetSingleActiveItem() {
$facet = new Facet([], 'facet');
$facet->setResults($this->originalResults);
+ $facet->setUrlAlias('test');
$facet->setFieldIdentifier('test');
$request = new Request();
@@ -77,6 +78,7 @@ class QueryStringUrlProcessorTest extends UnitTestCase {
public function testSetMultipleActiveItems() {
$facet = new Facet([], 'facet');
$facet->setResults($this->originalResults);
+ $facet->setUrlAlias('test');
$facet->setFieldIdentifier('test');
$request = new Request();
@@ -93,6 +95,7 @@ class QueryStringUrlProcessorTest extends UnitTestCase {
*/
public function testEmptyBuild() {
$facet = new Facet([], 'facet');
+ $facet->setUrlAlias('test');
$facet->setFacetSourceId('facet_source__dummy');
$request = new Request();
@@ -109,6 +112,7 @@ class QueryStringUrlProcessorTest extends UnitTestCase {
public function testBuild() {
$facet = new Facet([], 'facet');
$facet->setFieldIdentifier('test');
+ $facet->setUrlAlias('test');
$facet->setFacetSourceId('facet_source__dummy');
$request = new Request();
@@ -130,6 +134,7 @@ class QueryStringUrlProcessorTest extends UnitTestCase {
public function testBuildWithActiveItem() {
$facet = new Facet([], 'facet');
$facet->setFieldIdentifier('test');
+ $facet->setUrlAlias('test');
$facet->setFacetSourceId('facet_source__dummy');
$original_results = $this->originalResults;
@@ -178,6 +183,7 @@ class QueryStringUrlProcessorTest extends UnitTestCase {
$facet = new Facet([], 'facet');
$facet->setFieldIdentifier('test');
$facet->setFacetSourceId('facet_source__dummy');
+ $facet->setUrlAlias('test');
$request = new Request();
$request->query->set('ab', []);