You are here

public function LayoutParagraphsBuilder::preRender in Layout Paragraphs 2.0.x

Pre-render callback: Renders the UI.

File

src/Element/LayoutParagraphsBuilder.php, line 171

Class

LayoutParagraphsBuilder
Defines a render element for building the Layout Builder UI.

Namespace

Drupal\layout_paragraphs\Element

Code

public function preRender($element) {
  $this->layoutParagraphsLayout = $this->tempstore
    ->get($element['#layout_paragraphs_layout']);
  $this->langcode = $this->entityRepository
    ->getTranslationFromContext($this->layoutParagraphsLayout
    ->getEntity())
    ->language()
    ->getId();
  $this->sourceLangcode = $element['#source_langcode'];
  $element_uuid = $element['#uuid'];
  $preview_view_mode = $this->layoutParagraphsLayout
    ->getSetting('preview_view_mode', 'default');
  $element['#components'] = [];
  if ($this
    ->isTranslating()) {
    $this
      ->initTranslations();
    $element['#translation_warning'] = $this
      ->translationWarning();
  }

  // Build a flat list of component build arrays.
  foreach ($this->layoutParagraphsLayout
    ->getComponents() as $component) {

    /** @var \Drupal\layout_paragraphs\LayoutParagraphsComponent $component */
    $element['#components'][$component
      ->getEntity()
      ->uuid()] = $this
      ->buildComponent($component, $preview_view_mode);
  }

  // Nest child components inside their respective sections and regions.
  foreach ($this->layoutParagraphsLayout
    ->getComponents() as $component) {

    /** @var \Drupal\layout_paragraphs\LayoutParagraphsComponent $component */
    $uuid = $component
      ->getEntity()
      ->uuid();
    if ($component
      ->isLayout()) {
      $section = $this->layoutParagraphsLayout
        ->getLayoutSection($component
        ->getEntity());
      $layout_plugin_instance = $this
        ->layoutPluginInstance($section);
      foreach (array_keys($element['#components'][$uuid]['regions']) as $region_name) {
        foreach ($section
          ->getComponentsForRegion($region_name) as $child_component) {
          $child_uuid = $child_component
            ->getEntity()
            ->uuid();
          $element['#components'][$uuid]['regions'][$region_name][$child_uuid] =& $element['#components'][$child_uuid];
        }
      }
      $element['#components'][$uuid]['regions'] = $layout_plugin_instance
        ->build($element['#components'][$uuid]['regions']);
      $element['#components'][$uuid]['regions']['#weight'] = 1000;
    }
  }

  // If a element #uuid is provided, render the matching element.
  // This is used in cases where a single component needs
  // to be rendered - for example, as part of an AJAX response.
  if ($element_uuid) {
    if (isset($element['#components'][$element_uuid])) {
      return [
        'build' => $element['#components'][$element_uuid],
      ];
    }
  }
  $element['#attributes'] = [
    'class' => [
      'lp-builder',
      'lp-builder-' . $this->layoutParagraphsLayout
        ->id(),
    ],
    'data-lpb-id' => $this->layoutParagraphsLayout
      ->id(),
  ];
  $element['#attached']['library'] = [
    'layout_paragraphs/builder',
  ];
  $element['#attached']['drupalSettings']['lpBuilder'][$this->layoutParagraphsLayout
    ->id()] = $this->layoutParagraphsLayout
    ->getSettings();
  $element['#is_empty'] = $this->layoutParagraphsLayout
    ->isEmpty();
  $element['#empty_message'] = $this->layoutParagraphsLayout
    ->getSetting('empty_message', $this
    ->t('Start adding content.'));
  if ($this->layoutParagraphsLayout
    ->getSetting('require_layouts', FALSE)) {
    $element['#insert_button'] = $this
      ->insertSectionButton([
      'layout_paragraphs_layout' => $this->layoutParagraphsLayout
        ->id(),
    ]);
  }
  else {
    $element['#insert_button'] = $this
      ->insertComponentButton([
      'layout_paragraphs_layout' => $this->layoutParagraphsLayout
        ->id(),
    ]);
  }
  $element['#root_components'] = [];
  foreach ($this->layoutParagraphsLayout
    ->getRootComponents() as $component) {

    /** @var \Drupal\layout_paragraphs\LayoutParagraphsComponent $component */
    $uuid = $component
      ->getEntity()
      ->uuid();
    $element['#root_components'][$uuid] =& $element['#components'][$uuid];
  }
  if (count($element['#root_components'])) {
    $element['#attributes']['class'][] = 'has-components';
  }
  return $element;
}