Commit 6cbf1565 authored by borisson_'s avatar borisson_ Committed by StryKaizer
Browse files

Issue #2855320 by borisson_, StryKaizer, jummonk, jeni_dc: Provide facet id...

Issue #2855320 by borisson_, StryKaizer, jummonk, jeni_dc: Provide facet id and raw value in Facets summary result items
parent 881e6d14
......@@ -46,6 +46,8 @@ function facets_theme($existing, $type, $theme, $path) {
'facets_result_item' => [
'base hook' => 'facets_result_item',
'variables' => [
'facet' => NULL,
'raw_value' => '',
'value' => '',
'show_count' => FALSE,
'count' => NULL,
......@@ -54,6 +56,7 @@ function facets_theme($existing, $type, $theme, $path) {
],
'facets_item_list' => [
'variables' => [
'facet' => NULL,
'items' => [],
'title' => '',
'list_type' => 'ul',
......
......@@ -110,7 +110,7 @@ class CoreNodeSearchDate extends QueryTypePluginBase {
$date_gap = $date_handler->getDateGap($active_item['start']['iso'], $active_item['end']['iso']);
$gap = $date_handler->getNextDateGap($date_gap, $date_handler::FACETS_DATE_MINUTE);
$last_parent = '[' . $active_item['start']['iso'] . ' TO ' . $active_item['end']['iso'] . ']';
$result = new Result($last_parent, $date_handler->formatTimestamp($active_item['start']['timestamp'], $date_gap), NULL);
$result = new Result($this->facet, $last_parent, $date_handler->formatTimestamp($active_item['start']['timestamp'], $date_gap), NULL);
$result->setActiveState(TRUE);
// Sets the children for the current parent..
if ($parent) {
......@@ -166,14 +166,14 @@ class CoreNodeSearchDate extends QueryTypePluginBase {
$last_value = end($facet_results);
if ($last_value) {
if ($new_value != $last_value->getRawValue()) {
$facet_results[] = new Result($new_value, $date_handler->formatTimestamp($value, $gap), $count);
$facet_results[] = new Result($this->facet, $new_value, $date_handler->formatTimestamp($value, $gap), $count);
}
else {
$last_value->setCount($last_value->getCount() + 1);
}
}
else {
$facet_results[] = new Result($new_value, $date_handler->formatTimestamp($value, $gap), $count);
$facet_results[] = new Result($this->facet, $new_value, $date_handler->formatTimestamp($value, $gap), $count);
}
}
......
......@@ -82,7 +82,7 @@ class CoreNodeSearchString extends QueryTypePluginBase {
if (!empty($results)) {
$facet_results = [];
foreach ($results as $result) {
$facet_results[] = new Result($result->value, $result->value, $result->count);
$facet_results[] = new Result($this->facet, $result->value, $result->value, $result->count);
}
$this->facet->setResults($facet_results);
}
......
......@@ -59,7 +59,7 @@ class SliderProcessor extends ProcessorPluginBase implements PostQueryProcessorI
$new_results = [];
for ($i = $min; $i <= $max; $i += $step) {
$count = isset($simple_results['f_' . $i]) ? $simple_results['f_' . $i]['count'] : 0;
$new_results[] = new Result((float) $i, (float) $i, $count);
$new_results[] = new Result($facet, (float) $i, (float) $i, $count);
}
// Overwrite the current facet values with the generated results.
......
......@@ -75,8 +75,8 @@ class RangeSliderProcessorTest extends UnitTestCase {
/** @var \Drupal\facets\Result\ResultInterface[] $results */
$results = [
new Result(1, 1, 1),
new Result(5, 5, 5),
new Result($facet->reveal(), 1, 1, 1),
new Result($facet->reveal(), 5, 5, 5),
];
$results[0]->setUrl(new Url('test'));
$results[1]->setUrl(new Url('test'));
......
......@@ -44,8 +44,8 @@ class SliderProcessorTest extends UnitTestCase {
$facet->setWidget('raw', $widgetconfig);
$this->configureContainer($widgetconfig);
$result_lower = new Result(5, '5', 1);
$result_higher = new Result(150, '150', 1);
$result_lower = new Result($facet, 5, '5', 1);
$result_higher = new Result($facet, 150, '150', 1);
$facet->setResults([$result_lower, $result_higher]);
// Process the data.
......@@ -76,11 +76,11 @@ class SliderProcessorTest extends UnitTestCase {
$facet->setWidget('raw', $widgetconfig);
$this->configureContainer($widgetconfig);
$original_results[] = new Result(1, 'Small', 5);
$original_results[] = new Result($facet, 1, 'Small', 5);
foreach (range(100, 100000, 10) as $k) {
$original_results[] = new Result($k, 'result ' . $k, 1);
$original_results[] = new Result($facet, $k, 'result ' . $k, 1);
}
$original_results[] = new Result(150000, 'Big', 5);
$original_results[] = new Result($facet, 150000, 'Big', 5);
$facet->setResults($original_results);
// Process the data.
......@@ -108,7 +108,7 @@ class SliderProcessorTest extends UnitTestCase {
$original_results = [];
foreach ([10, 100, 200, 5] as $k) {
$original_results[] = new Result($k, 'result ' . $k, 1);
$original_results[] = new Result($facet, $k, 'result ' . $k, 1);
}
$facet->setResults($original_results);
......@@ -137,8 +137,8 @@ class SliderProcessorTest extends UnitTestCase {
$facet->setWidget('raw', $widgetconfig);
$this->configureContainer($widgetconfig);
$result_lower = new Result(5, '5', 1);
$result_higher = new Result(150, '150', 1);
$result_lower = new Result($facet, 5, '5', 1);
$result_higher = new Result($facet, 150, '150', 1);
$facet->setResults([$result_lower, $result_higher]);
// Process the data.
......@@ -159,8 +159,8 @@ class SliderProcessorTest extends UnitTestCase {
$facet->setWidget('raw', $widgetconfig);
$this->configureContainer($widgetconfig);
$result_lower = new Result(5, '5', 4);
$result_higher = new Result(15, '15', 4);
$result_lower = new Result($facet, 5, '5', 4);
$result_higher = new Result($facet, 15, '15', 4);
$facet->setResults([$result_lower, $result_higher]);
// Process the data.
......
......@@ -78,16 +78,16 @@ class SliderWidgetTest extends WidgetTestBase {
$container->set('url_generator', $url_generator->reveal());
\Drupal::setContainer($container);
$facet = new Facet(['id' => 'barn_owl'], 'facets_facet');
$originalResults = [];
foreach (range(3, 20000, 2) as $rv) {
$res = new Result($rv, 'Value: ' . $rv, ceil($rv / 2));
$res = new Result($facet, $rv, 'Value: ' . $rv, ceil($rv / 2));
$res->setUrl(new Url('test'));
$originalResults[] = $res;
}
$this->originalResults = $originalResults;
$facet = new Facet(['id' => 'barn_owl'], 'facets_facet');
$facet->setResults($this->originalResults);
$facet->setFieldIdentifier('owl');
$facet->setWidget('slider', []);
......
......@@ -156,6 +156,8 @@ class DefaultFacetsSummaryManager {
'#show_count' => $show_count,
'#count' => $result->getCount(),
'#is_active' => TRUE,
'#facet' => $result->getFacet(),
'#raw_value' => $result->getRawValue(),
];
$item = (new Link($item, $result->getUrl()))->toRenderable();
$items[] = $item;
......
......@@ -71,7 +71,7 @@ class SearchApiRange extends QueryTypePluginBase {
if ($result['count'] || $query_operator == 'or') {
$count = $result['count'];
$result_filter = trim($result['filter'], '"');
$result = new Result($result_filter, $result_filter, $count);
$result = new Result($this->facet, $result_filter, $result_filter, $count);
$facet_results[] = $result;
}
}
......
......@@ -76,7 +76,7 @@ class SearchApiString extends QueryTypePluginBase {
if ($result['count'] || $query_operator == 'or') {
$count = $result['count'];
$result_filter = trim($result['filter'], '"');
$result = new Result($result_filter, $result_filter, $count);
$result = new Result($this->facet, $result_filter, $result_filter, $count);
$facet_results[] = $result;
}
}
......
......@@ -82,7 +82,7 @@ abstract class QueryTypeRangeBase extends QueryTypePluginBase {
);
}
else {
$facet_results[$result_filter['raw']] = new Result($result_filter['raw'], $result_filter['display'], $count);
$facet_results[$result_filter['raw']] = new Result($this->facet, $result_filter['raw'], $result_filter['display'], $count);
}
}
}
......
......@@ -3,12 +3,20 @@
namespace Drupal\facets\Result;
use Drupal\Core\Url;
use Drupal\facets\FacetInterface;
/**
* The default implementation of the result interfaces.
*/
class Result implements ResultInterface {
/**
* The facet related to the result.
*
* @var \Drupal\facets\FacetInterface
*/
protected $facet;
/**
* The facet value.
*
......@@ -54,6 +62,8 @@ class Result implements ResultInterface {
/**
* Constructs a new result value object.
*
* @param \Drupal\facets\FacetInterface $facet
* The facet related to the result.
* @param mixed $raw_value
* The raw value.
* @param mixed $display_value
......@@ -61,7 +71,8 @@ class Result implements ResultInterface {
* @param int|null $count
* The amount of items or NULL.
*/
public function __construct($raw_value, $display_value, $count) {
public function __construct(FacetInterface $facet, $raw_value, $display_value, $count) {
$this->facet = $facet;
$this->rawValue = $raw_value;
$this->displayValue = $display_value;
$this->count = (int) $count;
......@@ -159,4 +170,11 @@ class Result implements ResultInterface {
return FALSE;
}
/**
* {@inheritdoc}
*/
public function getFacet() {
return $this->facet;
}
}
......@@ -9,6 +9,14 @@ use Drupal\Core\Url;
*/
interface ResultInterface {
/**
* Returns the facet related to the result.
*
* @return \Drupal\facets\FacetInterface
* The facet related to the result.
*/
public function getFacet();
/**
* Returns the raw value as present in the index.
*
......
......@@ -62,6 +62,7 @@ abstract class WidgetPluginBase extends PluginBase implements WidgetPluginInterf
return [
'#theme' => $this->getFacetItemListThemeHook($facet),
'#facet' => $facet,
'#items' => $items,
'#attributes' => [
'data-drupal-facet-id' => $facet->id(),
......@@ -232,6 +233,8 @@ abstract class WidgetPluginBase extends PluginBase implements WidgetPluginInterf
'#value' => $result->getDisplayValue(),
'#show_count' => $this->getConfiguration()['show_numbers'] && ($count !== NULL),
'#count' => $count,
'#facet' => $result->getFacet(),
'#raw_value' => $result->getRawValue(),
];
}
......
......@@ -15,6 +15,9 @@
* string or render array.
* - context: A list of contextual data associated with the list. May contain:
* - list_style: The ID of the widget plugin this facet uses.
* - facet: The facet for this result item.
* - id: the machine name for the facet.
* - label: The facet label.
*
* @see facets_preprocess_facets_item_list()
*
......
......@@ -5,9 +5,13 @@
*
* Available variables:
* - value: The item value.
* - raw_value: The raw item value.
* - show_count: If this facet provides count.
* - count: The amount of results.
* - is_active: The item is active.
* - facet: The facet for this result item.
* - id: the machine name for the facet.
* - label: The facet label.
*
* @ingroup themeable
*/
......
......@@ -248,14 +248,14 @@ class FacetTest extends KernelTestBase {
* @covers ::isActiveValue
*/
public function testResults() {
$entity = new Facet([], 'facets_facet');
/** @var \Drupal\facets\Result\ResultInterface[] $results */
$results = [
new Result('llama', 'llama', 10),
new Result('badger', 'badger', 15),
new Result('owl', 'owl', 5),
new Result($entity, 'llama', 'llama', 10),
new Result($entity, 'badger', 'badger', 15),
new Result($entity, 'owl', 'owl', 5),
];
$entity = new Facet([], 'facets_facet');
$this->assertEmpty($entity->getResults());
$entity->setResults($results);
......
......@@ -2,6 +2,7 @@
namespace Drupal\Tests\facets\Unit\Plugin\processor;
use Drupal\facets\Entity\Facet;
use Drupal\facets\Plugin\facets\processor\ActiveWidgetOrderProcessor;
use Drupal\facets\Result\Result;
use Drupal\Tests\UnitTestCase;
......@@ -33,13 +34,14 @@ class ActiveWidgetOrderProcessorTest extends UnitTestCase {
protected function setUp() {
parent::setUp();
$facet = new Facet([], 'facets_facet');
/** @var \Drupal\facets\Result\Result[] $original_results */
$original_results = [
new Result('Boxer', 'Boxer', 10),
new Result('Old Major', 'Old Major', 3),
new Result('Minimus', 'Minimus', 60),
new Result('Mr Whymper', 'Mr. Whymper', 1),
new Result('Clover', 'Clover', 50),
new Result($facet, 'Boxer', 'Boxer', 10),
new Result($facet, 'Old Major', 'Old Major', 3),
new Result($facet, 'Minimus', 'Minimus', 60),
new Result($facet, 'Mr Whymper', 'Mr. Whymper', 1),
new Result($facet, 'Clover', 'Clover', 50),
];
$original_results[1]->setActiveState(TRUE);
......
......@@ -34,9 +34,10 @@ class BooleanItemProcessorTest extends UnitTestCase {
protected function setUp() {
parent::setUp();
$facet = new Facet([], 'facets_facet');
$this->originalResults = [
new Result(0, 0, 10),
new Result(1, 1, 15),
new Result($facet, 0, 0, 10),
new Result($facet, 1, 1, 15),
];
$this->processor = new BooleanItemProcessor([], 'boolean_item_processor', []);
......
......@@ -35,10 +35,11 @@ class CountLimitProcessorTest extends UnitTestCase {
protected function setUp() {
parent::setUp();
$facet = new Facet([], 'facets_facet');
$this->originalResults = [
new Result('llama', 'llama', 10),
new Result('badger', 'badger', 5),
new Result('duck', 'duck', 15),
new Result($facet, 'llama', 'llama', 10),
new Result($facet, 'badger', 'badger', 5),
new Result($facet, 'duck', 'duck', 15),
];
$processor_id = 'count_limit';
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment