function _bricks_nested_get_new_elements in Bricks 2.x
Parameters
array $render_elements:
Return value
array
1 call to _bricks_nested_get_new_elements()
- _bricks_nest_items in ./
bricks.module - Helper function: converts element's items to a tree structure.
File
- ./
bricks.module, line 73
Code
function _bricks_nested_get_new_elements(array $render_elements) : array {
// A stack of parents, it contains a flat list of parents (also called
// ancestors).
$parents = [
BRICKS_TREE_ROOT,
];
$previous_depth = 0;
foreach ($render_elements as $key => $render_element) {
// At this point, the element contains a 'content' key containing a render
// array to view an entity and an empty attributes object. Remove this
// layer and keep only content.
$content = $render_element['content'] ?? [];
// Now, find the field reference item referencing the entity in content.
$field_item = _bricks_nest_get_field_item($content);
// Sanity check.
if (!$field_item) {
continue;
}
// The bricks tree widget records the indentation of the item as depth.
$depth = $field_item
->getDepth();
// Magic #1a. if the current depth is higher than the previous one then the
// previous key is the current parent. This is the equivalent of dragging
// an element in the widget to the right. The current parent is always at
// index 0, the grandparent is at index 1 etc.
if ($depth > $previous_depth) {
array_unshift($parents, $previous_key);
}
elseif ($depth < $previous_depth) {
for ($i = 0; $i < $previous_depth - $depth; $i++) {
array_shift($parents);
}
}
$previous_depth = $depth;
$previous_key = $key;
// Magic #2. Put $parents[0] in #bricks_parent_key (and add the other
// extras). The function itself is not doing anything magical.
$new_elements[$key] = _bricks_nest_get_new_element($content, $key, $field_item, $parents[0]);
}
return $new_elements ?? [];
}