You are here

public function LinksWidget::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

File

src/Plugin/facets/widget/LinksWidget.php, line 41

Class

LinksWidget
The links widget.

Namespace

Drupal\facets\Plugin\facets\widget

Code

public function build(FacetInterface $facet) {
  $build = parent::build($facet);
  $this
    ->appendWidgetLibrary($build);
  $soft_limit = (int) $this
    ->getConfiguration()['soft_limit'];
  if ($soft_limit !== 0) {
    $show_less_label = $this
      ->getConfiguration()['soft_limit_settings']['show_less_label'];
    $show_more_label = $this
      ->getConfiguration()['soft_limit_settings']['show_more_label'];
    $build['#attached']['library'][] = 'facets/soft-limit';
    $build['#attached']['drupalSettings']['facets']['softLimit'][$facet
      ->id()] = $soft_limit;
    $build['#attached']['drupalSettings']['facets']['softLimitSettings'][$facet
      ->id()]['showLessLabel'] = $show_less_label;
    $build['#attached']['drupalSettings']['facets']['softLimitSettings'][$facet
      ->id()]['showMoreLabel'] = $show_more_label;
  }
  if ($facet
    ->getUseHierarchy()) {
    $build['#attached']['library'][] = 'facets/drupal.facets.hierarchical';
  }
  $results = $facet
    ->getResults();
  if ($this
    ->getConfiguration()['show_reset_link'] && count($results) > 0 && (!$this
    ->getConfiguration()['hide_reset_when_no_selection'] || $facet
    ->getActiveItems())) {

    // Add reset link.
    $max_items = array_sum(array_map(function ($item) {
      return $item
        ->getCount();
    }, $results));
    $urlProcessorManager = \Drupal::service('plugin.manager.facets.url_processor');
    $url_processor = $urlProcessorManager
      ->createInstance($facet
      ->getFacetSourceConfig()
      ->getUrlProcessorName(), [
      'facet' => $facet,
    ]);
    $active_filters = $url_processor
      ->getActiveFilters();
    if (isset($active_filters[''])) {
      unset($active_filters['']);
    }
    unset($active_filters[$facet
      ->id()]);

    // Only if there are still active filters, use url generator.
    if ($active_filters) {
      $url = \Drupal::service('facets.utility.url_generator')
        ->getUrl($active_filters, FALSE);
    }
    else {
      $request = \Drupal::request();
      $url = Url::createFromRequest($request);
      $params = $request->query
        ->all();
      unset($params[$url_processor
        ->getFilterKey()]);
      if (\array_key_exists('page', $params)) {

        // Go back to the first page on reset.
        unset($params['page']);
      }
      $url
        ->setRouteParameter('facets_query', '');
      $url
        ->setOption('query', $params);
    }
    $result_item = new Result($facet, 'reset_all', $this
      ->getConfiguration()['reset_text'], $max_items);
    $result_item
      ->setActiveState(FALSE);
    $result_item
      ->setUrl($url);

    // Check if any other facet is in use.
    $none_active = TRUE;
    foreach ($results as $result) {
      if ($result
        ->isActive() || $result
        ->hasActiveChildren()) {
        $none_active = FALSE;
        break;
      }
    }

    // Add an is-active class when no other facet is in use.
    if ($none_active) {
      $result_item
        ->setActiveState(TRUE);
    }

    // Build item.
    $item = $this
      ->buildListItems($facet, $result_item);

    // Add a class for the reset link wrapper.
    $item['#wrapper_attributes']['class'][] = 'facets-reset';

    // Put reset facet link on first place.
    array_unshift($build['#items'], $item);
  }
  return $build;
}