Commit ecffe406 authored by borisson_'s avatar borisson_ Committed by borisson_

Issue #2873523 by borisson_: Facet summary show count should work independant...

Issue #2873523 by borisson_: Facet summary show count should work independant from the facet show count setting
parent 13c29d16
......@@ -84,6 +84,7 @@ class DefaultFacetsSummaryManager {
// Let the facet_manager build the facets.
$facetsource_id = $facets_summary->getFacetSourceId();
/** @var \Drupal\facets\Entity\Facet[] $facets */
$facets = $this->facetManager->getFacetsByFacetSourceId($facetsource_id);
// Get the current results from the facets and let all processors that
// trigger on the build step do their build processing.
......@@ -104,7 +105,6 @@ class DefaultFacetsSummaryManager {
// The first facet therefor will trigger the processing. Note that
// processing is done only once, so repeatedly calling this method will
// not trigger the processing more than once.
$this->facetManager->processFacets($facetsource_id);
$this->facetManager->build($facet);
}
......@@ -116,44 +116,11 @@ class DefaultFacetsSummaryManager {
];
$results = [];
// Go through each facet and get the results. After, check if we have to
// show the counts for each facet and respectively set those to NULL if this
// should not be shown. We do that here so that we can use our sort
// processors on all the facet items accordingly.
foreach ($facets as $facet) {
$facet_results = $facet->getResults();
$show_count = $facets_config[$facet->id()]['show_count'];
if (!$show_count) {
foreach ($facet_results as $facet_result_id => $facet_result) {
$facet_results[$facet_result_id]->setCount(NULL);
}
}
$results = array_merge($facet_results, $results);
}
// Trigger sort stage.
$active_sort_processors = [];
foreach ($facets_summary->getProcessorsByStage(ProcessorInterface::STAGE_SORT) as $processor) {
$active_sort_processors[] = $processor;
$results = array_merge($results, $this->buildResultTree($show_count, $facet->getResults()));
}
// Sort the results.
uasort($results, function ($a, $b) use ($active_sort_processors) {
$return = 0;
/** @var \Drupal\facets_summary\Processor\SortProcessorPluginBase $sort_processor */
foreach ($active_sort_processors as $sort_processor) {
if ($return = $sort_processor->sortResults($a, $b)) {
if ($sort_processor->getConfiguration()['sort'] == 'DESC') {
$return *= -1;
}
break;
}
}
return $return;
});
$build['#items'] = $this->buildResultTree($results);
$build['#items'] = $results;
// Allow our Facets Summary processors to alter the build array in a
// configured order.
......@@ -170,20 +137,22 @@ class DefaultFacetsSummaryManager {
/**
* Build result tree, taking possible children into account.
*
* @param array $results
* @param bool $show_count
* Show the count next to the facet.
* @param \Drupal\facets\Result\ResultInterface[] $results
* Facet results array.
*
* @return array
* Facet render items.
* The rendered links to the active facets.
*/
protected function buildResultTree($results) {
protected function buildResultTree($show_count, $results) {
$items = [];
foreach ($results as $result) {
if ($result->isActive()) {
$item = [
'#theme' => 'facets_result_item',
'#value' => $result->getDisplayValue(),
'#show_count' => $result->getCount() !== NULL,
'#show_count' => $show_count,
'#count' => $result->getCount(),
'#is_active' => TRUE,
];
......@@ -191,7 +160,7 @@ class DefaultFacetsSummaryManager {
$items[] = $item;
}
if ($children = $result->getChildren()) {
$items = array_merge($items, $this->buildResultTree($children));
$items = array_merge($items, $this->buildResultTree($show_count, $children));
}
}
return $items;
......
......@@ -27,7 +27,7 @@ class FacetsSummaryBlock extends BlockBase implements FacetsSummaryBlockInterfac
*
* @var \Drupal\facets_summary\FacetsSummaryManager\DefaultFacetsSummaryManager
*/
protected $facetManager;
protected $facetsSummaryManager;
/**
* The associated facets_source_summary entity.
......
......@@ -202,6 +202,79 @@ class IntegrationTest extends FacetsTestBase {
$this->assertEquals('none', $current_cache['type']);
}
/**
* Tests counts for summaries.
*
* @see https://www.drupal.org/node/2873523
*/
public function testCount() {
// Create facets.
$this->createFacet('Otter', 'otter', 'keywords');
// Clear all the caches between building the 2 facets - because things fail
// otherwise.
$this->resetAll();
$this->createFacet('Wolverine', 'wolverine');
// Make sure the numbers are shown with the facets.
$edit = [
'widget' => 'links',
'widget_config[show_numbers]' => '1',
];
$this->drupalPostForm('admin/config/search/facets/otter/edit', $edit, 'Save');
$this->drupalPostForm('admin/config/search/facets/wolverine/edit', $edit, 'Save');
// Add a summary.
$values = [
'name' => 'Mustelidae',
'id' => 'mustelidae',
'facet_source_id' => 'search_api:views_page__search_api_test_view__page_1',
];
$this->drupalPostForm('admin/config/search/facets/add-facet-summary', $values, 'Save');
// Configure the summary to hide the count.
$summaries = [
'facets[otter][checked]' => TRUE,
'facets[otter][label]' => 'Summary giraffe',
'facets[otter][show_count]' => FALSE,
'facets[wolverine][checked]' => TRUE,
'facets[wolverine][label]' => 'Summary llama',
'facets[wolverine][show_count]' => FALSE,
];
$this->drupalPostForm(NULL, $summaries, 'Save');
// Place the block.
$block = [
'region' => 'footer',
'id' => str_replace('_', '-', 'owl'),
'weight' => 50,
];
$summary_block = $this->drupalPlaceBlock('facets_summary_block:mustelidae', $block);
$this->drupalGet('search-api-test-fulltext');
$webAssert = $this->assertSession();
$webAssert->pageTextContains('Displaying 5 search results');
$this->assertFacetBlocksAppear();
$webAssert->pageTextContains($summary_block->label());
$this->assertFacetLabel('article (2)');
$this->assertFacetLabel('apple (2)');
$summaries = [
'facets[otter][show_count]' => TRUE,
'facets[wolverine][show_count]' => TRUE,
];
$this->drupalPostForm('admin/config/search/facets/facet-summary/mustelidae/edit', $summaries, 'Save');
$this->drupalGet('search-api-test-fulltext');
$webAssert = $this->assertSession();
$webAssert->pageTextContains('Displaying 5 search results');
$this->assertFacetBlocksAppear();
$webAssert->pageTextContains($summary_block->label());
$this->assertFacetLabel('article (2)');
$this->assertFacetLabel('apple (2)');
}
/**
* Tests configuring show_count processor.
*/
......
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