You are here

public function LayoutParagraphsWidget::buildLayout in Layout Paragraphs 1.0.x

Builds a single layout element.

1 call to LayoutParagraphsWidget::buildLayout()
LayoutParagraphsWidget::buildLayouts in src/Plugin/Field/FieldWidget/LayoutParagraphsWidget.php
Restructures $elements array into layout.

File

src/Plugin/Field/FieldWidget/LayoutParagraphsWidget.php, line 881

Class

LayoutParagraphsWidget
Entity Reference with Layout field widget.

Namespace

Drupal\layout_paragraphs\Plugin\Field\FieldWidget

Code

public function buildLayout($layout_element, &$elements, &$disabled_items) {

  /* @var \Drupal\Core\Entity\EntityInterface $entity */
  $entity = $layout_element['#entity'];
  $uuid = $entity
    ->uuid();
  $layout_element['preview']['regions'] = [
    '#weight' => 100,
  ] + $layout_element['#layout_instance']
    ->build($layout_element['preview']['regions']);
  $layout_element['preview']['regions']['#parents'] = $layout_element['#parents'];
  foreach ($elements as $index => $element) {
    if (!empty($element['#region']) && $element['#parent_uuid'] == $uuid) {

      /* @var \Drupal\Core\Entity\EntityInterface $child_entity */
      $child_entity = $element['#entity'];
      $child_uuid = $child_entity
        ->uuid();
      $region = $element['#region'];

      // Recursive processing for layouts within layouts.
      if ($element['#layout']) {
        $sub_element = $this
          ->buildLayout($element, $elements, $disabled_items);
      }
      else {
        $sub_element = $element;
      }
      unset($elements[$index]);
      if (isset($layout_element['preview']['regions'][$region])) {
        $layout_element['preview']['regions'][$region][$child_uuid] = $sub_element;
      }
      else {
        $disabled_items[$child_uuid] = $sub_element;
      }
    }
  }
  return $layout_element;
}