Updated to Drupal 8.6.4, which is PHP 7.3 friendly. Also updated HTMLaw library....
[yaffs-website] / web / core / modules / layout_builder / src / Controller / ChooseBlockController.php
1 <?php
2
3 namespace Drupal\layout_builder\Controller;
4
5 use Drupal\Core\Ajax\AjaxHelperTrait;
6 use Drupal\Core\Block\BlockManagerInterface;
7 use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
8 use Drupal\Core\StringTranslation\StringTranslationTrait;
9 use Drupal\Core\Url;
10 use Drupal\layout_builder\Context\LayoutBuilderContextTrait;
11 use Drupal\layout_builder\SectionStorageInterface;
12 use Symfony\Component\DependencyInjection\ContainerInterface;
13
14 /**
15  * Defines a controller to choose a new block.
16  *
17  * @internal
18  */
19 class ChooseBlockController implements ContainerInjectionInterface {
20
21   use AjaxHelperTrait;
22   use LayoutBuilderContextTrait;
23   use StringTranslationTrait;
24
25   /**
26    * The block manager.
27    *
28    * @var \Drupal\Core\Block\BlockManagerInterface
29    */
30   protected $blockManager;
31
32   /**
33    * ChooseBlockController constructor.
34    *
35    * @param \Drupal\Core\Block\BlockManagerInterface $block_manager
36    *   The block manager.
37    */
38   public function __construct(BlockManagerInterface $block_manager) {
39     $this->blockManager = $block_manager;
40   }
41
42   /**
43    * {@inheritdoc}
44    */
45   public static function create(ContainerInterface $container) {
46     return new static(
47       $container->get('plugin.manager.block')
48     );
49   }
50
51   /**
52    * Provides the UI for choosing a new block.
53    *
54    * @param \Drupal\layout_builder\SectionStorageInterface $section_storage
55    *   The section storage.
56    * @param int $delta
57    *   The delta of the section to splice.
58    * @param string $region
59    *   The region the block is going in.
60    *
61    * @return array
62    *   A render array.
63    */
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';
68
69     // @todo Explicitly cast delta to an integer, remove this in
70     //   https://www.drupal.org/project/drupal/issues/2984509.
71     $delta = (int) $delta;
72
73     $definitions = $this->blockManager->getFilteredDefinitions('layout_builder', $this->getAvailableContexts($section_storage), [
74       'section_storage' => $section_storage,
75       'delta' => $delta,
76       'region' => $region,
77     ]);
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'] = [
83         '#theme' => 'links',
84       ];
85       foreach ($blocks as $block_id => $block) {
86         $link = [
87           'title' => $block['admin_label'],
88           'url' => Url::fromRoute('layout_builder.add_block',
89             [
90               'section_storage_type' => $section_storage->getStorageType(),
91               'section_storage' => $section_storage->getStorageId(),
92               'delta' => $delta,
93               'region' => $region,
94               'plugin_id' => $block_id,
95             ]
96           ),
97         ];
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';
102         }
103         $build[$category]['links']['#links'][] = $link;
104       }
105     }
106     return $build;
107   }
108
109 }