Commit 9064a63c authored by joachim's avatar joachim Committed by borisson_

Issue #2896202 by joachim: add template suggestions for...

Issue #2896202 by joachim: add template suggestions for facets-item-list.html.twig based on widget type and facet ID
parent 0caaba90
......@@ -41,7 +41,7 @@ class ArrayWidget extends WidgetPluginBase {
$items[$facet->getFieldIdentifier()][] = $text;
}
else {
$items[$facet->getFieldIdentifier()][] = $this->buildListItems($result);
$items[$facet->getFieldIdentifier()][] = $this->buildListItems($facet, $result);
}
}
......@@ -51,7 +51,7 @@ class ArrayWidget extends WidgetPluginBase {
/**
* {@inheritdoc}
*/
protected function buildListItems(ResultInterface $result) {
protected function buildListItems($facet, ResultInterface $result) {
if ($children = $result->getChildren()) {
$items = $this->prepare($result);
......
......@@ -99,17 +99,17 @@ class DateBasicWidget extends WidgetPluginBase {
public function build(FacetInterface $facet) {
$this->facet = $facet;
$items = array_map(function (Result $result) {
$items = array_map(function (Result $result) use ($facet) {
if (empty($result->getUrl())) {
return ['#markup' => $this->extractText($result)];
}
else {
return $this->buildListItems($result);
return $this->buildListItems($facet, $result);
}
}, $facet->getResults());
return [
'#theme' => 'facets_item_list',
'#theme' => $this->getFacetItemListThemeHook($facet),
'#items' => $items,
'#attributes' => ['data-drupal-facet-id' => $facet->id()],
'#cache' => [
......
......@@ -49,19 +49,19 @@ abstract class WidgetPluginBase extends PluginBase implements WidgetPluginInterf
public function build(FacetInterface $facet) {
$this->facet = $facet;
$items = array_map(function (Result $result) {
$items = array_map(function (Result $result) use ($facet) {
if (empty($result->getUrl())) {
return $this->buildResultItem($result);
}
else {
return $this->buildListItems($result);
return $this->buildListItems($facet, $result);
}
}, $facet->getResults());
$widget = $facet->getWidget();
return [
'#theme' => 'facets_item_list',
'#theme' => $this->getFacetItemListThemeHook($facet),
'#items' => $items,
'#attributes' => [
'data-drupal-facet-id' => $facet->id(),
......@@ -77,6 +77,24 @@ abstract class WidgetPluginBase extends PluginBase implements WidgetPluginInterf
];
}
/**
* Provides a full array of possible theme functions to try for a given hook.
*
* This allows the following template suggestions:
* - facets-item-list--WIDGET_TYPE--FACET_ID
* - facets-item-list--WIDGET_TYPE
* - facets-item-list
*
* @param \Drupal\facets\FacetInterface $facet
* The facet whose output is being generated.
*
* @return string
* A theme hook name with suggestions, suitable for the #theme property.
*/
protected function getFacetItemListThemeHook(FacetInterface $facet) {
return 'facets_item_list__' . $facet->getWidget()['type'] . '__' . $facet->id();
}
/**
* {@inheritdoc}
*/
......@@ -130,13 +148,15 @@ abstract class WidgetPluginBase extends PluginBase implements WidgetPluginInterf
/**
* Builds a renderable array of result items.
*
* @param \Drupal\facets\FacetInterface $facet
* The facet we need to build.
* @param \Drupal\facets\Result\ResultInterface $result
* A result item.
*
* @return array
* A renderable array of the result.
*/
protected function buildListItems(ResultInterface $result) {
protected function buildListItems($facet, ResultInterface $result) {
$classes = ['facet-item'];
$items = $this->prepareLink($result);
......@@ -147,11 +167,11 @@ abstract class WidgetPluginBase extends PluginBase implements WidgetPluginInterf
$child_items = [];
$classes[] = 'facet-item--expanded';
foreach ($children as $child) {
$child_items[] = $this->buildListItems($child);
$child_items[] = $this->buildListItems($facet, $child);
}
$items['children'] = [
'#theme' => 'facets_item_list',
'#theme' => $this->getFacetItemListThemeHook($facet),
'#items' => $child_items,
];
......
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