3 namespace Drupal\layout_builder\Controller;
5 use Drupal\Core\Ajax\AjaxHelperTrait;
6 use Drupal\Core\Block\BlockManagerInterface;
7 use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
8 use Drupal\Core\StringTranslation\StringTranslationTrait;
10 use Drupal\layout_builder\Context\LayoutBuilderContextTrait;
11 use Drupal\layout_builder\SectionStorageInterface;
12 use Symfony\Component\DependencyInjection\ContainerInterface;
15 * Defines a controller to choose a new block.
19 class ChooseBlockController implements ContainerInjectionInterface {
22 use LayoutBuilderContextTrait;
23 use StringTranslationTrait;
28 * @var \Drupal\Core\Block\BlockManagerInterface
30 protected $blockManager;
33 * ChooseBlockController constructor.
35 * @param \Drupal\Core\Block\BlockManagerInterface $block_manager
38 public function __construct(BlockManagerInterface $block_manager) {
39 $this->blockManager = $block_manager;
45 public static function create(ContainerInterface $container) {
47 $container->get('plugin.manager.block')
52 * Provides the UI for choosing a new block.
54 * @param \Drupal\layout_builder\SectionStorageInterface $section_storage
55 * The section storage.
57 * The delta of the section to splice.
58 * @param string $region
59 * The region the block is going in.
64 public function build(SectionStorageInterface $section_storage, $delta, $region) {
65 $build['#title'] = $this->t('Choose a block');
66 $build['#type'] = 'container';
67 $build['#attributes']['class'][] = 'block-categories';
69 // @todo Explicitly cast delta to an integer, remove this in
70 // https://www.drupal.org/project/drupal/issues/2984509.
71 $delta = (int) $delta;
73 $definitions = $this->blockManager->getFilteredDefinitions('layout_builder', $this->getAvailableContexts($section_storage), [
74 'section_storage' => $section_storage,
78 foreach ($this->blockManager->getGroupedDefinitions($definitions) as $category => $blocks) {
79 $build[$category]['#type'] = 'details';
80 $build[$category]['#open'] = TRUE;
81 $build[$category]['#title'] = $category;
82 $build[$category]['links'] = [
85 foreach ($blocks as $block_id => $block) {
87 'title' => $block['admin_label'],
88 'url' => Url::fromRoute('layout_builder.add_block',
90 'section_storage_type' => $section_storage->getStorageType(),
91 'section_storage' => $section_storage->getStorageId(),
94 'plugin_id' => $block_id,
98 if ($this->isAjax()) {
99 $link['attributes']['class'][] = 'use-ajax';
100 $link['attributes']['data-dialog-type'][] = 'dialog';
101 $link['attributes']['data-dialog-renderer'][] = 'off_canvas';
103 $build[$category]['links']['#links'][] = $link;