You are here

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

Builds the structure for a single layout paragraph item.

Also adds elements for the layout instance and regions. Regions will be populated with paragraphs further down the line, then rendered in the layout.

Parameters

\Drupal\paragraphs\ParagraphInterface $entity: The paragraph to render.

Return value

array Returns a build array.

Throws

\Drupal\Component\Plugin\Exception\PluginException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

\Drupal\Core\TypedData\Exception\MissingDataException

1 call to EntityReferenceLayoutFormatter::buildLayoutContainer()
EntityReferenceLayoutFormatter::buildLayoutTree in src/Plugin/Field/FieldFormatter/EntityReferenceLayoutFormatter.php
Builds the structure for the entire layout.

File

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

Class

EntityReferenceLayoutFormatter
Entity Reference with Layout field formatter.

Namespace

Drupal\entity_reference_layout\Plugin\Field\FieldFormatter

Code

public function buildLayoutContainer(ParagraphInterface $entity) {

  /** @var \Drupal\entity_reference_layout\Plugin\Field\FieldType\EntityReferenceLayoutRevisioned $referringItem */
  $referringItem = $entity->_referringItem;
  $layout = $referringItem
    ->get('layout')
    ->getString();
  $config = $referringItem
    ->get('config')
    ->getValue();
  if (!$this->layoutPluginManager
    ->getDefinition($layout, FALSE)) {
    $messenger = \Drupal::messenger();
    $messenger
      ->addMessage($this
      ->t('Layout `%layout_id` is unknown.', [
      '%layout_id' => $layout,
    ]), 'warning');
    return [];
  }
  $layout_instance = $this->layoutPluginManager
    ->createInstance($layout, $config);
  $build = $this
    ->buildEntityView($entity);
  $build['#layout_instance'] = $layout_instance;
  $build['#regions'] = [];
  foreach ($layout_instance
    ->getPluginDefinition()
    ->getRegionNames() as $region_name) {
    $build['#regions'][$region_name] = [];
  }
  return $build;
}