SearchApiGranular.php in Facets 8
File
src/Plugin/facets/query_type/SearchApiGranular.php
View source
<?php
namespace Drupal\facets\Plugin\facets\query_type;
use Drupal\facets\QueryType\QueryTypeRangeBase;
use Drupal\facets\Result\Result;
class SearchApiGranular extends QueryTypeRangeBase {
public function calculateRange($value) {
return [
'start' => $value,
'stop' => (int) $value + $this
->getGranularity(),
];
}
public function build() {
if (empty($this->results)) {
return $this->facet;
}
$supportedFeatures = array_flip($this->query
->getIndex()
->getServerInstance()
->getBackend()
->getSupportedFeatures());
if (isset($supportedFeatures['search_api_granular'])) {
$query_operator = $this->facet
->getQueryOperator();
$facet_results = [];
foreach ($this->results as $result) {
if ($result['count'] || $query_operator == 'or') {
$result_filter = trim($result['filter'], '"');
$facet_results[] = new Result($this->facet, $result_filter, $result_filter, $result['count']);
}
}
$this->facet
->setResults($facet_results);
return $this->facet;
}
return parent::build();
}
public function calculateResultFilter($value) {
assert($this
->getGranularity() > 0);
$min_value = (int) $this
->getMinValue();
$max_value = $this
->getMaxValue();
$granularity = $this
->getGranularity();
if ($value < $min_value || !empty($max_value) && $value > $max_value + $granularity - 1) {
return FALSE;
}
return [
'display' => $value - fmod($value - $min_value, $this
->getGranularity()),
'raw' => $value - fmod($value - $min_value, $this
->getGranularity()),
];
}
protected function getFacetOptions() {
return $this->facet
->getProcessors()['granularity_item']
->getConfiguration() + parent::getFacetOptions();
}
protected function getGranularity() {
return $this->facet
->getProcessors()['granularity_item']
->getConfiguration()['granularity'];
}
protected function getMinValue() {
return $this->facet
->getProcessors()['granularity_item']
->getConfiguration()['min_value'];
}
protected function getMaxValue() {
return $this->facet
->getProcessors()['granularity_item']
->getConfiguration()['max_value'];
}
}