Commit 2ed48251 authored by dragos-dumi's avatar dragos-dumi Committed by borisson_

Issue #2878851 by dragos-dumi, borisson_: Facet summary "Show a summary of all...

Issue #2878851 by dragos-dumi, borisson_: Facet summary "Show a summary of all selected facets"  values
parent 872fd435
......@@ -29,11 +29,15 @@ class ShowSummaryProcessor extends ProcessorPluginBase implements BuildProcessor
if (isset($build['#items'])) {
/** @var \Drupal\facets\Entity\Facet $facet */
foreach ($facets as $facet) {
if (empty($facet->getActiveItems())) {
continue;
}
$items = $this->getActiveDisplayValues($facet->getResults());
$facet_summary = [
'#theme' => 'facets_summary_facet',
'#label' => $facets_config[$facet->id()]['label'],
'#separator' => $facets_config[$facet->id()]['separator'],
'#items' => $facet->getActiveItems(),
'#items' => $items,
'#facet_id' => $facet->id(),
'#facet_admin_label' => $facet->getName(),
];
......@@ -43,4 +47,25 @@ class ShowSummaryProcessor extends ProcessorPluginBase implements BuildProcessor
return $build;
}
/**
* Get all active results' display values from hierarchy.
*
* @param \Drupal\facets\Result\ResultInterface[] $results
* The results to check for active children.
*
* @return \Drupal\facets\Result\ResultInterface[]
* The active results found.
*/
protected function getActiveDisplayValues($results) {
$items = [];
foreach ($results as $result) {
if ($result->isActive()) {
$items[] = $result->getDisplayValue();
}
if ($result->hasActiveChildren()) {
$items = array_merge($items, $this->getActiveDisplayValues($result->getChildren()));
}
}
return $items;
}
}
......@@ -169,6 +169,58 @@ class IntegrationTest extends FacetsTestBase {
$this->checkResetFacetsProcessor();
}
/**
* Tests "Show a summary of all selected facets".
*
* Regression test for https://www.drupal.org/node/2878851.
*/
public function testShowSummary() {
// Create facets.
$this->createFacet('Giraffe', 'giraffe', 'keywords');
// Clear all the caches between building the 2 facets - because things fail
// otherwise.
$this->resetAll();
$this->createFacet('Llama', 'llama');
// Add a summary.
$values = [
'name' => 'Owlß',
'id' => 'owl',
'facet_source_id' => 'search_api:views_page__search_api_test_view__page_1',
];
$this->drupalPostForm('admin/config/search/facets/add-facet-summary', $values, 'Save');
// Edit the summary and enable the facets.
$summaries = [
'facets[giraffe][checked]' => TRUE,
'facets[giraffe][label]' => 'Summary giraffe',
'facets[llama][checked]' => TRUE,
'facets[llama][label]' => 'Summary llama',
'facets_summary_settings[show_summary][status]' => TRUE
];
$this->drupalPostForm(NULL, $summaries, 'Save');
$block = [
'region' => 'footer',
'id' => str_replace('_', '-', 'owl'),
'weight' => 50,
];
$block = $this->drupalPlaceBlock('facets_summary_block:owl', $block);
$this->drupalGet('search-api-test-fulltext');
$this->assertText('Displaying 5 search results');
$this->clickLink('item');
/** @var \Behat\Mink\Element\NodeElement[] $list_items */
$list_items = $this->getSession()
->getPage()
->findById('block-' . $block->id())
->findAll('css', 'li');
$this->assertCount(2, $list_items);
$this->assertEquals('Summary llama: item', $list_items[0]->getText());
$this->assertEquals('(-) item', $list_items[1]->getText());
}
/**
* Check that the disabling of the cache works.
*/
......
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