function Section::toRenderArray

Same name in other branches
  1. 9 core/modules/layout_builder/src/Section.php \Drupal\layout_builder\Section::toRenderArray()
  2. 8.9.x core/modules/layout_builder/src/Section.php \Drupal\layout_builder\Section::toRenderArray()
  3. 11.x core/modules/layout_builder/src/Section.php \Drupal\layout_builder\Section::toRenderArray()

Returns the renderable array for this section.

Parameters

\Drupal\Core\Plugin\Context\ContextInterface[] $contexts: An array of available contexts.

bool $in_preview: TRUE if the section is being previewed, FALSE otherwise.

Return value

array A renderable array representing the content of the section.

File

core/modules/layout_builder/src/Section.php, line 85

Class

Section
Provides a domain object for layout sections.

Namespace

Drupal\layout_builder

Code

public function toRenderArray(array $contexts = [], $in_preview = FALSE) {
    $regions = [];
    foreach ($this->getComponents() as $component) {
        if ($output = $component->toRenderArray($contexts, $in_preview)) {
            $regions[$component->getRegion()][$component->getUuid()] = $output;
        }
    }
    $layout = $this->getLayout($contexts);
    if ($layout instanceof PreviewAwarePluginInterface) {
        $layout->setInPreview($in_preview);
    }
    $build = $layout->build($regions);
    // If an entity was used to build the layout, store it on the build.
    if (!Element::isEmpty($build) && isset($contexts['layout_builder.entity'])) {
        $build['#entity'] = $contexts['layout_builder.entity']->getContextValue();
    }
    return $build;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.