3 namespace Drupal\layout_builder\Controller;
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;
11 use Drupal\layout_builder\SectionStorageInterface;
12 use Symfony\Component\DependencyInjection\ContainerInterface;
15 * Defines a controller to choose a new section.
19 class ChooseSectionController implements ContainerInjectionInterface {
22 use StringTranslationTrait;
27 * @var \Drupal\Core\Layout\LayoutPluginManagerInterface
29 protected $layoutManager;
32 * ChooseSectionController constructor.
34 * @param \Drupal\Core\Layout\LayoutPluginManagerInterface $layout_manager
37 public function __construct(LayoutPluginManagerInterface $layout_manager) {
38 $this->layoutManager = $layout_manager;
44 public static function create(ContainerInterface $container) {
46 $container->get('plugin.manager.core.layout')
51 * Choose a layout plugin to add as a section.
53 * @param \Drupal\layout_builder\SectionStorageInterface $section_storage
54 * The section storage.
56 * The delta of the section to splice.
61 public function build(SectionStorageInterface $section_storage, $delta) {
62 $output['#title'] = $this->t('Choose a layout');
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);
71 $definition->getIcon(60, 80, 1, 3),
73 '#type' => 'container',
74 '#children' => $definition->getLabel(),
77 '#url' => Url::fromRoute(
78 $layout instanceof PluginFormInterface ? 'layout_builder.configure_section' : 'layout_builder.add_section',
80 'section_storage_type' => $section_storage->getStorageType(),
81 'section_storage' => $section_storage->getStorageId(),
83 'plugin_id' => $plugin_id,
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';
94 $output['layouts'] = [
95 '#theme' => 'item_list',