You are here

function FacetapiAjaxWidgetLinks::buildListItems in Ajax facets 7.3

Transforms the render array for use with theme_item_list().

The recursion allows this function to act on the various levels of a hierarchical data set.

Parameters

array $build: The items in the facet's render array being transformed.

Return value

array The "items" parameter for theme_item_list().

File

plugins/facetapi/ajax_widget_links.inc, line 37
The facetapi ajax links widget.

Class

FacetapiAjaxWidgetLinks
Widget that renders facets as a list of clickable links.

Code

function buildListItems($build) {
  $settings = $this->settings->settings;

  // Initializes links attributes, adds rel="nofollow" if configured.
  $attributes = $settings['nofollow'] ? [
    'rel' => 'nofollow',
  ] : [];
  $attributes += [
    'class' => $this
      ->getItemClasses(),
  ];

  // Builds rows.
  $items = [];
  foreach ($build as $value => $item) {
    $row = [
      'class' => [],
    ];

    // Initializes variables passed to theme hook.
    $variables = [
      'text' => $item['#markup'],
      'path' => $item['#path'],
      'count' => !empty($settings['display_count']) ? $item['#count'] : NULL,
      'options' => [
        'attributes' => $attributes,
        'html' => $item['#html'],
        'query' => $item['#query'],
      ],
    ];

    // Adds the facetapi-zero-results class to items that have no results.
    if (!$item['#count']) {
      $variables['options']['attributes']['class'][] = 'facetapi-zero-results';
    }

    // If the item has no children, it is a leaf.
    if (empty($item['#item_children'])) {
      $row['class'][] = 'leaf';
    }
    else {

      // If the item is active or the "show_expanded" setting is selected,
      // show this item as expanded so we see its children.
      if (!empty($item['#active']) || !empty($settings['show_expanded'])) {
        $row['class'][] = 'expanded';
        $row['children'] = $this
          ->buildListItems($item['#item_children']);
      }
      else {
        $row['class'][] = 'collapsed';
      }
    }

    // Gets theme hook, adds last minute classes.
    $class = !empty($item['#active']) ? 'facetapi-active' : 'facetapi-inactive';
    $variables['options']['attributes']['class'][] = $class;

    // Set options for ajax query.
    $variables['options']['attributes']['data-facet-value'] = $item['#indexed_value'];
    $variables['options']['attributes']['data-facet-name'] = rawurlencode($this->settings->facet);
    $variables['options']['attributes']['data-raw-facet-name'] = $this->settings->facet;

    // Themes the link, adds row to items.
    $row['data'] = theme($item['#theme'], $variables);
    $items[] = $row;
  }
  $facet_settings = $this->facet
    ->getSettings();
  $this->jsSettings['limit_active_items'] = $facet_settings->settings['limit_active_items'];
  return $items;
}