You are here

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

Renders a single Layout Paragraph Section for the provided paragraph.

Parameters

Drupal\paragraphs\Entity\Paragraph $paragraph: The paragraph entity.

string $view_mode: The view mode.

Return value

array The component render array.

File

src/LayoutParagraphsRendererService.php, line 59

Class

LayoutParagraphsRendererService
Class definition for a layout paragraphs service.

Namespace

Drupal\layout_paragraphs

Code

public function renderLayoutSection(array &$build, Paragraph $paragraph, string $view_mode = 'default') {
  if (!LayoutParagraphsComponent::isLayoutComponent($paragraph)) {

    // @Todo: consider throwing an exception if $paragraph does not have a layout applied.
    return [];
  }
  if ($paragraph->_referringItem) {
    $layout = new LayoutParagraphsLayout($paragraph->_referringItem
      ->getParent());
  }
  else {
    $parent_entity = $this
      ->getParentEntity($paragraph);
    $field_name = $paragraph
      ->get('parent_field_name')->value;
    $layout = new LayoutParagraphsLayout($parent_entity->{$field_name});
  }
  $section = $layout
    ->getLayoutSection($paragraph);
  return $this
    ->buildLayoutSection($section, $view_mode);
}