View source
<?php
namespace Drupal\draggable_dashboard\Controller;
use Drupal\Component\Serialization\Json;
use Drupal\Core\Block\BlockManagerInterface;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Plugin\Context\LazyContextRepository;
use Drupal\Core\Plugin\PluginFormFactoryInterface;
use Drupal\Core\Url;
use Drupal\draggable_dashboard\Entity\DashboardEntityInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class DraggableDashboardController extends ControllerBase {
protected $blockManager;
protected $contextRepository;
protected $pluginFormFactory;
public function __construct(BlockManagerInterface $block_manager, LazyContextRepository $context_repository, PluginFormFactoryInterface $plugin_form_manager) {
$this->blockManager = $block_manager;
$this->contextRepository = $context_repository;
$this->pluginFormFactory = $plugin_form_manager;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('plugin.manager.block'), $container
->get('context.repository'), $container
->get('plugin_form.factory'));
}
public function listBlocks(Request $request, DashboardEntityInterface $dashboard_entity, $region) {
$headers = [
[
'data' => $this
->t('Block'),
],
[
'data' => $this
->t('Category'),
],
[
'data' => $this
->t('Operations'),
],
];
$definitions = $this->blockManager
->getFilteredDefinitions('block_ui', $this->contextRepository
->getAvailableContexts(), [
'region' => $region,
]);
$definitions = $this->blockManager
->getSortedDefinitions($definitions);
$definitions = array_filter($definitions, function (array $definition) {
return empty($definition['_block_ui_hidden']);
});
$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('draggable_dashboard.block_add', [
'dashboard_entity' => $dashboard_entity
->id(),
'plugin_id' => $plugin_id,
'region' => $region,
]),
'attributes' => [
'class' => [
'use-ajax',
],
'data-dialog-type' => 'modal',
'data-dialog-options' => Json::encode([
'width' => 700,
]),
],
];
$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;
}
}