function _bricks_nest_items in Bricks 2.x
Same name and namespace in other branches
- 8 bricks.module \_bricks_nest_items()
Helper function: converts element's items to a tree structure.
To understand how this function converts a flat list of render elements into a tree, search this function and _bricks_nested_get_new_elements for magic.
1 call to _bricks_nest_items()
- bricks_preprocess_field in ./
bricks.module - Prepares variables for `field.html.twig`.
File
- ./
bricks.module, line 37
Code
function _bricks_nest_items($render_elements) {
// Every element in $new_elements is a render array to view an entity plus a
// few bricks specific extras: some CSS classes are added, the entity label
// is surfaced and most importantly #bricks_parent_key points to the parent
// in the $new_elements array and #layout is extracted from the field item
// options.
$new_elements = _bricks_nested_get_new_elements($render_elements);
// Process the elements in reverse order: See magic #3 why.
foreach (array_reverse(array_keys($new_elements)) as $key) {
// Save the parent key because moving into a layout loses it.
$parent_key = $new_elements[$key]['#bricks_parent_key'];
// If this is a layout paragraph, move the children of it into a layout.
if (!empty($new_elements[$key]['#layout']) && \Drupal::service('module_handler')
->moduleExists('layout_discovery')) {
$keep = array_intersect_key($new_elements[$key], array_flip([
'#label',
'#attributes',
'#paragraph',
]));
$new_elements[$key] = $keep + _bricks_build_layout_from_items($new_elements[$key]['#layout'], $new_elements[$key]['layout_children']);
}
// Magic #3. If this is not a root element, move it under the parent.
// By processing the elements from the bottom, by the time the parent is
// reached, all the children is moved under it.
if ($parent_key !== BRICKS_TREE_ROOT) {
array_unshift($new_elements[$parent_key]['layout_children'], $new_elements[$key]);
unset($new_elements[$key]);
}
}
return $new_elements;
}