Commit 9f937187 authored by git's avatar git Committed by borisson_

Issue #2862703 by alan-ps: Add Reset Facets Button to summary

parent 5bac28df
<?php
namespace Drupal\facets_summary\Plugin\facets_summary\processor;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Link;
use Drupal\facets_summary\FacetsSummaryInterface;
use Drupal\facets_summary\Processor\BuildProcessorInterface;
use Drupal\facets_summary\Processor\ProcessorPluginBase;
/**
* Provides a processor that allows to reset facet filters.
*
* @SummaryProcessor(
* id = "reset_facets",
* label = @Translation("Adds reset facets link."),
* description = @Translation("When checked, this facet will add a link to reset enabled facets."),
* stages = {
* "build" = 50
* }
* )
*/
class ResetFacetsProcessor extends ProcessorPluginBase implements BuildProcessorInterface {
/**
* {@inheritdoc}
*/
public function build(FacetsSummaryInterface $facets_summary, array $build, array $facets) {
$conf = $facets_summary->getProcessorConfigs()[$this->getPluginId()];
// Do nothing if there are no selected facets or reset text is empty.
if (empty($build['#items']) || empty($conf['settings']['link_text'])) {
return $build;
}
$request = \Drupal::requestStack()->getMasterRequest();
$query_params = $request->query->all();
// Bypass all active facets and remove them from the query parameters array.
foreach ($facets as $facet) {
$url_alias = $facet->getUrlAlias();
$filter_key = $facet->getFacetSourceConfig()->getFilterKey() ?: 'f';
if (isset($query_params[$filter_key])) {
foreach ($query_params[$filter_key] as $delta => $param) {
if (strpos($param, $url_alias . ':') !== FALSE) {
unset($query_params[$filter_key][$delta]);
}
}
if (!$query_params[$filter_key]) {
unset($query_params[$filter_key]);
}
}
}
// Lets use any first facet to get correct url.
$results = reset($facets)->getResults();
/** @var \Drupal\Core\Url $first_item_url */
$first_item_url = reset($results)->getUrl();
$first_item_url = clone ($first_item_url);
$first_item_url->setOptions(['query' => $query_params]);
$item = (new Link($conf['settings']['link_text'], $first_item_url))->toRenderable();
array_unshift($build['#items'], $item);
return $build;
}
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state, FacetsSummaryInterface $facets_summary) {
// By default, there should be no config form.
$processors = $facets_summary->getProcessors();
$config = isset($processors[$this->getPluginId()]) ? $processors[$this->getPluginId()] : NULL;
$build['link_text'] = [
'#type' => 'textfield',
'#title' => $this->t('Reset facets link text'),
'#default_value' => !is_null($config) ? $config->getConfiguration()['link_text'] : $this->defaultConfiguration()['link_text'],
];
return $build;
}
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return ['link_text' => ''];
}
/**
* {@inheritdoc}
*/
public function isHidden() {
return FALSE;
}
/**
* {@inheritdoc}
*/
public function isLocked() {
return FALSE;
}
}
......@@ -3,6 +3,7 @@
namespace Drupal\Tests\facets_summary\Functional;
use Drupal\Tests\facets\Functional\FacetsTestBase;
use Drupal\facets_summary\Entity\FacetsSummary;
use Drupal\views\Views;
use Symfony\Component\DependencyInjection\ContainerInterface;
......@@ -86,6 +87,8 @@ class IntegrationTest extends FacetsTestBase {
// Enable a facet and check it's status after saving.
$this->drupalPostForm(NULL, ['facets[llama][checked]' => TRUE], 'Save');
$this->assertFieldChecked('edit-facets-llama-checked');
$this->configureResetFacetsProcessor();
}
/**
......@@ -170,6 +173,8 @@ class IntegrationTest extends FacetsTestBase {
->findById('block-' . $block->id())
->findAll('css', 'li');
$this->assertCount(2, $list_items);
$this->checkResetFacetsProcessor();
}
/**
......@@ -204,4 +209,72 @@ class IntegrationTest extends FacetsTestBase {
$this->assertEquals('none', $current_cache['type']);
}
/**
* Tests configuring reset facets processor.
*/
protected function configureResetFacetsProcessor() {
$this->assertSession()->checkboxNotChecked('edit-facets-summary-settings-reset-facets-status');
$this->drupalPostForm(NULL, ['facets_summary_settings[reset_facets][status]' => TRUE], 'Save');
$this->assertSession()->checkboxChecked('edit-facets-summary-settings-reset-facets-status');
$this->assertSession()->pageTextContains(t('Facets Summary Owl has been updated.'));
$this->assertSession()->fieldExists('facets_summary_settings[reset_facets][settings][link_text]');
$this->drupalPostForm(NULL, ['facets_summary_settings[reset_facets][settings][link_text]' => 'Reset facets'], 'Save');
$this->assertSession()->pageTextContains(t('Facets Summary Owl has been updated.'));
$this->assertSession()->fieldValueEquals('facets_summary_settings[reset_facets][settings][link_text]', 'Reset facets');
}
/**
* Tests reset facets processor.
*/
protected function checkResetFacetsProcessor() {
// Create new facets summary.
FacetsSummary::create([
'id' => 'reset_facets',
'name' => t('Reset facets summary'),
'facet_source_id' => 'search_api:views_page__search_api_test_view__page_1',
'facets' => [
'giraffe' => [
'checked' => 1,
'label' => 'Giraffe',
'separator' => ',',
'weight' => 0,
'show_count' => 0,
],
'llama' => [
'checked' => 1,
'label' => 'Llama',
'separator' => ',',
'weight' => 0,
'show_count' => 0,
],
],
'processor_configs' => [
'reset_facets' => [
'processor_id' => 'reset_facets',
'weights' => ['build' => -10],
'settings' => ['link_text' => 'Reset facets'],
],
],
])->save();
// Clear the cache after the new facet summary entity was created.
$this->resetAll();
// Place a block and test reset facets processor.
$this->drupalPlaceBlock('facets_summary_block:reset_facets', ['region' => 'footer', 'id' => 'reset-facets']);
$this->drupalGet('search-api-test-fulltext');
$this->assertSession()->pageTextContains(t('Displaying 5 search results'));
$this->assertSession()->pageTextNotContains(t('Reset facets'));
$this->clickLink('apple');
$this->assertSession()->pageTextContains(t('Displaying 2 search results'));
$this->assertSession()->pageTextContains(t('Reset facets'));
$this->clickLink(t('Reset facets'));
$this->assertSession()->pageTextContains(t('Displaying 5 search results'));
$this->assertSession()->pageTextNotContains(t('Reset facets'));
}
}
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