diff --git a/modules/core_search_facets/tests/src/Functional/IntegrationTest.php b/modules/core_search_facets/tests/src/Functional/IntegrationTest.php index 7f29a5b..c8defb9 100644 --- a/modules/core_search_facets/tests/src/Functional/IntegrationTest.php +++ b/modules/core_search_facets/tests/src/Functional/IntegrationTest.php @@ -107,8 +107,11 @@ public function testPostDate() { $this->blocks[$facet_id] = $this->createBlock($facet_id); $this->setShowAmountOfResults($facet_id, TRUE); - $this->drupalGet("admin/config/search/facets/$facet_id/edit"); - $this->drupalPostForm(NULL, ['widget' => 'datebasic'], 'Save'); + $edit = [ + 'facet_settings[date_item][status]' => TRUE, + 'facet_settings[date_item][settings][display_relative]' => '0', + ]; + $this->drupalPostForm("admin/config/search/facets/$facet_id/edit", $edit, 'Save'); // Assert date facets. $this->drupalGet('search/node', ['query' => ['keys' => 'test']]); @@ -146,8 +149,11 @@ public function testUpdatedDate() { $this->blocks[$facet_id] = $this->createBlock($facet_id); $this->setShowAmountOfResults($facet_id, TRUE); - $this->drupalGet("admin/config/search/facets/$facet_id/edit"); - $this->drupalPostForm(NULL, ['widget' => 'datebasic'], 'Save'); + $edit = [ + 'facet_settings[date_item][status]' => TRUE, + 'facet_settings[date_item][settings][display_relative]' => '0', + ]; + $this->drupalPostForm("admin/config/search/facets/$facet_id/edit", $edit, 'Save'); // Update the changed date. The nodes were created on February/March 2016 // and the changed date is June 3, 2016. diff --git a/src/Entity/Facet.php b/src/Entity/Facet.php index 5fc7d87..c90a2f0 100644 --- a/src/Entity/Facet.php +++ b/src/Entity/Facet.php @@ -475,14 +475,14 @@ public function getQueryType() { if ($widgetQueryType === NULL && count($processorQueryTypes) === 0) { return $this->pickQueryType($query_types, 'string'); } - // The widget has made a decision and processors have specific needs. - if ($widgetQueryType !== NULL && count($processorQueryTypes) === 0) { - return $this->pickQueryType($query_types, $widgetQueryType); - } // The widget has made no decision but the processors have made 1 decision. if ($widgetQueryType === NULL && count($processorQueryTypes) === 1) { return $this->pickQueryType($query_types, key($processorQueryTypes)); } + // The widget has made a decision and processors have specific needs. + if ($widgetQueryType !== NULL && count($processorQueryTypes) === 0) { + return $this->pickQueryType($query_types, $widgetQueryType); + } // The widget has made a decision, and so have the processors, but it's // the same. if ($widgetQueryType !== NULL && count($processorQueryTypes) === 1 && key($processorQueryTypes) === $widgetQueryType) { @@ -490,7 +490,7 @@ public function getQueryType() { } // Invalid choice. - throw new InvalidQueryTypeException("Invalid query type combination in widget / processors"); + throw new InvalidQueryTypeException("Invalid query type combination in widget / processors. Widget: {$widgetQueryType}, Processors: " . array_keys($processorQueryTypes) . "."); } /** diff --git a/src/Plugin/facets/query_type/SearchApiDate.php b/src/Plugin/facets/query_type/SearchApiDate.php index cf86935..1e0c769 100644 --- a/src/Plugin/facets/query_type/SearchApiDate.php +++ b/src/Plugin/facets/query_type/SearchApiDate.php @@ -109,11 +109,10 @@ protected function calculateRangeAbsolute($value) { $stopDate = $dateTime::createFromFormat('Y-m-d\TH:i:s', $value . ':59'); break; - case static::FACETAPI_DATE_SECOND: + default: $startDate = $dateTime::createFromFormat('Y-m-d\TH:i:s', $value); $stopDate = $dateTime::createFromFormat('Y-m-d\TH:i:s', $value); break; - } return [ @@ -176,11 +175,10 @@ protected function calculateRangeRelative($value) { $stopDate->sub(new \DateInterval('PT1S')); break; - case static::FACETAPI_DATE_SECOND: + default: $startDate = $dateTime::createFromFormat('Y-m-d\TH:i:s', $value); $stopDate = clone $startDate; break; - } return [ @@ -241,12 +239,12 @@ public function calculateResultFilterAbsolute($value) { $raw = $date->format('Y-m-d\TH:i'); break; - case static::FACETAPI_DATE_SECOND: + default: $format = 'd/m/Y H:i:s'; $raw = $date->format('Y-m-d\TH:i:s'); break; - } + $format = $date_display ? $date_display : $format; return [ 'display' => $date->format($format), @@ -362,7 +360,7 @@ public function calculateResultFilterRelative($value) { $raw = $date->format('Y-m-d\TH:i:s'); break; - case static::FACETAPI_DATE_SECOND: + default: $rounded = new \DateInterval('P' . $interval->y . 'Y' . $interval->m . 'M' . $interval->d . 'DT' . $interval->h . 'H' . $interval->i . $interval->s . 'S'); $display = $interval->y ? $this->formatPlural($interval->y, '1 year', '@count years') . ' ' : ''; $display .= $interval->m ? $this->formatPlural($interval->m, '1 month', '@count months') . ' ' : ''; @@ -384,7 +382,6 @@ public function calculateResultFilterRelative($value) { } $raw = $date->format('Y-m-d\TH:i:s'); break; - } return [ @@ -397,23 +394,32 @@ public function calculateResultFilterRelative($value) { * Retrieve configuration: Granularity to use. * * Default behaviour an integer for the steps that the facet works in. + * + * @return int + * The granularity for this config. */ protected function getGranularity() { - return $this->facet->getWidgetInstance()->getConfiguration()['granularity']; + return $this->getConfiguration()['granularity']; } /** * Retrieve configuration: If the date should be displayed relatively. + * + * @return bool + * Returns true if the display should be relative, false if it's absolute. */ protected function getDisplayRelative() { - return $this->facet->getWidgetInstance()->getConfiguration()['display_relative']; + return $this->getConfiguration()['display_relative']; } /** * Retrieve configuration: Date display format. + * + * @return string + * Returns the format. */ protected function getDateFormat() { - return $this->facet->getWidgetInstance()->getConfiguration()['date_display']; + return $this->getConfiguration()['date_display']; } } diff --git a/src/QueryType/QueryTypePluginManager.php b/src/QueryType/QueryTypePluginManager.php index 4859c9d..d37fbb4 100644 --- a/src/QueryType/QueryTypePluginManager.php +++ b/src/QueryType/QueryTypePluginManager.php @@ -18,4 +18,24 @@ public function __construct(\Traversable $namespaces, CacheBackendInterface $cac parent::__construct('Plugin/facets/query_type', $namespaces, $module_handler, 'Drupal\facets\QueryType\QueryTypeInterface', 'Drupal\facets\Annotation\FacetsQueryType'); } + /** + * {@inheritdoc} + */ + public function createInstance($plugin_id, array $configuration = []) { + /** @var \Drupal\facets\FacetInterface $facet */ + $facet = $configuration['facet']; + $processors = $facet->getProcessors(); + + if (!isset($processors['date_item'])) { + return parent::createInstance($plugin_id, $configuration); + } + + $dateProcessorConfig = $processors['date_item']->getConfiguration(); + $configuration['granularity'] = $dateProcessorConfig['granularity']; + $configuration['display_relative'] = $dateProcessorConfig['display_relative']; + $configuration['date_display'] = $dateProcessorConfig['date_display']; + + return parent::createInstance($plugin_id, $configuration); + } + } diff --git a/src/Widget/WidgetPluginBase.php b/src/Widget/WidgetPluginBase.php index fd1393f..b653d49 100644 --- a/src/Widget/WidgetPluginBase.php +++ b/src/Widget/WidgetPluginBase.php @@ -123,7 +123,7 @@ public function getConfiguration() { * {@inheritdoc} */ public function getQueryType() { - return 'string'; + return NULL; } /**