You are here

public function EntityReferenceLayoutFormatter::buildLayoutTree in Entity Reference with Layout 8

Builds the structure for the entire layout.

Parameters

\Drupal\Core\Field\EntityReferenceFieldItemListInterface $items: The items to render.

string $langcode: The current language code.

Return value

array A render array.

Throws

\Drupal\Component\Plugin\Exception\PluginException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

\Drupal\Core\TypedData\Exception\MissingDataException

See also

EntityReferenceRevisionsEntityFormatter::viewElements()

1 call to EntityReferenceLayoutFormatter::buildLayoutTree()
EntityReferenceLayoutFormatter::viewElements in src/Plugin/Field/FieldFormatter/EntityReferenceLayoutFormatter.php
Builds a renderable array for a field value.

File

src/Plugin/Field/FieldFormatter/EntityReferenceLayoutFormatter.php, line 109

Class

EntityReferenceLayoutFormatter
Entity Reference with Layout field formatter.

Namespace

Drupal\entity_reference_layout\Plugin\Field\FieldFormatter

Code

public function buildLayoutTree(EntityReferenceFieldItemListInterface $items, $langcode) {
  $build = [];
  $containerUUID = FALSE;

  /* @var \Drupal\paragraphs\ParagraphInterface $entity */
  foreach ($this
    ->getEntitiesToView($items, $langcode) as $delta => $entity) {

    /** @var \Drupal\image\Plugin\Field\FieldType\ImageItem referringItem */
    $referringItem = $entity->_referringItem;

    // Protect ourselves from recursive rendering.
    static $depth = 0;
    $depth++;
    if ($depth > 20) {
      $this->loggerFactory
        ->get('entity')
        ->error($this
        ->t('Recursive rendering detected when rendering entity @entity_type @entity_id. Aborting rendering.', [
        '@entity_type' => $entity
          ->getEntityTypeId(),
        '@entity_id' => $entity
          ->id(),
      ]));
      return $build;
    }

    // Container.
    if ($referringItem
      ->get('layout')
      ->getString()) {
      $containerUUID = $entity
        ->uuid();
      $build[$containerUUID] = $this
        ->buildLayoutContainer($entity);
    }
    else {
      if (!$containerUUID) {
        $messenger = \Drupal::messenger();
        $messenger
          ->addMessage($this
          ->t('No parent container defined'), 'warning');
      }
      else {
        $referringItem = $entity->_referringItem;
        $region = $referringItem
          ->get('region')
          ->getString();
        $build[$containerUUID]['#regions'][$region][] = $this
          ->buildEntityView($entity);
      }
    }
    $depth = 0;
  }
  foreach ($build as &$layout_paragraph) {
    if (isset($layout_paragraph['#layout_instance'])) {
      $regions = $layout_paragraph['#regions'];
      $layout_paragraph['rendered_layout'] = [
        '#weight' => 1000,
      ] + $layout_paragraph['#layout_instance']
        ->build($regions);
    }
  }
  return $build;
}