3 namespace Drupal\layout_builder\Controller;
5 use Drupal\Core\Block\BlockManagerInterface;
6 use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
8 use Drupal\layout_builder\Context\LayoutBuilderContextTrait;
9 use Drupal\layout_builder\SectionStorageInterface;
10 use Symfony\Component\DependencyInjection\ContainerInterface;
13 * Defines a controller to choose a new block.
17 class ChooseBlockController implements ContainerInjectionInterface {
20 use LayoutBuilderContextTrait;
25 * @var \Drupal\Core\Block\BlockManagerInterface
27 protected $blockManager;
30 * ChooseBlockController constructor.
32 * @param \Drupal\Core\Block\BlockManagerInterface $block_manager
35 public function __construct(BlockManagerInterface $block_manager) {
36 $this->blockManager = $block_manager;
42 public static function create(ContainerInterface $container) {
44 $container->get('plugin.manager.block')
49 * Provides the UI for choosing a new block.
51 * @param \Drupal\layout_builder\SectionStorageInterface $section_storage
52 * The section storage.
54 * The delta of the section to splice.
55 * @param string $region
56 * The region the block is going in.
61 public function build(SectionStorageInterface $section_storage, $delta, $region) {
62 $build['#type'] = 'container';
63 $build['#attributes']['class'][] = 'block-categories';
65 $definitions = $this->blockManager->getDefinitionsForContexts($this->getAvailableContexts($section_storage));
66 foreach ($this->blockManager->getGroupedDefinitions($definitions) as $category => $blocks) {
67 $build[$category]['#type'] = 'details';
68 $build[$category]['#open'] = TRUE;
69 $build[$category]['#title'] = $category;
70 $build[$category]['links'] = [
73 foreach ($blocks as $block_id => $block) {
75 'title' => $block['admin_label'],
76 'url' => Url::fromRoute('layout_builder.add_block',
78 'section_storage_type' => $section_storage->getStorageType(),
79 'section_storage' => $section_storage->getStorageId(),
82 'plugin_id' => $block_id,
86 if ($this->isAjax()) {
87 $link['attributes']['class'][] = 'use-ajax';
88 $link['attributes']['data-dialog-type'][] = 'dialog';
89 $link['attributes']['data-dialog-renderer'][] = 'off_canvas';
91 $build[$category]['links']['#links'][] = $link;