diff --git a/config/schema/facets.facet.schema.yml b/config/schema/facets.facet.schema.yml index b4928e2..78a62cb 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 5c74a7d..cc41308 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 index. * * @var string @@ -131,7 +139,7 @@ class Facet extends ConfigEntityBase implements FacetInterface { /** * The facet source belonging to this facet. * - * @var \Drupal\facets\FacetSourceInterface + * @var \Drupal\facets\FacetSource\FacetSourceInterface * * @see getFacetSource() */ @@ -459,6 +467,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 getFacetSources($only_enabled = FALSE) { if (!isset($this->facetSourcePlugins)) { $this->facetSourcePlugins = []; diff --git a/src/FacetInterface.php b/src/FacetInterface.php index d50426d..ba4844c 100644 --- a/src/FacetInterface.php +++ b/src/FacetInterface.php @@ -57,6 +57,21 @@ interface FacetInterface extends ConfigEntityInterface { * @return mixed */ 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. * diff --git a/src/Form/FacetForm.php b/src/Form/FacetForm.php index 2326131..5b1db50 100644 --- a/src/Form/FacetForm.php +++ b/src/Form/FacetForm.php @@ -174,6 +174,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 79cd1ef..eeacf63 100644 --- a/src/Plugin/facets/processor/QueryStringUrlProcessor.php +++ b/src/Plugin/facets/processor/QueryStringUrlProcessor.php @@ -32,6 +32,13 @@ class QueryStringUrlProcessor extends UrlProcessorPluginBase { const SEPARATOR = ':'; /** + * A string of how to represent the facet in the url. + * + * @var string + */ + protected $url_alias; + + /** * @var array * An array containing the active filters */ @@ -58,9 +65,10 @@ class QueryStringUrlProcessor extends UrlProcessorPluginBase { return []; } + $this->url_alias = $facet->getUrlAlias(); /** @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->filter_key, [], TRUE); @@ -95,9 +103,12 @@ class QueryStringUrlProcessor extends UrlProcessorPluginBase { * {@inheritdoc} */ public function preQuery(FacetInterface $facet) { + + $this->url_alias = $facet->getUrlAlias(); + // Get the filter key of the facet. - if (isset($this->active_filters[$facet->getFieldAlias()])) { - foreach ($this->active_filters[$facet->getFieldAlias()] as $value) { + if (isset($this->active_filters[$this->url_alias])) { + foreach ($this->active_filters[$this->url_alias] as $value) { $facet->setActiveItem(trim($value, '"')); } } diff --git a/src/Tests/IntegrationTest.php b/src/Tests/IntegrationTest.php index 2f52e44..02c98dc 100644 --- a/src/Tests/IntegrationTest.php +++ b/src/Tests/IntegrationTest.php @@ -249,6 +249,7 @@ class IntegrationTest extends FacetWebTestBase { 'name' => '', 'id' => $facet_id, 'status' => 1, + 'url_alias' => $facet_name, ]; // 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 a4b22eb..b638685 100644 --- a/tests/src/Unit/Plugin/processor/QueryStringUrlProcessorTest.php +++ b/tests/src/Unit/Plugin/processor/QueryStringUrlProcessorTest.php @@ -52,7 +52,7 @@ class QueryStringUrlProcessorTest extends UnitTestCase { public function testSetSingleActiveItem() { $facet = new Facet([], 'facet'); $facet->setResults($this->original_results); - $facet->setFieldIdentifier('test'); + $facet->setUrlAlias('test'); $request = new Request; $request->query->set('f', ['test:badger']); @@ -66,7 +66,7 @@ class QueryStringUrlProcessorTest extends UnitTestCase { public function testSetMultipleActiveItems() { $facet = new Facet([], 'facet'); $facet->setResults($this->original_results); - $facet->setFieldIdentifier('test'); + $facet->setUrlAlias('test'); $request = new Request; $request->query->set('f', ['test:badger', 'test:mushroom', 'donkey:kong']); @@ -91,7 +91,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; @@ -111,7 +111,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->original_results;