3 namespace Drupal\block\Controller;
5 use Drupal\Component\Utility\Html;
6 use Drupal\block\BlockInterface;
7 use Drupal\Core\Controller\ControllerBase;
8 use Drupal\Core\Extension\ThemeHandlerInterface;
9 use Symfony\Component\DependencyInjection\ContainerInterface;
10 use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
13 * Controller routines for admin block routes.
15 class BlockController extends ControllerBase {
20 * @var \Drupal\Core\Extension\ThemeHandlerInterface
22 protected $themeHandler;
25 * Constructs a new BlockController instance.
27 * @param \Drupal\Core\Extension\ThemeHandlerInterface $theme_handler
30 public function __construct(ThemeHandlerInterface $theme_handler) {
31 $this->themeHandler = $theme_handler;
37 public static function create(ContainerInterface $container) {
39 $container->get('theme_handler')
44 * Calls a method on a block and reloads the listing page.
46 * @param \Drupal\block\BlockInterface $block
47 * The block being acted upon.
49 * The operation to perform, e.g., 'enable' or 'disable'.
51 * @return \Symfony\Component\HttpFoundation\RedirectResponse
52 * A redirect back to the listing page.
54 public function performOperation(BlockInterface $block, $op) {
55 $block->$op()->save();
56 drupal_set_message($this->t('The block settings have been updated.'));
57 return $this->redirect('block.admin_display');
61 * Returns a block theme demo page.
63 * @param string $theme
64 * The name of the theme.
67 * A #type 'page' render array containing the block region demo.
69 public function demo($theme) {
70 if (!$this->themeHandler->hasUi($theme)) {
71 throw new NotFoundHttpException();
75 '#title' => Html::escape($this->themeHandler->getName($theme)),
79 // The block demonstration page is not marked as an administrative
80 // page by \Drupal::service('router.admin_context')->isAdminRoute()
81 // function in order to use the frontend theme. Since JavaScript
82 // relies on a proper separation of admin pages, it needs to know this
83 // is an actual administrative page.
84 'path' => ['currentPathIsAdmin' => TRUE],
87 'block/drupal.block.admin',
92 // Show descriptions in each visible page region, nothing else.
93 $visible_regions = $this->getVisibleRegionNames($theme);
94 foreach (array_keys($visible_regions) as $region) {
95 $page[$region]['block_description'] = [
96 '#type' => 'inline_template',
97 '#template' => '<div class="block-region demo-block">{{ region_name }}</div>',
98 '#context' => ['region_name' => $visible_regions[$region]],
106 * Returns the human-readable list of regions keyed by machine name.
108 * @param string $theme
109 * The name of the theme.
112 * An array of human-readable region names keyed by machine name.
114 protected function getVisibleRegionNames($theme) {
115 return system_region_list($theme, REGIONS_VISIBLE);