You are here

public function LayoutParagraphsRendererService::buildLayoutSection in Layout Paragraphs 2.0.x

Build the render array for Layout Paragraph Section.

Parameters

LayoutParagraphsSection $section: The layout paragraph section.

string $view_mode: The view mode.

Return value

array The build array.

1 call to LayoutParagraphsRendererService::buildLayoutSection()
LayoutParagraphsRendererService::renderLayoutSection in src/LayoutParagraphsRendererService.php
Renders a single Layout Paragraph Section for the provided paragraph.

File

src/LayoutParagraphsRendererService.php, line 87

Class

LayoutParagraphsRendererService
Class definition for a layout paragraphs service.

Namespace

Drupal\layout_paragraphs

Code

public function buildLayoutSection(LayoutParagraphsSection $section, $view_mode = '') {
  $view_builder = $this->entityTypeManager
    ->getViewBuilder('paragraph');
  $layout = $this->layoutPluginManager
    ->createInstance($section
    ->getLayoutId(), $section
    ->getLayoutConfiguration());

  // Map rendered paragraphs into their respective regions.
  $regions = $layout
    ->getPluginDefinition()
    ->getRegions();
  foreach (array_keys($regions) as $region) {
    $regions[$region] = array_map(function ($component) use ($view_builder, $view_mode) {
      $entity = $component
        ->getEntity();
      $access = $entity
        ->access('view', NULL, TRUE);
      if ($access
        ->isAllowed()) {
        return $view_builder
          ->view($component
          ->getEntity(), $view_mode);
      }
    }, $section
      ->getComponentsForRegion($region));
  }
  return $layout
    ->build($regions);
}