You are here

public static function LayoutBuilderIdsRenderSubscriber::onBuildRender in Layout builder ids 2.0.x

Same name and namespace in other branches
  1. 8 src/EventSubscriber/LayoutBuilderIdsRenderSubscriber.php \Drupal\layout_builder_ids\EventSubscriber\LayoutBuilderIdsRenderSubscriber::onBuildRender()

Add each component's block styles to the render array.

Parameters

\Drupal\layout_builder\Event\SectionComponentBuildRenderArrayEvent $event: The section component render event.

File

src/EventSubscriber/LayoutBuilderIdsRenderSubscriber.php, line 35

Class

LayoutBuilderIdsRenderSubscriber
Render the layout with block ids.

Namespace

Drupal\layout_builder_ids\EventSubscriber

Code

public static function onBuildRender(SectionComponentBuildRenderArrayEvent $event) {

  // The render array.
  $build = $event
    ->getBuild();

  // This shouldn't happen - Layout Builder should have already created the
  // initial build data.
  if (empty($build)) {
    return;
  }

  // Get the layout builder id.
  $layout_builder_id = $event
    ->getComponent()
    ->get('layout_builder_id');

  // If there is a layout builder id, then set it in the attributes.
  if ((string) $layout_builder_id) {

    // Set the id attribute.
    $build['#attributes']['id'] = $layout_builder_id;

    // Now set the build for the event.
    $event
      ->setBuild($build);
  }
}