You are here

function _bricks_tree_items in Bricks​ 7.5

Same name and namespace in other branches
  1. 7.4 bricks.module \_bricks_tree_items()

Helper function: converts element's items to tree structure.

2 calls to _bricks_tree_items()
bricks_preprocess_entity in ./bricks.module
Preprocesses variables for entity.tpl.php.
bricks_process_field in ./bricks.module
Processes variables for theme_field().

File

./bricks.module, line 108

Code

function _bricks_tree_items($element, $items) {

  // Filter items:
  $items = array_intersect_key($items, element_children($items));

  // Simplify items:
  foreach ($items as $delta => $item) {
    $item = reset($item);
    $item = reset($item);
    $item['bricks'] = array();
    $items[$delta] = $item;
  }

  // Prepare items:
  $parents = array(
    -1,
  );
  $prev_depth = 0;
  foreach ($items as $delta => $item) {
    if (isset($element['#items'][$delta]['options'])) {
      $items[$delta]['#options'] = $element['#items'][$delta]['options'];
    }
    $depth = $element['#items'][$delta]['depth'];
    if ($depth > $prev_depth) {
      array_unshift($parents, $delta - 1);
    }
    else {
      if ($depth < $prev_depth) {
        array_splice($parents, 0, $prev_depth - $depth);
      }
    }
    $prev_depth = $depth;
    $items[$delta]['#parent_delta'] = $parents[0];
  }

  // Process items in reverse order (without recursion):
  $ritems = array_reverse($items, TRUE);
  foreach ($ritems as $delta => $item) {
    if ($item['#parent_delta'] != -1) {
      array_unshift($items[$item['#parent_delta']]['bricks'], $items[$delta]);
      unset($items[$delta]);
    }
  }
  return $items;
}