You are here

function vff_build_tree in Field Formatter Template 8

Same name and namespace in other branches
  1. 8.2 modules/vff/vff.theme.inc \vff_build_tree()

Build tree from flat array.

Parameters

array $flat: The array.

string $idField: The id field.

string $parentIdField: The parent id field.

string $childNodesField: The child name.

Return value

array The tree array.

1 call to vff_build_tree()
template_preprocess_views_formatter_template in modules/vff/vff.theme.inc
Prepares variables for views carousel template.

File

modules/vff/vff.theme.inc, line 26
Preprocess formatter template.

Code

function vff_build_tree(array $flat, $idField = 'id', $parentIdField = 'parentId', $childNodesField = 'childNodes') {
  $flat[] = [
    $idField => 0,
    $parentIdField => NULL,
    $childNodesField => [],
  ];
  $indexed = [];

  // First pass - get the array indexed by the primary id.
  foreach ($flat as $row) {
    $indexed[$row[$idField]] = $row;
    $indexed[$row[$idField]][$childNodesField] = [];
  }

  // Second pass.
  $root = NULL;
  foreach ($indexed as $id => $row) {
    $indexed[$row[$parentIdField]][$childNodesField][$id] =& $indexed[$id];
    if (!$row[$parentIdField]) {
      $root = $id;
    }
  }
  return $indexed[$root][$childNodesField];
}