You are here

function FacetapiWidgetLinks::buildListItems in Facet API 7

Same name and namespace in other branches
  1. 6.3 plugins/facetapi/widget_links.inc \FacetapiWidgetLinks::buildListItems()
  2. 7.2 plugins/facetapi/widget_links.inc \FacetapiWidgetLinks::buildListItems()

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().

1 call to FacetapiWidgetLinks::buildListItems()
FacetapiWidgetLinks::execute in plugins/facetapi/widget_links.inc
Implements FacetapiWidget::execute().

File

plugins/facetapi/widget_links.inc, line 88
The facetapi_links and facetapi_checkbox_links widget plugin classes.

Class

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

Code

function buildListItems($build) {
  $settings = $this->settings->settings;
  $attributes_init = array(
    'class' => $this
      ->getItemClasses(),
  );

  // Builds rows.
  $items = array();
  foreach ($build as $value => $item) {

    // Reset attributes for each item, combining potential custom ones and the
    // default "class" attribute. The item's "#attributes" property can be
    // used to e.g. set rel="nofollow" for individual items.
    $attributes = isset($item['#attributes']) ? $item['#attributes'] + $attributes_init : $attributes_init;
    $row = array(
      'class' => array(),
    );

    // Allow adding classes via altering.
    if (isset($item['#class'])) {
      $attributes['class'] = array_merge($attributes['class'], $item['#class']);
    }

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

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

      // We need nofollow only for items with non zero results.
      if ($settings['nofollow']) {

        // Check if rel="nofollow" should be added to links.
        switch ($settings['nofollow']) {

          // Always add nofollow.
          case 1:
            $variables['options']['attributes']['rel'] = 'nofollow';
            break;

          // Add nofollow if there is already an active item of this facet.
          case 2:
            if ($this->facet
              ->getAdapter()
              ->getActiveItems(array(
              'name' => $this->key,
            ))) {
              $variables['options']['attributes']['rel'] = 'nofollow';
            }
            break;

          // Add nofollow if there is already an active item of any facet.
          case 3:
            if ($this->facet
              ->getAdapter()
              ->getAllActiveItems()) {
              $variables['options']['attributes']['rel'] = 'nofollow';
            }
            break;
        }
      }
    }

    // Add an ID to identify this link.
    $variables['options']['attributes']['id'] = drupal_html_id('facetapi-link');
    $variables['options'] += array(
      'html' => $item['#html'],
      'query' => $item['#query'],
    );

    // If the item is active, the li is active
    if ($item['#active']) {
      $row['class'][] = 'active';
    }

    // 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 ($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 = $item['#active'] ? 'facetapi-active' : 'facetapi-inactive';
    $variables['options']['attributes']['class'][] = $class;

    // Themes the link, adds row to items.
    $row['data'] = theme($item['#theme'], $variables);
    $items[] = $row;
  }
  return $items;
}