class ChooseSectionController

Same name in other branches
  1. 8.9.x core/modules/layout_builder/src/Controller/ChooseSectionController.php \Drupal\layout_builder\Controller\ChooseSectionController
  2. 10 core/modules/layout_builder/src/Controller/ChooseSectionController.php \Drupal\layout_builder\Controller\ChooseSectionController
  3. 11.x core/modules/layout_builder/src/Controller/ChooseSectionController.php \Drupal\layout_builder\Controller\ChooseSectionController

Defines a controller to choose a new section.

@internal Controller classes are internal.

Hierarchy

Expanded class hierarchy of ChooseSectionController

File

core/modules/layout_builder/src/Controller/ChooseSectionController.php, line 22

Namespace

Drupal\layout_builder\Controller
View source
class ChooseSectionController implements ContainerInjectionInterface {
    use AjaxHelperTrait;
    use LayoutBuilderContextTrait;
    use LayoutBuilderHighlightTrait;
    use StringTranslationTrait;
    
    /**
     * The layout manager.
     *
     * @var \Drupal\Core\Layout\LayoutPluginManagerInterface
     */
    protected $layoutManager;
    
    /**
     * ChooseSectionController constructor.
     *
     * @param \Drupal\Core\Layout\LayoutPluginManagerInterface $layout_manager
     *   The layout manager.
     */
    public function __construct(LayoutPluginManagerInterface $layout_manager) {
        $this->layoutManager = $layout_manager;
    }
    
    /**
     * {@inheritdoc}
     */
    public static function create(ContainerInterface $container) {
        return new static($container->get('plugin.manager.core.layout'));
    }
    
    /**
     * Choose a layout plugin to add as a section.
     *
     * @param \Drupal\layout_builder\SectionStorageInterface $section_storage
     *   The section storage.
     * @param int $delta
     *   The delta of the section to splice.
     *
     * @return array
     *   The render array.
     */
    public function build(SectionStorageInterface $section_storage, int $delta) {
        $items = [];
        $definitions = $this->layoutManager
            ->getFilteredDefinitions('layout_builder', $this->getPopulatedContexts($section_storage), [
            'section_storage' => $section_storage,
        ]);
        foreach ($definitions as $plugin_id => $definition) {
            $layout = $this->layoutManager
                ->createInstance($plugin_id);
            $item = [
                '#type' => 'link',
                '#title' => [
                    'icon' => $definition->getIcon(60, 80, 1, 3),
                    'label' => [
                        '#type' => 'container',
                        '#children' => $definition->getLabel(),
                    ],
                ],
                '#url' => Url::fromRoute($layout instanceof PluginFormInterface ? 'layout_builder.configure_section' : 'layout_builder.add_section', [
                    'section_storage_type' => $section_storage->getStorageType(),
                    'section_storage' => $section_storage->getStorageId(),
                    'delta' => $delta,
                    'plugin_id' => $plugin_id,
                ]),
            ];
            if ($this->isAjax()) {
                $item['#attributes']['class'][] = 'use-ajax';
                $item['#attributes']['data-dialog-type'][] = 'dialog';
                $item['#attributes']['data-dialog-renderer'][] = 'off_canvas';
            }
            $items[$plugin_id] = $item;
        }
        $output['layouts'] = [
            '#theme' => 'item_list__layouts',
            '#items' => $items,
            '#attributes' => [
                'class' => [
                    'layout-selection',
                ],
                'data-layout-builder-target-highlight-id' => $this->sectionAddHighlightId($delta),
            ],
        ];
        return $output;
    }

}

Members

Title Sort descending Deprecated Modifiers Object type Summary Overriden Title Overrides
AjaxHelperTrait::getRequestWrapperFormat protected function Gets the wrapper format of the current request.
AjaxHelperTrait::isAjax protected function Determines if the current request is via AJAX.
ChooseSectionController::$layoutManager protected property The layout manager.
ChooseSectionController::build public function Choose a layout plugin to add as a section.
ChooseSectionController::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create
ChooseSectionController::__construct public function ChooseSectionController constructor.
LayoutBuilderContextTrait::$contextRepository protected property The context repository.
LayoutBuilderContextTrait::contextRepository protected function Gets the context repository service.
LayoutBuilderContextTrait::getAvailableContexts Deprecated protected function Provides all available contexts, both global and section_storage-specific.
LayoutBuilderContextTrait::getPopulatedContexts protected function Returns all populated contexts, both global and section-storage-specific.
LayoutBuilderHighlightTrait::blockAddHighlightId protected function Provides the ID used to highlight the active Layout Builder UI element.
LayoutBuilderHighlightTrait::blockUpdateHighlightId protected function Provides the ID used to highlight the active Layout Builder UI element.
LayoutBuilderHighlightTrait::sectionAddHighlightId protected function Provides the ID used to highlight the active Layout Builder UI element.
LayoutBuilderHighlightTrait::sectionUpdateHighlightId protected function Provides the ID used to highlight the active Layout Builder UI element.
StringTranslationTrait::$stringTranslation protected property The string translation service. 3
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.