annotate core/modules/block/src/Controller/BlockController.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 129ea1e6d783
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\block\Controller;
Chris@0 4
Chris@0 5 use Drupal\Component\Utility\Html;
Chris@0 6 use Drupal\block\BlockInterface;
Chris@0 7 use Drupal\Core\Controller\ControllerBase;
Chris@0 8 use Drupal\Core\Extension\ThemeHandlerInterface;
Chris@0 9 use Symfony\Component\DependencyInjection\ContainerInterface;
Chris@0 10 use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
Chris@0 11
Chris@0 12 /**
Chris@0 13 * Controller routines for admin block routes.
Chris@0 14 */
Chris@0 15 class BlockController extends ControllerBase {
Chris@0 16
Chris@0 17 /**
Chris@0 18 * The theme handler.
Chris@0 19 *
Chris@0 20 * @var \Drupal\Core\Extension\ThemeHandlerInterface
Chris@0 21 */
Chris@0 22 protected $themeHandler;
Chris@0 23
Chris@0 24 /**
Chris@0 25 * Constructs a new BlockController instance.
Chris@0 26 *
Chris@0 27 * @param \Drupal\Core\Extension\ThemeHandlerInterface $theme_handler
Chris@0 28 * The theme handler.
Chris@0 29 */
Chris@0 30 public function __construct(ThemeHandlerInterface $theme_handler) {
Chris@0 31 $this->themeHandler = $theme_handler;
Chris@0 32 }
Chris@0 33
Chris@0 34 /**
Chris@0 35 * {@inheritdoc}
Chris@0 36 */
Chris@0 37 public static function create(ContainerInterface $container) {
Chris@0 38 return new static(
Chris@0 39 $container->get('theme_handler')
Chris@0 40 );
Chris@0 41 }
Chris@0 42
Chris@0 43 /**
Chris@0 44 * Calls a method on a block and reloads the listing page.
Chris@0 45 *
Chris@0 46 * @param \Drupal\block\BlockInterface $block
Chris@0 47 * The block being acted upon.
Chris@0 48 * @param string $op
Chris@0 49 * The operation to perform, e.g., 'enable' or 'disable'.
Chris@0 50 *
Chris@0 51 * @return \Symfony\Component\HttpFoundation\RedirectResponse
Chris@0 52 * A redirect back to the listing page.
Chris@0 53 */
Chris@0 54 public function performOperation(BlockInterface $block, $op) {
Chris@0 55 $block->$op()->save();
Chris@17 56 $this->messenger()->addStatus($this->t('The block settings have been updated.'));
Chris@0 57 return $this->redirect('block.admin_display');
Chris@0 58 }
Chris@0 59
Chris@0 60 /**
Chris@0 61 * Returns a block theme demo page.
Chris@0 62 *
Chris@0 63 * @param string $theme
Chris@0 64 * The name of the theme.
Chris@0 65 *
Chris@0 66 * @return array
Chris@0 67 * A #type 'page' render array containing the block region demo.
Chris@0 68 */
Chris@0 69 public function demo($theme) {
Chris@0 70 if (!$this->themeHandler->hasUi($theme)) {
Chris@0 71 throw new NotFoundHttpException();
Chris@0 72 }
Chris@0 73
Chris@0 74 $page = [
Chris@0 75 '#title' => Html::escape($this->themeHandler->getName($theme)),
Chris@0 76 '#type' => 'page',
Chris@0 77 '#attached' => [
Chris@0 78 'drupalSettings' => [
Chris@0 79 // The block demonstration page is not marked as an administrative
Chris@0 80 // page by \Drupal::service('router.admin_context')->isAdminRoute()
Chris@0 81 // function in order to use the frontend theme. Since JavaScript
Chris@0 82 // relies on a proper separation of admin pages, it needs to know this
Chris@0 83 // is an actual administrative page.
Chris@0 84 'path' => ['currentPathIsAdmin' => TRUE],
Chris@0 85 ],
Chris@0 86 'library' => [
Chris@0 87 'block/drupal.block.admin',
Chris@0 88 ],
Chris@0 89 ],
Chris@0 90 ];
Chris@0 91
Chris@0 92 // Show descriptions in each visible page region, nothing else.
Chris@0 93 $visible_regions = $this->getVisibleRegionNames($theme);
Chris@0 94 foreach (array_keys($visible_regions) as $region) {
Chris@0 95 $page[$region]['block_description'] = [
Chris@0 96 '#type' => 'inline_template',
Chris@0 97 '#template' => '<div class="block-region demo-block">{{ region_name }}</div>',
Chris@0 98 '#context' => ['region_name' => $visible_regions[$region]],
Chris@0 99 ];
Chris@0 100 }
Chris@0 101
Chris@0 102 return $page;
Chris@0 103 }
Chris@0 104
Chris@0 105 /**
Chris@0 106 * Returns the human-readable list of regions keyed by machine name.
Chris@0 107 *
Chris@0 108 * @param string $theme
Chris@0 109 * The name of the theme.
Chris@0 110 *
Chris@0 111 * @return array
Chris@0 112 * An array of human-readable region names keyed by machine name.
Chris@0 113 */
Chris@0 114 protected function getVisibleRegionNames($theme) {
Chris@0 115 return system_region_list($theme, REGIONS_VISIBLE);
Chris@0 116 }
Chris@0 117
Chris@0 118 }