public function LayoutParagraphsWidget::buildLayouts in Layout Paragraphs 1.0.x
Restructures $elements array into layout.
Parameters
array $elements: The widget elements.
\Drupal\Core\Form\FormStateInterface $form_state: The form state object.
Return value
array The restructured with layouts.
File
- src/
Plugin/ Field/ FieldWidget/ LayoutParagraphsWidget.php, line 820
Class
- LayoutParagraphsWidget
- Entity Reference with Layout field widget.
Namespace
Drupal\layout_paragraphs\Plugin\Field\FieldWidgetCode
public function buildLayouts(array $elements, FormStateInterface $form_state) {
$tree = [
'#parents' => [],
];
$paragraph_elements = [];
$elements['#items'] = [];
foreach (Element::children($elements) as $index) {
$element = $elements[$index];
if (!empty($element['#widget_item'])) {
$paragraph_elements[$element['#entity']
->uuid()] = $element;
// Maintain a hidden flat list of elements to easily locate items.
$elements['#items'][$element['#entity']
->uuid()] = $element;
unset($elements[$index]);
}
}
// Move any orphaned items to the disabled bin.
foreach ($paragraph_elements as $index => $element) {
$paragraph = $element['#entity'];
$layout_settings = $this
->getLayoutSettings($paragraph);
$parent_uuid = $layout_settings['parent_uuid'];
if ($parent_uuid && !isset($paragraph_elements[$parent_uuid])) {
$paragraph_elements[$index]['#region'] = '_disabled';
}
}
// Sort items by weight to make sure we're processing the correct order.
uasort($paragraph_elements, function ($a, $b) {
if ($a['#weight'] == $b['#weight']) {
return 0;
}
return $a['#weight'] < $b['#weight'] ? -1 : 1;
});
while ($element = array_shift($paragraph_elements)) {
/* @var \Drupal\Core\Entity\EntityInterface $entity */
$entity = $element['#entity'];
if ($element['#layout']) {
$tree[$entity
->uuid()] = $this
->buildLayout($element, $paragraph_elements, $elements['disabled']['items']);
}
else {
$tree[$entity
->uuid()] = $element;
}
// Move disabled items to disabled region.
if ($element['#region'] == '_disabled') {
$elements['disabled']['items'][$entity
->uuid()] = $tree[$entity
->uuid()];
unset($tree[$entity
->uuid()]);
}
}
$elements['active_items'] = [
'#type' => 'container',
'#attributes' => [
'class' => [
'active-items',
],
'id' => $this->wrapperId . "--active-items",
],
'items' => $tree,
'#parents' => [],
];
return $elements;
}