You are here

public function JsonApiResponseWidget::build in JSON:API Search API 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

File

modules/jsonapi_search_api_facets/src/Plugin/facets/widget/JsonApiResponseWidget.php, line 26

Class

JsonApiResponseWidget
A simple widget class that returns for inclusion in JSON:API Search API.

Namespace

Drupal\jsonapi_search_api_facets\Plugin\facets\widget

Code

public function build(FacetInterface $facet) {
  $build = [
    'id' => $facet
      ->id(),
    'label' => $facet
      ->getName(),
    'path' => $facet
      ->getUrlAlias(),
    'terms' => [],
  ];
  $configuration = $facet
    ->getWidget();
  $this->showNumbers = empty($configuration['show_numbers']) ? FALSE : (bool) $configuration['show_numbers'];
  foreach ($facet
    ->getResults() as $result) {
    if ($result
      ->getUrl() === NULL) {
      $build['terms'][] = $this
        ->generateValues($result);
    }
    else {
      $build['terms'][] = $this
        ->buildListItems($facet, $result);
    }
  }
  return $build;
}