function _bricks_nest_items in Bricks 8
Same name and namespace in other branches
- 2.x bricks.module \_bricks_nest_items()
Helper function: converts element's items to a tree structure.
1 call to _bricks_nest_items()
- bricks_preprocess_field in ./
bricks.module - Prepares variables for `field.html.twig`.
File
- ./
bricks.module, line 24
Code
function _bricks_nest_items($element, $items) {
// Prepare items:
$parents = [
-1,
];
$prev_depth = 0;
foreach ($items as $delta => $item) {
if (!$element['#items'][$delta]->entity) {
unset($element['#items'][$delta]);
unset($items[$delta]);
continue;
}
$depth = $element['#items'][$delta]->depth;
if ($depth > $prev_depth) {
array_unshift($parents, $delta - 1);
}
elseif ($depth < $prev_depth) {
array_splice($parents, 0, $prev_depth - $depth);
}
$prev_depth = $depth;
$items[$delta] = $items[$delta]['content'];
$items[$delta]['#label'] = $element['#items'][$delta]->entity
->label();
$items[$delta]['#delta'] = $delta;
$items[$delta]['#parent_delta'] = $parents[0];
$items[$delta]['#attributes']['class'][] = 'brick';
$items[$delta]['#attributes']['class'][] = 'brick--type--' . Html::cleanCssIdentifier($element['#items'][$delta]->entity
->bundle());
$items[$delta]['#attributes']['class'][] = 'brick--id--' . $element['#items'][$delta]->entity
->id();
$items[$delta]['childs'] = [];
if (!empty($element['#items'][$delta]->options['view_mode'])) {
$items[$delta]['#view_mode'] = $element['#items'][$delta]->options['view_mode'];
}
if (!empty($element['#items'][$delta]->options['layout'])) {
$items[$delta]['#layout'] = $element['#items'][$delta]->options['layout'];
}
if (!empty($element['#items'][$delta]->options['css_class'])) {
$items[$delta]['#attributes']['class'][] = $element['#items'][$delta]->options['css_class'];
}
// Disable entity render cache, rely on field cache:
$items[$delta]['#cache'] = [
'disabled' => TRUE,
];
}
// Process items in reverse order (without recursion):
$rdeltas = array_reverse(array_keys($items));
foreach ($rdeltas as $delta) {
$parent_delta = $items[$delta]['#parent_delta'];
if (!empty($items[$delta]['#layout']) && \Drupal::service('module_handler')
->moduleExists('layout_discovery')) {
$items[$delta] = array_intersect_key($items[$delta], array_flip([
'#label',
'#attributes',
])) + _bricks_build_layout_from_items($items[$delta]['#layout'], $items[$delta]['childs']);
}
if ($parent_delta != -1) {
array_unshift($items[$parent_delta]['childs'], $items[$delta]);
unset($items[$delta]);
}
}
return $items;
}