You are here

protected function DefaultFacetManager::buildHierarchicalTree in Facets 8

Builds an hierarchical structure for results.

When given an array of results and an array which defines the hierarchical structure, this will build the results structure and set all childs.

Parameters

\Drupal\facets\Result\ResultInterface[] $keyed_results: An array of results keyed by id.

array $parent_groups: An array of 'child id arrays' keyed by their parent id.

Return value

\Drupal\facets\Result\ResultInterface[] An array of results structured hierarchically.

1 call to DefaultFacetManager::buildHierarchicalTree()
DefaultFacetManager::build in src/FacetManager/DefaultFacetManager.php
Builds a facet and returns it as a renderable array.

File

src/FacetManager/DefaultFacetManager.php, line 414

Class

DefaultFacetManager
The facet manager.

Namespace

Drupal\facets\FacetManager

Code

protected function buildHierarchicalTree(array $keyed_results, array $parent_groups) {
  foreach ($keyed_results as &$result) {
    $current_id = $result
      ->getRawValue();
    if (isset($parent_groups[$current_id]) && $parent_groups[$current_id]) {
      $child_ids = $parent_groups[$current_id];
      $child_keyed_results = [];
      foreach ($child_ids as $child_id) {
        if (isset($keyed_results[$child_id])) {
          $child_keyed_results[$child_id] = $keyed_results[$child_id];
        }
        else {

          // Children could already be built by Facets Summary manager, if
          // they are, just loading them will suffice.
          $children = $keyed_results[$current_id]
            ->getChildren();
          if (!empty($children[$child_id])) {
            $child_keyed_results[$child_id] = $children[$child_id];
          }
        }
      }
      $result
        ->setChildren($child_keyed_results);
      $this->childIds = array_merge($this->childIds, $child_ids);
    }
  }
  return $keyed_results;
}