diff --git a/src/Plugin/facets/processor/GranularItemProcessor.php b/src/Plugin/facets/processor/GranularItemProcessor.php index 2f2a539..5329d32 100644 --- a/src/Plugin/facets/processor/GranularItemProcessor.php +++ b/src/Plugin/facets/processor/GranularItemProcessor.php @@ -34,7 +34,7 @@ public function build(FacetInterface $facet, array $results) { */ public function defaultConfiguration() { return [ - 'granularity' => 0, + 'granularity' => 1, ] + parent::defaultConfiguration(); } @@ -44,16 +44,14 @@ public function defaultConfiguration() { public function buildConfigurationForm(array $form, FormStateInterface $form_state, FacetInterface $facet) { $configuration = $this->getConfiguration(); - $form += parent::buildConfigurationForm($form, $form_state, $facet); - - $form['granularity'] = [ + $build['granularity'] = [ '#type' => 'number', '#title' => $this->t('Granularity'), '#default_value' => $configuration['granularity'], '#description' => $this->t('The numeric size of the steps to group the result facets in.'), ]; - return $form; + return $build; } /** diff --git a/src/Plugin/facets/query_type/SearchApiGranular.php b/src/Plugin/facets/query_type/SearchApiGranular.php index b41594e..9eed200 100644 --- a/src/Plugin/facets/query_type/SearchApiGranular.php +++ b/src/Plugin/facets/query_type/SearchApiGranular.php @@ -42,11 +42,11 @@ public function calculateResultFilter($value) { * * Default behaviour an integer for the steps that the facet works in. * - * @return mixed + * @return int * If not an integer the inheriting class needs to deal with calculations. */ protected function getGranularity() { - return $this->facet->getWidgetInstance()->getConfiguration()['granularity']; + return $this->facet->getProcessors()['granularity_item']->getConfiguration()['granularity']; } } diff --git a/tests/src/Functional/ProcessorIntegrationTest.php b/tests/src/Functional/ProcessorIntegrationTest.php index 0027fe2..e79c8aa 100644 --- a/tests/src/Functional/ProcessorIntegrationTest.php +++ b/tests/src/Functional/ProcessorIntegrationTest.php @@ -201,6 +201,96 @@ public function testBooleanProcessorIntegration() { $this->assertFacetLabel('Øn'); } + /** + * Tests the for configuration of granularity processor. + */ + public function testNumericGranularity() { + $field_name = 'field_integer'; + $field_storage = FieldStorageConfig::create([ + 'field_name' => $field_name, + 'entity_type' => 'entity_test_mulrev_changed', + 'type' => 'integer', + ]); + $field_storage->save(); + $field = FieldConfig::create([ + 'field_storage' => $field_storage, + 'bundle' => 'item', + ]); + $field->save(); + $field = FieldConfig::create([ + 'field_storage' => $field_storage, + 'bundle' => 'article', + ]); + $field->save(); + + $index = $this->getIndex(); + + // Index the taxonomy and entity reference fields. + $integerField = new Field($index, $field_name); + $integerField->setType('integer'); + $integerField->setPropertyPath($field_name); + $integerField->setDatasourceId('entity:entity_test_mulrev_changed'); + $integerField->setLabel('IntegerField'); + $index->addField($integerField); + + $index->save(); + $this->indexItems($this->indexId); + + $entity_test_storage = \Drupal::entityTypeManager() + ->getStorage('entity_test_mulrev_changed'); + + foreach ([30, 35, 40, 100] as $val) { + $entity_test_storage->create([ + 'name' => 'foo bar baz', + 'body' => 'test test int', + 'type' => 'item', + 'keywords' => ['orange'], + 'category' => 'item_category', + $field_name => $val, + ])->save(); + } + + $this->indexItems($this->indexId); + + $facet_id = "integer"; + + // Create facet. + $this->editForm = 'admin/config/search/facets/' . $facet_id . '/edit'; + $this->createFacet("Integer", $facet_id, $field_name); + + // Check values. + $this->drupalGet('search-api-test-fulltext'); + $this->assertFacetLabel('100'); + $this->assertFacetLabel('30'); + $this->assertFacetLabel('35'); + $this->assertFacetLabel('40'); + + $form = [ + 'facet_settings[granularity_item][status]' => TRUE, + ]; + $this->drupalPostForm($this->editForm, $form, 'Save'); + + // Check values. + $this->drupalGet('search-api-test-fulltext'); + $this->assertFacetLabel('30 (1)'); + $this->assertFacetLabel('35'); + $this->assertFacetLabel('40'); + $this->assertFacetLabel('100'); + + $form = [ + 'facet_settings[granularity_item][status]' => TRUE, + 'facet_settings[granularity_item][settings][granularity]' => 10, + ]; + $this->drupalPostForm($this->editForm, $form, 'Save'); + + // Check values. + $this->drupalGet('search-api-test-fulltext'); + $this->assertFacetLabel('30 (2)'); + $this->assertSession()->pageTextNotContains('35'); + $this->assertFacetLabel('40'); + $this->assertFacetLabel('100'); + } + /** * Tests the for sorting processors in the frontend with a 'keywords' facet. */