You are here

public function WidgetPluginBase::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 WidgetPluginInterface::build

3 calls to WidgetPluginBase::build()
DropdownWidget::build in src/Plugin/facets/widget/DropdownWidget.php
Builds the facet widget for rendering.
LinksWidget::build in src/Plugin/facets/widget/LinksWidget.php
Builds the facet widget for rendering.
SliderWidget::build in modules/facets_range_widget/src/Plugin/facets/widget/SliderWidget.php
Builds the facet widget for rendering.
4 methods override WidgetPluginBase::build()
ArrayWidget::build in src/Plugin/facets/widget/ArrayWidget.php
Builds the facet widget for rendering.
DropdownWidget::build in src/Plugin/facets/widget/DropdownWidget.php
Builds the facet widget for rendering.
LinksWidget::build in src/Plugin/facets/widget/LinksWidget.php
Builds the facet widget for rendering.
SliderWidget::build in modules/facets_range_widget/src/Plugin/facets/widget/SliderWidget.php
Builds the facet widget for rendering.

File

src/Widget/WidgetPluginBase.php, line 45

Class

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

Namespace

Drupal\facets\Widget

Code

public function build(FacetInterface $facet) {
  $this->facet = $facet;
  $items = array_map(function (Result $result) use ($facet) {
    if (empty($result
      ->getUrl())) {
      return $this
        ->buildResultItem($result);
    }
    else {

      // When the facet is being build in an AJAX request, and the facetsource
      // is a block, we need to update the url to use the current request url.
      if ($result
        ->getUrl()
        ->isRouted() && $result
        ->getUrl()
        ->getRouteName() === 'facets.block.ajax') {
        $request = \Drupal::request();
        $url_object = \Drupal::service('path.validator')
          ->getUrlIfValid($request
          ->getPathInfo());
        if ($url_object) {
          $url = $result
            ->getUrl();
          $options = $url
            ->getOptions();
          $route_params = $url_object
            ->getRouteParameters();
          $route_name = $url_object
            ->getRouteName();
          $result
            ->setUrl(new Url($route_name, $route_params, $options));
        }
      }
      return $this
        ->buildListItems($facet, $result);
    }
  }, $facet
    ->getResults());
  $widget = $facet
    ->getWidget();
  return [
    '#theme' => $this
      ->getFacetItemListThemeHook($facet),
    '#facet' => $facet,
    '#items' => $items,
    '#attributes' => [
      'data-drupal-facet-id' => $facet
        ->id(),
      'data-drupal-facet-alias' => $facet
        ->getUrlAlias(),
      'class' => [
        $facet
          ->getActiveItems() ? 'facet-active' : 'facet-inactive',
      ],
    ],
    '#context' => !empty($widget['type']) ? [
      'list_style' => $widget['type'],
    ] : [],
    '#cache' => [
      'contexts' => [
        'url.path',
        'url.query_args',
      ],
    ],
  ];
}