Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OPENEUROPA-3422: Full-text facets to also show as selected filter. #32

Merged
merged 3 commits into from
Oct 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 17 additions & 9 deletions src/ListBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Drupal\facets\Utility\FacetsUrlGenerator;
use Drupal\oe_list_pages\Form\ListFacetsForm;
use Drupal\oe_list_pages\Plugin\facets\processor\DefaultStatusProcessorInterface;
use Drupal\oe_list_pages\Plugin\facets\widget\FulltextWidget;

/**
* Default list builder implementation.
Expand Down Expand Up @@ -246,14 +247,16 @@ public function buildSelectedFilters(ContentEntityInterface $entity): array {
continue;
}

if (!$facet->getResults()) {
// If the facet doesn't have results, we cannot get any display value
// later on. Except for the facets which use a full text widget because
// we can use the actual submitted value.
if (!$facet->getResults() && !$facet->getWidgetInstance() instanceof FulltextWidget) {
continue;
}

$keyed_facets[$facet->id()] = $facet;
$items = $facet->getActiveItems();
$cache->addCacheableDependency($facet);
$active_filters[$facet->id()] = $items;
$active_filters[$facet->id()] = $facet->getActiveItems();
}

if (!$active_filters) {
Expand Down Expand Up @@ -287,10 +290,9 @@ public function buildSelectedFilters(ContentEntityInterface $entity): array {
$items = [];
foreach ($active_filters as $facet_id => $filters) {
$facet = $keyed_facets[$facet_id];
$facet_results = $facet->getResults();
$item = [];
foreach ($filters as $key => $value) {
$display_value = $this->getFacetResultDisplayLabel($facet_results, $value);
$display_value = $this->getFacetResultDisplayLabel($facet, $value);
if (!$display_value) {
continue;
}
Expand Down Expand Up @@ -377,16 +379,22 @@ public function buildPagerInfo(ContentEntityInterface $entity): array {
/**
* Returns the display label of a facet result.
*
* @param \Drupal\facets\Result\Result[] $facet_results
* All the results.
* @param \Drupal\facets\FacetInterface $facet
* The facet.
* @param string $value
* The raw value.
*
* @return string|null
* The display value.
*/
protected function getFacetResultDisplayLabel(array $facet_results, string $value): ?string {
foreach ($facet_results as $facet_result) {
protected function getFacetResultDisplayLabel(FacetInterface $facet, string $value): ?string {
if ($facet->getWidgetInstance() instanceof FulltextWidget) {
// For facets that use the full text widget, the actual value is the
// selected item.
return $value;
}

foreach ($facet->getResults() as $facet_result) {
if ($facet_result->getRawValue() === $value) {
return (string) $facet_result->getDisplayValue();
}
Expand Down
6 changes: 3 additions & 3 deletions src/Plugin/facets/processor/DateUrlProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ public function build(FacetInterface $facet, array $results) {
if ($operator === 'bt') {
$display = new FormattableMarkup('@operator @first and @second', [
'@operator' => $operators[$operator],
'@first' => $first_date->format('d F Y'),
'@second' => $second_date->format('d F Y'),
'@first' => $first_date->format('j F Y'),
'@second' => $second_date->format('j F Y'),
]);
$result = new Result($facet, $active_filters['_raw'], $display, 0);
$facet_results[] = $result;
Expand All @@ -69,7 +69,7 @@ public function build(FacetInterface $facet, array $results) {

$display = new FormattableMarkup('@operator @first', [
'@operator' => $operators[$operator],
'@first' => $first_date->format('d F Y'),
'@first' => $first_date->format('j F Y'),
]);
$result = new Result($facet, $active_filters['_raw'], $display, 0);
$facet_results[] = $result;
Expand Down
35 changes: 35 additions & 0 deletions tests/src/FunctionalJavascript/ListPagesFiltersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ public function testSelectedListPageFilters(): void {
$page->checkField('Select one');
$page->checkField('Published');
$page->checkField('Period');
$page->checkField('Body');
$page->checkField('Created');
$page->fillField('Title', 'List page for ct1');
$page->pressButton('Save');
Expand Down Expand Up @@ -292,6 +293,40 @@ public function testSelectedListPageFilters(): void {
$this->assertSession()->pageTextContains('another yellow fruit');
$this->assertSession()->linkExistsExact('Between 19 October 2019 and 26 October 2019');

// Test that the full text widget-based filter shows also the selected
// value.
$this->getSession()->getPage()->pressButton('Clear filters');
$this->getSession()->getPage()->fillField('Body', 'banana');
$this->getSession()->getPage()->selectFieldOption('Select one', 'test1', TRUE);
$this->getSession()->getPage()->pressButton('Search');
$this->assertSelectedFiltersLabels(['Body', 'Period', 'Select one']);
$this->assertSession()->linkExistsExact('banana');
$this->assertSession()->linkExistsExact('test1');
$this->assertSession()->pageTextContains('one yellow fruit');
$this->assertSession()->pageTextNotContains('another yellow fruit');
// Remove the select field and assert we still show the link with the
// text filter.
$this->getSession()->getPage()->clickLink('test1');
$this->assertSelectedFiltersLabels(['Body', 'Period']);
$this->assertSession()->linkExistsExact('banana');
$this->assertSession()->linkNotExistsExact('test1');
$this->assertSession()->pageTextContains('one yellow fruit');
$this->assertSession()->pageTextNotContains('another yellow fruit');
// Add back the select and remove the text filter.
$this->getSession()->getPage()->selectFieldOption('Select one', 'test1', TRUE);
$this->getSession()->getPage()->pressButton('Search');
$this->assertSelectedFiltersLabels(['Body', 'Period', 'Select one']);
$this->assertSession()->linkExistsExact('banana');
$this->assertSession()->linkExistsExact('test1');
$this->assertSession()->pageTextContains('one yellow fruit');
$this->assertSession()->pageTextNotContains('another yellow fruit');
$this->getSession()->getPage()->clickLink('banana');
$this->assertSelectedFiltersLabels(['Period', 'Select one']);
$this->assertSession()->linkNotExistsExact('banana');
$this->assertSession()->linkExistsExact('test1');
$this->assertSession()->pageTextContains('one yellow fruit');
$this->assertSession()->pageTextNotContains('another yellow fruit');

// Test the period filter with a default status.
$this->getSession()->getPage()->pressButton('Clear filters');
$this->getSession()->getPage()->selectFieldOption('Published', 'Yes', TRUE);
Expand Down