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