You are here

protected function WidgetPluginBase::buildListItems in Facets 8

Builds a renderable array of result items.

Parameters

\Drupal\facets\FacetInterface $facet: The facet we need to build.

\Drupal\facets\Result\ResultInterface $result: A result item.

Return value

array A renderable array of the result.

2 calls to WidgetPluginBase::buildListItems()
LinksWidget::build in src/Plugin/facets/widget/LinksWidget.php
Builds the facet widget for rendering.
WidgetPluginBase::build in src/Widget/WidgetPluginBase.php
Builds the facet widget for rendering.
1 method overrides WidgetPluginBase::buildListItems()
ArrayWidget::buildListItems in src/Plugin/facets/widget/ArrayWidget.php
Builds a renderable array of result items.

File

src/Widget/WidgetPluginBase.php, line 173

Class

WidgetPluginBase
A base class for widgets that implements most of the boilerplate.

Namespace

Drupal\facets\Widget

Code

protected function buildListItems(FacetInterface $facet, ResultInterface $result) {
  $classes = [
    'facet-item',
  ];
  $items = $this
    ->prepareLink($result);
  $children = $result
    ->getChildren();

  // Check if we need to expand this result.
  if ($children && ($this->facet
    ->getExpandHierarchy() || $result
    ->isActive() || $result
    ->hasActiveChildren())) {
    $child_items = [];
    $classes[] = 'facet-item--expanded';
    foreach ($children as $child) {
      $child_items[] = $this
        ->buildListItems($facet, $child);
    }
    $items['children'] = [
      '#theme' => $this
        ->getFacetItemListThemeHook($facet),
      '#items' => $child_items,
    ];
    if ($result
      ->hasActiveChildren()) {
      $classes[] = 'facet-item--active-trail';
    }
  }
  else {
    if ($children) {
      $classes[] = 'facet-item--collapsed';
    }
  }
  if ($result
    ->isActive()) {
    $items['#attributes']['class'][] = 'is-active';
  }
  $items['#wrapper_attributes'] = [
    'class' => $classes,
  ];
  $items['#attributes']['data-drupal-facet-item-id'] = Html::getClass($this->facet
    ->getUrlAlias() . '-' . strtr($result
    ->getRawValue(), ' \'\\"', '---'));
  $items['#attributes']['data-drupal-facet-item-value'] = $result
    ->getRawValue();
  $items['#attributes']['data-drupal-facet-item-count'] = $result
    ->getCount();
  return $items;
}