comparison core/modules/layout_builder/src/Controller/ChooseBlockController.php @ 14:1fec387a4317

Update Drupal core to 8.5.2 via Composer
author Chris Cannam
date Mon, 23 Apr 2018 09:46:53 +0100
parents
children 129ea1e6d783
comparison
equal deleted inserted replaced
13:5fb285c0d0e3 14:1fec387a4317
1 <?php
2
3 namespace Drupal\layout_builder\Controller;
4
5 use Drupal\Core\Block\BlockManagerInterface;
6 use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
7 use Drupal\Core\Url;
8 use Drupal\layout_builder\Context\LayoutBuilderContextTrait;
9 use Drupal\layout_builder\SectionStorageInterface;
10 use Symfony\Component\DependencyInjection\ContainerInterface;
11
12 /**
13 * Defines a controller to choose a new block.
14 *
15 * @internal
16 */
17 class ChooseBlockController implements ContainerInjectionInterface {
18
19 use AjaxHelperTrait;
20 use LayoutBuilderContextTrait;
21
22 /**
23 * The block manager.
24 *
25 * @var \Drupal\Core\Block\BlockManagerInterface
26 */
27 protected $blockManager;
28
29 /**
30 * ChooseBlockController constructor.
31 *
32 * @param \Drupal\Core\Block\BlockManagerInterface $block_manager
33 * The block manager.
34 */
35 public function __construct(BlockManagerInterface $block_manager) {
36 $this->blockManager = $block_manager;
37 }
38
39 /**
40 * {@inheritdoc}
41 */
42 public static function create(ContainerInterface $container) {
43 return new static(
44 $container->get('plugin.manager.block')
45 );
46 }
47
48 /**
49 * Provides the UI for choosing a new block.
50 *
51 * @param \Drupal\layout_builder\SectionStorageInterface $section_storage
52 * The section storage.
53 * @param int $delta
54 * The delta of the section to splice.
55 * @param string $region
56 * The region the block is going in.
57 *
58 * @return array
59 * A render array.
60 */
61 public function build(SectionStorageInterface $section_storage, $delta, $region) {
62 $build['#type'] = 'container';
63 $build['#attributes']['class'][] = 'block-categories';
64
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'] = [
71 '#theme' => 'links',
72 ];
73 foreach ($blocks as $block_id => $block) {
74 $link = [
75 'title' => $block['admin_label'],
76 'url' => Url::fromRoute('layout_builder.add_block',
77 [
78 'section_storage_type' => $section_storage->getStorageType(),
79 'section_storage' => $section_storage->getStorageId(),
80 'delta' => $delta,
81 'region' => $region,
82 'plugin_id' => $block_id,
83 ]
84 ),
85 ];
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';
90 }
91 $build[$category]['links']['#links'][] = $link;
92 }
93 }
94 return $build;
95 }
96
97 }