annotate 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
rev   line source
Chris@14 1 <?php
Chris@14 2
Chris@14 3 namespace Drupal\layout_builder\Controller;
Chris@14 4
Chris@14 5 use Drupal\Core\Block\BlockManagerInterface;
Chris@14 6 use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
Chris@14 7 use Drupal\Core\Url;
Chris@14 8 use Drupal\layout_builder\Context\LayoutBuilderContextTrait;
Chris@14 9 use Drupal\layout_builder\SectionStorageInterface;
Chris@14 10 use Symfony\Component\DependencyInjection\ContainerInterface;
Chris@14 11
Chris@14 12 /**
Chris@14 13 * Defines a controller to choose a new block.
Chris@14 14 *
Chris@14 15 * @internal
Chris@14 16 */
Chris@14 17 class ChooseBlockController implements ContainerInjectionInterface {
Chris@14 18
Chris@14 19 use AjaxHelperTrait;
Chris@14 20 use LayoutBuilderContextTrait;
Chris@14 21
Chris@14 22 /**
Chris@14 23 * The block manager.
Chris@14 24 *
Chris@14 25 * @var \Drupal\Core\Block\BlockManagerInterface
Chris@14 26 */
Chris@14 27 protected $blockManager;
Chris@14 28
Chris@14 29 /**
Chris@14 30 * ChooseBlockController constructor.
Chris@14 31 *
Chris@14 32 * @param \Drupal\Core\Block\BlockManagerInterface $block_manager
Chris@14 33 * The block manager.
Chris@14 34 */
Chris@14 35 public function __construct(BlockManagerInterface $block_manager) {
Chris@14 36 $this->blockManager = $block_manager;
Chris@14 37 }
Chris@14 38
Chris@14 39 /**
Chris@14 40 * {@inheritdoc}
Chris@14 41 */
Chris@14 42 public static function create(ContainerInterface $container) {
Chris@14 43 return new static(
Chris@14 44 $container->get('plugin.manager.block')
Chris@14 45 );
Chris@14 46 }
Chris@14 47
Chris@14 48 /**
Chris@14 49 * Provides the UI for choosing a new block.
Chris@14 50 *
Chris@14 51 * @param \Drupal\layout_builder\SectionStorageInterface $section_storage
Chris@14 52 * The section storage.
Chris@14 53 * @param int $delta
Chris@14 54 * The delta of the section to splice.
Chris@14 55 * @param string $region
Chris@14 56 * The region the block is going in.
Chris@14 57 *
Chris@14 58 * @return array
Chris@14 59 * A render array.
Chris@14 60 */
Chris@14 61 public function build(SectionStorageInterface $section_storage, $delta, $region) {
Chris@14 62 $build['#type'] = 'container';
Chris@14 63 $build['#attributes']['class'][] = 'block-categories';
Chris@14 64
Chris@14 65 $definitions = $this->blockManager->getDefinitionsForContexts($this->getAvailableContexts($section_storage));
Chris@14 66 foreach ($this->blockManager->getGroupedDefinitions($definitions) as $category => $blocks) {
Chris@14 67 $build[$category]['#type'] = 'details';
Chris@14 68 $build[$category]['#open'] = TRUE;
Chris@14 69 $build[$category]['#title'] = $category;
Chris@14 70 $build[$category]['links'] = [
Chris@14 71 '#theme' => 'links',
Chris@14 72 ];
Chris@14 73 foreach ($blocks as $block_id => $block) {
Chris@14 74 $link = [
Chris@14 75 'title' => $block['admin_label'],
Chris@14 76 'url' => Url::fromRoute('layout_builder.add_block',
Chris@14 77 [
Chris@14 78 'section_storage_type' => $section_storage->getStorageType(),
Chris@14 79 'section_storage' => $section_storage->getStorageId(),
Chris@14 80 'delta' => $delta,
Chris@14 81 'region' => $region,
Chris@14 82 'plugin_id' => $block_id,
Chris@14 83 ]
Chris@14 84 ),
Chris@14 85 ];
Chris@14 86 if ($this->isAjax()) {
Chris@14 87 $link['attributes']['class'][] = 'use-ajax';
Chris@14 88 $link['attributes']['data-dialog-type'][] = 'dialog';
Chris@14 89 $link['attributes']['data-dialog-renderer'][] = 'off_canvas';
Chris@14 90 }
Chris@14 91 $build[$category]['links']['#links'][] = $link;
Chris@14 92 }
Chris@14 93 }
Chris@14 94 return $build;
Chris@14 95 }
Chris@14 96
Chris@14 97 }