annotate core/modules/layout_builder/src/Controller/ChooseSectionController.php @ 19:fa3358dc1485 tip

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