You are here

public function SliderWidget::build in Facets 8

Builds the facet widget for rendering.

Parameters

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

Return value

array A renderable array.

Overrides WidgetPluginBase::build

1 call to SliderWidget::build()
RangeSliderWidget::build in modules/facets_range_widget/src/Plugin/facets/widget/RangeSliderWidget.php
Builds the facet widget for rendering.
1 method overrides SliderWidget::build()
RangeSliderWidget::build in modules/facets_range_widget/src/Plugin/facets/widget/RangeSliderWidget.php
Builds the facet widget for rendering.

File

modules/facets_range_widget/src/Plugin/facets/widget/SliderWidget.php, line 38

Class

SliderWidget
The slider widget.

Namespace

Drupal\facets_range_widget\Plugin\facets\widget

Code

public function build(FacetInterface $facet) {
  $build = parent::build($facet);
  $results = $facet
    ->getResults();
  if (empty($results)) {
    return $build;
  }
  ksort($results);
  $show_numbers = $facet
    ->getWidgetInstance()
    ->getConfiguration()['show_numbers'];
  $urls = [];
  $labels = [];
  foreach ($results as $result) {
    $urls['f_' . $result
      ->getRawValue()] = $result
      ->getUrl()
      ->toString();
    $labels[] = $result
      ->getDisplayValue() . ($show_numbers ? ' (' . $result
      ->getCount() . ')' : '');
  }

  // The results set on the facet are sorted where the minimum is the first
  // item and the last one is the one with the highest results, so it's safe
  // to use min/max.
  $min = (double) reset($results)
    ->getRawValue();
  $max = (double) end($results)
    ->getRawValue();
  $build['#items'] = [
    [
      '#type' => 'html_tag',
      '#tag' => 'div',
      '#attributes' => [
        'class' => [
          'facet-slider',
        ],
        'id' => $facet
          ->id(),
      ],
    ],
  ];
  $active = $facet
    ->getActiveItems();
  $build['#attached']['library'][] = 'facets_range_widget/slider';
  $build['#attached']['drupalSettings']['facets']['sliders'][$facet
    ->id()] = [
    'min' => $min,
    'max' => $max,
    'value' => isset($active[0]) ? (double) $active[0] : '',
    'urls' => $urls,
    'prefix' => $this
      ->getConfiguration()['prefix'],
    'suffix' => $this
      ->getConfiguration()['suffix'],
    'step' => $this
      ->getConfiguration()['step'],
    'labels' => $labels,
  ];
  return $build;
}