"Fossies" - the Fresh Open Source Software Archive

Member "drupal-8.9.10/core/modules/layout_builder/src/Controller/ChooseSectionController.php" (26 Nov 2020, 3248 Bytes) of package /linux/www/drupal-8.9.10.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "ChooseSectionController.php" see the Fossies "Dox" file reference documentation.

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