Commit 1ffa3d3b authored by borisson_'s avatar borisson_ Committed by borisson_

Issue #2624410 by borisson_: Create settings for the url processor to change the facet's alias

parent 6f1aa3ae
......@@ -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'
......
......@@ -37,6 +37,7 @@ use Drupal\facets\FacetInterface;
* config_export = {
* "id",
* "name",
* "url_alias",
* "field_identifier",
* "query_type_name",
* "facet_source_id",
......@@ -70,6 +71,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.
*
......@@ -349,6 +357,20 @@ class Facet extends ConfigEntityBase implements FacetInterface {
return $this->name;
}
/**
* {@inheritdoc}
*/
public function getUrlAlias() {
return $this->url_alias;
}
/**
* {@inheritdoc}
*/
public function setUrlAlias($url_alias) {
$this->url_alias = $url_alias;
}
/**
* {@inheritdoc}
*/
......
......@@ -69,6 +69,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.
*
......
......@@ -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;
......
......@@ -33,6 +33,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.
*
......@@ -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, '"'));
}
}
......
......@@ -148,6 +148,60 @@ class IntegrationTest extends FacetWebTestBase {
$this->assertUrl($expected_url);
}
/**
* 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.
*
......@@ -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
......
......@@ -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', []);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment