You are here

public function ChooseBlockController::build in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/layout_builder/src/Controller/ChooseBlockController.php \Drupal\layout_builder\Controller\ChooseBlockController::build()

Provides the UI for choosing a new block.

Parameters

\Drupal\layout_builder\SectionStorageInterface $section_storage: The section storage.

int $delta: The delta of the section to splice.

string $region: The region the block is going in.

Return value

array A render array.

1 string reference to 'ChooseBlockController::build'
layout_builder.routing.yml in core/modules/layout_builder/layout_builder.routing.yml
core/modules/layout_builder/layout_builder.routing.yml

File

core/modules/layout_builder/src/Controller/ChooseBlockController.php, line 95

Class

ChooseBlockController
Defines a controller to choose a new block.

Namespace

Drupal\layout_builder\Controller

Code

public function build(SectionStorageInterface $section_storage, $delta, $region) {
  if ($this->entityTypeManager
    ->hasDefinition('block_content_type') && ($types = $this->entityTypeManager
    ->getStorage('block_content_type')
    ->loadMultiple())) {
    if (count($types) === 1) {
      $type = reset($types);
      $plugin_id = 'inline_block:' . $type
        ->id();
      if ($this->blockManager
        ->hasDefinition($plugin_id)) {
        $url = Url::fromRoute('layout_builder.add_block', [
          'section_storage_type' => $section_storage
            ->getStorageType(),
          'section_storage' => $section_storage
            ->getStorageId(),
          'delta' => $delta,
          'region' => $region,
          'plugin_id' => $plugin_id,
        ]);
      }
    }
    else {
      $url = Url::fromRoute('layout_builder.choose_inline_block', [
        'section_storage_type' => $section_storage
          ->getStorageType(),
        'section_storage' => $section_storage
          ->getStorageId(),
        'delta' => $delta,
        'region' => $region,
      ]);
    }
    if (isset($url)) {
      $build['add_block'] = [
        '#type' => 'link',
        '#url' => $url,
        '#title' => $this
          ->t('Create @entity_type', [
          '@entity_type' => $this->entityTypeManager
            ->getDefinition('block_content')
            ->getSingularLabel(),
        ]),
        '#attributes' => $this
          ->getAjaxAttributes(),
        '#access' => $this->currentUser
          ->hasPermission('create and edit custom blocks'),
      ];
      $build['add_block']['#attributes']['class'][] = 'inline-block-create-button';
    }
  }
  $build['filter'] = [
    '#type' => 'search',
    '#title' => $this
      ->t('Filter by block name'),
    '#title_display' => 'invisible',
    '#size' => 30,
    '#placeholder' => $this
      ->t('Filter by block name'),
    '#attributes' => [
      'class' => [
        'js-layout-builder-filter',
      ],
      'title' => $this
        ->t('Enter a part of the block name to filter by.'),
    ],
  ];
  $block_categories['#type'] = 'container';
  $block_categories['#attributes']['class'][] = 'block-categories';
  $block_categories['#attributes']['class'][] = 'js-layout-builder-categories';
  $block_categories['#attributes']['data-layout-builder-target-highlight-id'] = $this
    ->blockAddHighlightId($delta, $region);

  // @todo Explicitly cast delta to an integer, remove this in
  //   https://www.drupal.org/project/drupal/issues/2984509.
  $delta = (int) $delta;
  $definitions = $this->blockManager
    ->getFilteredDefinitions('layout_builder', $this
    ->getAvailableContexts($section_storage), [
    'section_storage' => $section_storage,
    'delta' => $delta,
    'region' => $region,
  ]);
  $grouped_definitions = $this->blockManager
    ->getGroupedDefinitions($definitions);
  foreach ($grouped_definitions as $category => $blocks) {
    $block_categories[$category]['#type'] = 'details';
    $block_categories[$category]['#attributes']['class'][] = 'js-layout-builder-category';
    $block_categories[$category]['#open'] = TRUE;
    $block_categories[$category]['#title'] = $category;
    $block_categories[$category]['links'] = $this
      ->getBlockLinks($section_storage, $delta, $region, $blocks);
  }
  $build['block_categories'] = $block_categories;
  return $build;
}