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['#title'] = $this->t('Choose a block'); $build['#type'] = 'container'; $build['#attributes']['class'][] = 'block-categories'; // @todo Explicitly cast delta to an integer, remove this in // https://www.drupal.org/project/drupal/issues/2984509. $delta = (int) $delta; $definitions = $this->blockManager->getFilteredDefinitions('layout_builder', $this->getAvailableContexts($section_storage), [ 'section_storage' => $section_storage, 'delta' => $delta, 'region' => $region, ]); 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; } }