annotate core/modules/layout_builder/src/Controller/ChooseSectionController.php @ 17:129ea1e6d783

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