You are here

public function SimpleBlockAddControllerSubscriber::alterBuild in Simple Block 8

Alters the build produced by ChooseBlockController::build().

Parameters

\Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent $event: The kernel view event.

File

modules/simple_block_layout_builder/src/Event/Subscriber/SimpleBlockAddControllerSubscriber.php, line 75

Class

SimpleBlockAddControllerSubscriber
Alters the render array of Layout Builder ChooseBlockController::build().

Namespace

Drupal\simple_block_layout_builder\Event\Subscriber

Code

public function alterBuild(GetResponseForControllerResultEvent $event) : void {
  $route_name = $event
    ->getRequest()->attributes
    ->get('_route');
  if ($route_name !== 'layout_builder.choose_block') {
    return;
  }
  $original_build = $event
    ->getControllerResult();
  if (is_array($original_build)) {
    $section_storage = $this->routeMatch
      ->getParameter('section_storage');
    if (!$section_storage instanceof SectionStorageInterface) {
      throw new \InvalidArgumentException("Parameter 'section_storage' should implement \\Drupal\\layout_builder\\SectionStorageInterface");
    }
    $build = [];

    // Always keep 'add_block' on top, if exists.
    if (isset($original_build['add_block'])) {
      $build['add_block'] = $original_build['add_block'];
      unset($original_build['add_block']);
    }
    $build['add_simple_block'] = [
      '#type' => 'link',
      '#title' => $this
        ->t('Create simple block'),
      '#url' => Url::fromRoute('simple_block_layout_builder.edit_block', [
        'section_storage_type' => $section_storage
          ->getStorageType(),
        'section_storage' => $section_storage
          ->getStorageId(),
        'delta' => $this->routeMatch
          ->getParameter('delta'),
        'region' => $this->routeMatch
          ->getParameter('region'),
        'uuid' => NULL,
        'simple_block' => NULL,
      ]),
      '#attributes' => $this
        ->getAttributes(),
      '#access' => $this->currentUser
        ->hasPermission('administer blocks'),
    ];
    $event
      ->setControllerResult($build + $original_build);
  }
}