You are here

protected function LayoutParagraphsBuilder::buildComponent in Layout Paragraphs 2.0.x

Returns the build array for a single layout component.

Parameters

\Drupal\layout_paragraphs\LayoutParagraphsComponent $component: The component to render.

string $preview_view_mode: The view mode to use for rendering paragraphs.

Return value

array The build array.

1 call to LayoutParagraphsBuilder::buildComponent()
LayoutParagraphsBuilder::preRender in src/Element/LayoutParagraphsBuilder.php
Pre-render callback: Renders the UI.

File

src/Element/LayoutParagraphsBuilder.php, line 262

Class

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

Namespace

Drupal\layout_paragraphs\Element

Code

protected function buildComponent(LayoutParagraphsComponent $component, $preview_view_mode = 'default') {
  $entity = $component
    ->getEntity();
  $view_builder = $this->entityTypeManager
    ->getViewBuilder($entity
    ->getEntityTypeId());
  $build = $view_builder
    ->view($entity, $preview_view_mode, $entity
    ->language()
    ->getId());
  $build['#post_render'] = [
    [
      $this,
      'postRenderComponent',
    ],
  ];
  $build['#attributes']['data-uuid'] = $entity
    ->uuid();
  $build['#attributes']['data-type'] = $entity
    ->bundle();
  $build['#attributes']['data-id'] = $entity
    ->id();
  $build['#attributes']['class'][] = 'js-lpb-component';
  if ($entity
    ->isNew()) {
    $build['#attributes']['class'][] = 'is_new';
  }
  $url_params = [
    'layout_paragraphs_layout' => $this->layoutParagraphsLayout
      ->id(),
  ];
  $query_params = [
    'sibling_uuid' => $entity
      ->uuid(),
  ];
  if ($parent_uuid = $component
    ->getParentUuid()) {
    $query_params['parent_uuid'] = $parent_uuid;
  }
  if ($region = $component
    ->getRegion()) {
    $query_params['region'] = $region;
  }
  if ($this
    ->editAccess($entity)) {
    $edit_attributes = new Attribute([
      'href' => Url::fromRoute('layout_paragraphs.builder.edit_item', [
        'layout_paragraphs_layout' => $this->layoutParagraphsLayout
          ->id(),
        'component_uuid' => $entity
          ->uuid(),
      ])
        ->toString(),
      'class' => [
        'lpb-edit',
        'use-ajax',
      ],
      'data-dialog-type' => 'dialog',
      'data-dialog-options' => Json::encode($this
        ->dialogSettings($this->layoutParagraphsLayout)),
    ]);
  }
  else {
    $edit_attributes = [];
  }
  if ($this
    ->deleteAccess($entity)) {
    $delete_attributes = new Attribute([
      'href' => Url::fromRoute('layout_paragraphs.builder.delete_item', [
        'layout_paragraphs_layout' => $this->layoutParagraphsLayout
          ->id(),
        'component_uuid' => $entity
          ->uuid(),
      ])
        ->toString(),
      'class' => [
        'lpb-delete',
        'use-ajax',
      ],
      'data-dialog-type' => 'dialog',
      'data-dialog-options' => Json::encode([
        'modal' => TRUE,
        'target' => $this
          ->dialogId($this->layoutParagraphsLayout),
      ]),
    ]);
  }
  else {
    $delete_attributes = [];
  }
  $build['controls'] = [
    '#theme' => 'layout_paragraphs_builder_controls',
    '#attributes' => [
      'class' => [
        'lpb-controls',
      ],
    ],
    '#label' => $entity
      ->getParagraphType()->label,
    '#edit_attributes' => $edit_attributes,
    '#delete_attributes' => $delete_attributes,
    '#weight' => -10001,
  ];
  if ($component
    ->isLayout()) {
    $build['controls']['#attributes']['class'][] = 'is-layout';
  }
  if ($this
    ->createAccess()) {
    if (!$component
      ->getParentUuid() && $this->layoutParagraphsLayout
      ->getSetting('require_layouts')) {
      $build['insert_before'] = $this
        ->insertSectionButton($url_params, $query_params + [
        'placement' => 'before',
      ], -10000, [
        'before',
      ]);
      $build['insert_after'] = $this
        ->insertSectionButton($url_params, $query_params + [
        'placement' => 'after',
      ], 10000, [
        'after',
      ]);
    }
    else {
      $build['insert_before'] = $this
        ->insertComponentButton($url_params, $query_params + [
        'placement' => 'before',
      ], -10000, [
        'before',
      ]);
      $build['insert_after'] = $this
        ->insertComponentButton($url_params, $query_params + [
        'placement' => 'after',
      ], 10000, [
        'after',
      ]);
    }
  }
  if ($component
    ->isLayout()) {
    $section = $this->layoutParagraphsLayout
      ->getLayoutSection($entity);
    $layout_instance = $this
      ->layoutPluginInstance($section);
    $region_names = $layout_instance
      ->getPluginDefinition()
      ->getRegionNames();
    $build['#attributes']['class'][] = 'lpb-layout';
    $build['#attributes']['data-layout'] = $section
      ->getLayoutId();
    $build['#layout_plugin_instance'] = $layout_instance;
    $build['regions'] = [];
    foreach ($region_names as $region_name) {
      $url_params = [
        'layout_paragraphs_layout' => $this->layoutParagraphsLayout
          ->id(),
      ];
      $query_params = [
        'parent_uuid' => $entity
          ->uuid(),
        'region' => $region_name,
      ];
      $build['regions'][$region_name] = [
        '#attributes' => [
          'class' => [
            'js-lpb-region',
          ],
          'data-region' => $region_name,
          'data-region-uuid' => $entity
            ->uuid() . '-' . $region_name,
        ],
      ];
      if ($this
        ->createAccess()) {
        $build['regions'][$region_name]['insert_button'] = $this
          ->insertComponentButton($url_params, $query_params, 10000, [
          'center',
        ]);
      }
    }
  }
  return $build;
}