View source
<?php
namespace Drupal\block\Controller;
use Drupal\Component\Serialization\Json;
use Drupal\Core\Block\BlockManagerInterface;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\EventSubscriber\MainContentViewSubscriber;
use Drupal\Core\Menu\LocalActionManagerInterface;
use Drupal\Core\Plugin\Context\LazyContextRepository;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
class BlockLibraryController extends ControllerBase {
protected $blockManager;
protected $contextRepository;
protected $routeMatch;
protected $localActionManager;
public function __construct(BlockManagerInterface $block_manager, LazyContextRepository $context_repository, RouteMatchInterface $route_match, LocalActionManagerInterface $local_action_manager) {
$this->blockManager = $block_manager;
$this->routeMatch = $route_match;
$this->localActionManager = $local_action_manager;
$this->contextRepository = $context_repository;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('plugin.manager.block'), $container
->get('context.repository'), $container
->get('current_route_match'), $container
->get('plugin.manager.menu.local_action'));
}
public function listBlocks(Request $request, $theme) {
if ($request->query
->get(MainContentViewSubscriber::WRAPPER_FORMAT) === 'drupal_modal') {
$build['local_actions'] = $this
->buildLocalActions();
}
$headers = [
[
'data' => $this
->t('Block'),
],
[
'data' => $this
->t('Category'),
],
[
'data' => $this
->t('Operations'),
],
];
$definitions = $this->blockManager
->getDefinitionsForContexts($this->contextRepository
->getAvailableContexts());
$definitions = $this->blockManager
->getSortedDefinitions($definitions);
$region = $request->query
->get('region');
$rows = [];
foreach ($definitions as $plugin_id => $plugin_definition) {
$row = [];
$row['title']['data'] = [
'#type' => 'inline_template',
'#template' => '<div class="block-filter-text-source">{{ label }}</div>',
'#context' => [
'label' => $plugin_definition['admin_label'],
],
];
$row['category']['data'] = $plugin_definition['category'];
$links['add'] = [
'title' => $this
->t('Place block'),
'url' => Url::fromRoute('block.admin_add', [
'plugin_id' => $plugin_id,
'theme' => $theme,
]),
'attributes' => [
'class' => [
'use-ajax',
],
'data-dialog-type' => 'modal',
'data-dialog-options' => Json::encode([
'width' => 700,
]),
],
];
if ($region) {
$links['add']['query']['region'] = $region;
}
$row['operations']['data'] = [
'#type' => 'operations',
'#links' => $links,
];
$rows[] = $row;
}
$build['#attached']['library'][] = 'block/drupal.block.admin';
$build['filter'] = [
'#type' => 'search',
'#title' => $this
->t('Filter'),
'#title_display' => 'invisible',
'#size' => 30,
'#placeholder' => $this
->t('Filter by block name'),
'#attributes' => [
'class' => [
'block-filter-text',
],
'data-element' => '.block-add-table',
'title' => $this
->t('Enter a part of the block name to filter by.'),
],
];
$build['blocks'] = [
'#type' => 'table',
'#header' => $headers,
'#rows' => $rows,
'#empty' => $this
->t('No blocks available.'),
'#attributes' => [
'class' => [
'block-add-table',
],
],
];
return $build;
}
protected function buildLocalActions() {
$build = $this->localActionManager
->getActionsForRoute($this->routeMatch
->getRouteName());
if (!empty($build)) {
$build['#prefix'] = '<ul class="action-links">';
$build['#suffix'] = '</ul>';
}
return $build;
}
}