7398ff98e125c4779086e8d63f87015b0c774b8d
[yaffs-website] / web / core / modules / block / src / Controller / BlockController.php
1 <?php
2
3 namespace Drupal\block\Controller;
4
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;
11
12 /**
13  * Controller routines for admin block routes.
14  */
15 class BlockController extends ControllerBase {
16
17   /**
18    * The theme handler.
19    *
20    * @var \Drupal\Core\Extension\ThemeHandlerInterface
21    */
22   protected $themeHandler;
23
24   /**
25    * Constructs a new BlockController instance.
26    *
27    * @param \Drupal\Core\Extension\ThemeHandlerInterface $theme_handler
28    *   The theme handler.
29    */
30   public function __construct(ThemeHandlerInterface $theme_handler) {
31     $this->themeHandler = $theme_handler;
32   }
33
34   /**
35    * {@inheritdoc}
36    */
37   public static function create(ContainerInterface $container) {
38     return new static(
39       $container->get('theme_handler')
40     );
41   }
42
43   /**
44    * Calls a method on a block and reloads the listing page.
45    *
46    * @param \Drupal\block\BlockInterface $block
47    *   The block being acted upon.
48    * @param string $op
49    *   The operation to perform, e.g., 'enable' or 'disable'.
50    *
51    * @return \Symfony\Component\HttpFoundation\RedirectResponse
52    *   A redirect back to the listing page.
53    */
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');
58   }
59
60   /**
61    * Returns a block theme demo page.
62    *
63    * @param string $theme
64    *   The name of the theme.
65    *
66    * @return array
67    *   A #type 'page' render array containing the block region demo.
68    */
69   public function demo($theme) {
70     if (!$this->themeHandler->hasUi($theme)) {
71       throw new NotFoundHttpException();
72     }
73
74     $page = [
75       '#title' => Html::escape($this->themeHandler->getName($theme)),
76       '#type' => 'page',
77       '#attached' => [
78         'drupalSettings' => [
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],
85         ],
86         'library' => [
87           'block/drupal.block.admin',
88         ],
89       ],
90     ];
91
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]],
99       ];
100     }
101
102     return $page;
103   }
104
105   /**
106    * Returns the human-readable list of regions keyed by machine name.
107    *
108    * @param string $theme
109    *   The name of the theme.
110    *
111    * @return array
112    *   An array of human-readable region names keyed by machine name.
113    */
114   protected function getVisibleRegionNames($theme) {
115     return system_region_list($theme, REGIONS_VISIBLE);
116   }
117
118 }