You are here

public function PlaceBlockPageVariant::build in Drupal 8

Builds and returns the renderable array for the display variant.

The variant can contain cacheability metadata for the configuration that was passed in setConfiguration(). In the build() method, this should be added to the render array that is returned.

Return value

array A render array for the display variant.

Overrides BlockPageVariant::build

File

core/modules/block_place/src/Plugin/DisplayVariant/PlaceBlockPageVariant.php, line 91

Class

PlaceBlockPageVariant
Allows blocks to be placed directly within a region.

Namespace

Drupal\block_place\Plugin\DisplayVariant

Code

public function build() {
  $build = parent::build();
  $active_theme = $this->themeManager
    ->getActiveTheme();
  $theme_name = $active_theme
    ->getName();
  $destination = $this->redirectDestination
    ->get();
  $visible_regions = $this
    ->getVisibleRegionNames($theme_name);

  // Build an array of the region names in the right order.
  $build += array_fill_keys(array_keys($visible_regions), []);
  foreach ($visible_regions as $region => $region_name) {
    $query = [
      'region' => $region,
    ];
    if ($destination) {
      $query['destination'] = $destination;
    }
    $title = $this
      ->t('<span class="visually-hidden">Place block in the %region region</span>', [
      '%region' => $region_name,
    ]);
    $operations['block_description'] = [
      '#type' => 'inline_template',
      '#template' => '<div class="block-place-region">{{ link }}</div>',
      '#context' => [
        'link' => Link::createFromRoute($title, 'block.admin_library', [
          'theme' => $theme_name,
        ], [
          'query' => $query,
          'attributes' => [
            'title' => $title,
            'class' => [
              'use-ajax',
              'button',
              'button--small',
            ],
            'data-dialog-type' => 'modal',
            'data-dialog-options' => Json::encode([
              'width' => 700,
            ]),
          ],
        ]),
      ],
    ];
    $build[$region] = [
      'block_place_operations' => $operations,
    ] + $build[$region];
  }
  $build['#attached']['library'][] = 'block_place/drupal.block_place';
  return $build;
}