You are here

public function FacetapiFacet::processHierarchy in Facet API 6

Processes hierarchical relationships between the facet items.

Parameters

&$build: The facet's render array.

Return value

An instance of this class.

File

./facetapi.adapter.inc, line 561
Defines classes used by the FacetAPI module.

Class

FacetapiFacet
Stores facet data, provides methods that build the facet's render array.

Code

public function processHierarchy(&$build) {

  // Builds the hierarchy information if the hierarchy callback is defined.
  if (!empty($this->_facet['hierarchy callback']) && !empty($build)) {
    $parents = $this->_facet['hierarchy callback'](array_keys($build));
    foreach ($parents as $value => $parents) {
      foreach ($parents as $parent) {
        if (isset($build[$parent]) && isset($build[$value])) {

          // Use a reference so we see the updated data.
          $build[$parent]['#item_children'][$value] =& $build[$value];
          $build[$value]['#item_parents'][$parent] = $parent;
        }
      }
    }
  }

  // Tests whether parents have an active child.
  // @todo: Can we make this more efficient?
  do {
    $active = 0;
    foreach ($build as $value => $item) {
      if ($item['#active'] && !empty($item['#item_parents'])) {

        // @todo Can we build facets with multiple parents? Core taxonomy
        // form cannot, so we will need a check here.
        foreach ($item['#item_parents'] as $parent) {
          if (!$build[$parent]['#active']) {
            $active = $build[$parent]['#active'] = 1;
          }
        }
      }
    }
  } while ($active);

  // Strips children whose parents are inactive.
  $build = array_filter($build, 'facetapi_inactive_parent_filter');

  // Returns instance of this class.
  return $this;
}