diff --git a/src/Plugin/facets/query_type/SearchApiString.php b/src/Plugin/facets/query_type/SearchApiString.php index 27930e0..63f19b2 100644 --- a/src/Plugin/facets/query_type/SearchApiString.php +++ b/src/Plugin/facets/query_type/SearchApiString.php @@ -99,15 +99,15 @@ class SearchApiString extends QueryTypePluginBase { if ($query_operator === 'OR') { $unfiltered_results = $this->facet->getUnfilteredResults(); $field_identifier = $this->facet->getFieldIdentifier(); - foreach ($unfiltered_results[$field_identifier] as $unfiltered_result) { - if ($unfiltered_result['filter'] === $result['filter']) { + if ($unfiltered_result['filter'] === $result['filter'] && (bool) $result['count']) { $count = $unfiltered_result['count']; } } } - $result = new Result(trim($result['filter'], '"'), trim($result['filter'], '"'), $count); + $filter = trim($result['filter'], '"'); + $result = new Result($filter, $filter, $count); $facet_results[] = $result; } } diff --git a/src/Tests/IntegrationTest.php b/src/Tests/IntegrationTest.php index bc71790..4e787cb 100644 --- a/src/Tests/IntegrationTest.php +++ b/src/Tests/IntegrationTest.php @@ -455,6 +455,56 @@ class IntegrationTest extends WebTestBase { } /** + * Tests calculations of facet count. + */ + public function testFacetCountCalculations() { + $this->addFacet('Type'); + $this->addFacet('Keywords', 'keywords'); + $this->createBlock('type'); + $this->createBlock('keywords'); + + $this->drupalGet('admin/config/search/facets/keywords/edit'); + $edit = [ + 'widget' => 'links', + 'widget_configs[show_numbers]' => '1', + ]; + $this->drupalPostForm(NULL, $edit, $this->t('Save')); + $this->drupalGet('admin/config/search/facets/type/edit'); + $edit = [ + 'widget' => 'links', + 'widget_configs[show_numbers]' => '1', + ]; + $this->drupalPostForm(NULL, $edit, $this->t('Save')); + + $this->drupalGet('search-api-test-fulltext'); + $this->assertText('Displaying 5 search results'); + $this->assertText('article (2)'); + $this->assertText('grape (3)'); + + // Make sure that after clicking on article, which has only 2 entities, + // there are only 2 items left in the results for other facets as well. + // In this case, that means we can't have 3 entities tagged with grape. Both + // remaining entities are tagged with grape and strawberry. + $this->clickLinkPartialName('article'); + $this->assertNoText('grape (3)'); + $this->assertText('grape (2)'); + $this->assertText('strawberry (2)'); + + $this->drupalGet('search-api-test-fulltext'); + $this->assertText('Displaying 5 search results'); + $this->assertText('article (2)'); + $this->assertText('grape (3)'); + + // Make sure that after clicking on grape, which has only 3 entities, there + // are only 3 items left in the results for other facets as well. In this + // case, that means 2 entities of type article and 1 item. + $this->clickLinkPartialName('grape'); + $this->assertText('Displaying 3 search results'); + $this->assertText('article (2)'); + $this->assertText('item (1)'); + } + + /** * Configures empty behavior option to show a text on empty results. * * @param string $facet_name