annotate core/modules/layout_builder/src/Controller/ChooseSectionController.php @ 5:12f9dff5fda9 tip

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