annotate core/modules/layout_builder/src/Controller/ChooseSectionController.php @ 0:c75dbcec494b

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