diff --git a/facets.module b/facets.module index 40b6366..767d8e1 100644 --- a/facets.module +++ b/facets.module @@ -48,7 +48,7 @@ function facets_theme($existing, $type, $theme, $path) { 'variables' => [ 'facet' => NULL, 'raw_value' => '', - 'value' => '', + 'value' => [], 'show_count' => FALSE, 'count' => NULL, 'is_active' => FALSE, diff --git a/src/Result/Result.php b/src/Result/Result.php index 434606e..24e705b 100644 --- a/src/Result/Result.php +++ b/src/Result/Result.php @@ -59,6 +59,11 @@ class Result implements ResultInterface { */ protected $children = []; + /** + * Renderable result. + */ + protected $renderArray = ['value' => []]; + /** * Constructs a new result value object. * @@ -177,4 +182,26 @@ public function getFacet() { return $this->facet; } + /** + * {@inheritdoc} + */ + public function setRenderArrayProperty($name, $value) { + $this->renderArray[$name] = $value; + } + + /** + * {@inheritdoc} + */ + public function toRenderable() { + // Add display value to renderable array just before returning the + // renderable array to prevent other processors overruling the value + // property. + $this->setRenderArrayProperty('value', [ + '#plain_text' => $this->getDisplayValue(), + '#weight' => 0, + ]); + + return $this->renderArray; + } + } diff --git a/src/Result/ResultInterface.php b/src/Result/ResultInterface.php index a68fe97..df113a7 100644 --- a/src/Result/ResultInterface.php +++ b/src/Result/ResultInterface.php @@ -3,11 +3,12 @@ namespace Drupal\facets\Result; use Drupal\Core\Url; +use Drupal\Core\Render\RenderableInterface; /** * The interface defining what a facet result should look like. */ -interface ResultInterface { +interface ResultInterface extends RenderableInterface { /** * Returns the facet related to the result. @@ -113,4 +114,17 @@ public function setChildren(array $children); */ public function getChildren(); + /** + * Sets renderable array property. + * + * @param string $name + * Name of the renderable array property. + * + * @param mixed $value + * Value of the renderable array element, could be string, boolean or array. + * + * @see \Drupal\Core\Render\Element\RenderElement + */ + public function setRenderArrayProperty($name, $value); + } diff --git a/src/Widget/WidgetPluginBase.php b/src/Widget/WidgetPluginBase.php index 68cf0a7..cb4c4b4 100644 --- a/src/Widget/WidgetPluginBase.php +++ b/src/Widget/WidgetPluginBase.php @@ -230,7 +230,7 @@ protected function buildResultItem(ResultInterface $result) { return [ '#theme' => 'facets_result_item', '#is_active' => $result->isActive(), - '#value' => $result->getDisplayValue(), + '#value' => $result->toRenderable(), '#show_count' => $this->getConfiguration()['show_numbers'] && ($count !== NULL), '#count' => $count, '#facet' => $result->getFacet(), diff --git a/templates/facets-result-item.html.twig b/templates/facets-result-item.html.twig index 1fb7d3d..0bfb180 100644 --- a/templates/facets-result-item.html.twig +++ b/templates/facets-result-item.html.twig @@ -4,7 +4,7 @@ * Default theme implementation of a facet result item. * * Available variables: - * - value: The item value. + * - value: Renderable array with the item value. * - raw_value: The raw item value. * - show_count: If this facet provides count. * - count: The amount of results. diff --git a/tests/src/Unit/Plugin/widget/WidgetTestBase.php b/tests/src/Unit/Plugin/widget/WidgetTestBase.php index 510aa17..8b63803 100644 --- a/tests/src/Unit/Plugin/widget/WidgetTestBase.php +++ b/tests/src/Unit/Plugin/widget/WidgetTestBase.php @@ -132,7 +132,7 @@ protected function buildLinkAssertion($text, $raw_value, FacetInterface $facet, '#theme' => 'facets_result_item', '#raw_value' => $raw_value, '#facet' => $facet, - '#value' => $text, + '#value' => ['value' => ['#plain_text' => $text, '#weight' => 0]], '#show_count' => $show_numbers && ($count !== NULL), '#count' => $count, '#is_active' => $active,