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