Pull merge.
[yaffs-website] / web / core / modules / layout_builder / src / Controller / ChooseSectionController.php
1 <?php
2
3 namespace Drupal\layout_builder\Controller;
4
5 use Drupal\Core\Ajax\AjaxHelperTrait;
6 use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
7 use Drupal\Core\Layout\LayoutPluginManagerInterface;
8 use Drupal\Core\Plugin\PluginFormInterface;
9 use Drupal\Core\StringTranslation\StringTranslationTrait;
10 use Drupal\Core\Url;
11 use Drupal\layout_builder\SectionStorageInterface;
12 use Symfony\Component\DependencyInjection\ContainerInterface;
13
14 /**
15  * Defines a controller to choose a new section.
16  *
17  * @internal
18  */
19 class ChooseSectionController implements ContainerInjectionInterface {
20
21   use AjaxHelperTrait;
22   use StringTranslationTrait;
23
24   /**
25    * The layout manager.
26    *
27    * @var \Drupal\Core\Layout\LayoutPluginManagerInterface
28    */
29   protected $layoutManager;
30
31   /**
32    * ChooseSectionController constructor.
33    *
34    * @param \Drupal\Core\Layout\LayoutPluginManagerInterface $layout_manager
35    *   The layout manager.
36    */
37   public function __construct(LayoutPluginManagerInterface $layout_manager) {
38     $this->layoutManager = $layout_manager;
39   }
40
41   /**
42    * {@inheritdoc}
43    */
44   public static function create(ContainerInterface $container) {
45     return new static(
46       $container->get('plugin.manager.core.layout')
47     );
48   }
49
50   /**
51    * Choose a layout plugin to add as a section.
52    *
53    * @param \Drupal\layout_builder\SectionStorageInterface $section_storage
54    *   The section storage.
55    * @param int $delta
56    *   The delta of the section to splice.
57    *
58    * @return array
59    *   The render array.
60    */
61   public function build(SectionStorageInterface $section_storage, $delta) {
62     $output['#title'] = $this->t('Choose a layout');
63
64     $items = [];
65     $definitions = $this->layoutManager->getFilteredDefinitions('layout_builder', [], ['section_storage' => $section_storage]);
66     foreach ($definitions as $plugin_id => $definition) {
67       $layout = $this->layoutManager->createInstance($plugin_id);
68       $item = [
69         '#type' => 'link',
70         '#title' => [
71           $definition->getIcon(60, 80, 1, 3),
72           [
73             '#type' => 'container',
74             '#children' => $definition->getLabel(),
75           ],
76         ],
77         '#url' => Url::fromRoute(
78           $layout instanceof PluginFormInterface ? 'layout_builder.configure_section' : 'layout_builder.add_section',
79           [
80             'section_storage_type' => $section_storage->getStorageType(),
81             'section_storage' => $section_storage->getStorageId(),
82             'delta' => $delta,
83             'plugin_id' => $plugin_id,
84           ]
85         ),
86       ];
87       if ($this->isAjax()) {
88         $item['#attributes']['class'][] = 'use-ajax';
89         $item['#attributes']['data-dialog-type'][] = 'dialog';
90         $item['#attributes']['data-dialog-renderer'][] = 'off_canvas';
91       }
92       $items[] = $item;
93     }
94     $output['layouts'] = [
95       '#theme' => 'item_list',
96       '#items' => $items,
97       '#attributes' => [
98         'class' => [
99           'layout-selection',
100         ],
101       ],
102     ];
103
104     return $output;
105   }
106
107 }