summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorborisson_2017-07-07 08:10:52 (GMT)
committerJoris Vercammen2017-07-07 08:10:52 (GMT)
commitecffe4063fee69853e78d6640bf77e7f3df58bf7 (patch)
treed3a5249d19d03051ff5ba04423d324746f87a6fc
parent13c29d16e2755a39322fd5ca9e27c31baf74d4a5 (diff)
Issue #2873523 by borisson_: Facet summary show count should work independant from the facet show count setting8.x-1.0-alpha10
-rw-r--r--modules/facets_summary/src/FacetsSummaryManager/DefaultFacetsSummaryManager.php51
-rw-r--r--modules/facets_summary/src/Plugin/Block/FacetsSummaryBlock.php2
-rw-r--r--modules/facets_summary/tests/src/Functional/IntegrationTest.php73
3 files changed, 84 insertions, 42 deletions
diff --git a/modules/facets_summary/src/FacetsSummaryManager/DefaultFacetsSummaryManager.php b/modules/facets_summary/src/FacetsSummaryManager/DefaultFacetsSummaryManager.php
index 16c9b99..21a9544 100644
--- a/modules/facets_summary/src/FacetsSummaryManager/DefaultFacetsSummaryManager.php
+++ b/modules/facets_summary/src/FacetsSummaryManager/DefaultFacetsSummaryManager.php
@@ -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;
diff --git a/modules/facets_summary/src/Plugin/Block/FacetsSummaryBlock.php b/modules/facets_summary/src/Plugin/Block/FacetsSummaryBlock.php
index 42b4b32..93ccd99 100644
--- a/modules/facets_summary/src/Plugin/Block/FacetsSummaryBlock.php
+++ b/modules/facets_summary/src/Plugin/Block/FacetsSummaryBlock.php
@@ -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.
diff --git a/modules/facets_summary/tests/src/Functional/IntegrationTest.php b/modules/facets_summary/tests/src/Functional/IntegrationTest.php
index 5f21559..d42512e 100644
--- a/modules/facets_summary/tests/src/Functional/IntegrationTest.php
+++ b/modules/facets_summary/tests/src/Functional/IntegrationTest.php
@@ -203,6 +203,79 @@ class IntegrationTest extends FacetsTestBase {
}
/**
+ * 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.
*/
protected function configureShowCountProcessor() {