Mercurial > hg > isophonics-drupal-site
diff core/modules/layout_builder/src/Controller/ChooseBlockController.php @ 14:1fec387a4317
Update Drupal core to 8.5.2 via Composer
author | Chris Cannam |
---|---|
date | Mon, 23 Apr 2018 09:46:53 +0100 |
parents | |
children | 129ea1e6d783 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/core/modules/layout_builder/src/Controller/ChooseBlockController.php Mon Apr 23 09:46:53 2018 +0100 @@ -0,0 +1,97 @@ +<?php + +namespace Drupal\layout_builder\Controller; + +use Drupal\Core\Block\BlockManagerInterface; +use Drupal\Core\DependencyInjection\ContainerInjectionInterface; +use Drupal\Core\Url; +use Drupal\layout_builder\Context\LayoutBuilderContextTrait; +use Drupal\layout_builder\SectionStorageInterface; +use Symfony\Component\DependencyInjection\ContainerInterface; + +/** + * Defines a controller to choose a new block. + * + * @internal + */ +class ChooseBlockController implements ContainerInjectionInterface { + + use AjaxHelperTrait; + use LayoutBuilderContextTrait; + + /** + * The block manager. + * + * @var \Drupal\Core\Block\BlockManagerInterface + */ + protected $blockManager; + + /** + * ChooseBlockController constructor. + * + * @param \Drupal\Core\Block\BlockManagerInterface $block_manager + * The block manager. + */ + public function __construct(BlockManagerInterface $block_manager) { + $this->blockManager = $block_manager; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container) { + return new static( + $container->get('plugin.manager.block') + ); + } + + /** + * Provides the UI for choosing a new block. + * + * @param \Drupal\layout_builder\SectionStorageInterface $section_storage + * The section storage. + * @param int $delta + * The delta of the section to splice. + * @param string $region + * The region the block is going in. + * + * @return array + * A render array. + */ + public function build(SectionStorageInterface $section_storage, $delta, $region) { + $build['#type'] = 'container'; + $build['#attributes']['class'][] = 'block-categories'; + + $definitions = $this->blockManager->getDefinitionsForContexts($this->getAvailableContexts($section_storage)); + foreach ($this->blockManager->getGroupedDefinitions($definitions) as $category => $blocks) { + $build[$category]['#type'] = 'details'; + $build[$category]['#open'] = TRUE; + $build[$category]['#title'] = $category; + $build[$category]['links'] = [ + '#theme' => 'links', + ]; + foreach ($blocks as $block_id => $block) { + $link = [ + 'title' => $block['admin_label'], + 'url' => Url::fromRoute('layout_builder.add_block', + [ + 'section_storage_type' => $section_storage->getStorageType(), + 'section_storage' => $section_storage->getStorageId(), + 'delta' => $delta, + 'region' => $region, + 'plugin_id' => $block_id, + ] + ), + ]; + if ($this->isAjax()) { + $link['attributes']['class'][] = 'use-ajax'; + $link['attributes']['data-dialog-type'][] = 'dialog'; + $link['attributes']['data-dialog-renderer'][] = 'off_canvas'; + } + $build[$category]['links']['#links'][] = $link; + } + } + return $build; + } + +}