Commit cf4f0039 authored by catch's avatar catch

Issue #2497259 by catch, tim.plunkett, alexpott: system_region_list()...

Issue #2497259 by catch, tim.plunkett, alexpott: system_region_list() unnecessarily translates region names
parent 4ba73b4b
...@@ -1362,9 +1362,9 @@ function template_preprocess_page(&$variables) { ...@@ -1362,9 +1362,9 @@ function template_preprocess_page(&$variables) {
// Move some variables to the top level for themer convenience and template cleanliness. // Move some variables to the top level for themer convenience and template cleanliness.
$variables['title'] = $variables['page']['#title']; $variables['title'] = $variables['page']['#title'];
foreach (system_region_list(\Drupal::theme()->getActiveTheme()->getName()) as $region_key => $region_name) { foreach (\Drupal::theme()->getActiveTheme()->getRegions() as $region) {
if (!isset($variables['page'][$region_key])) { if (!isset($variables['page'][$region])) {
$variables['page'][$region_key] = array(); $variables['page'][$region] = array();
} }
} }
......
...@@ -220,8 +220,8 @@ protected function prepare(array $main_content, Request $request, RouteMatchInte ...@@ -220,8 +220,8 @@ protected function prepare(array $main_content, Request $request, RouteMatchInte
// $page is now fully built. Find all non-empty page regions, and add a // $page is now fully built. Find all non-empty page regions, and add a
// theme wrapper function that allows them to be consistently themed. // theme wrapper function that allows them to be consistently themed.
$regions = system_region_list(\Drupal::theme()->getActiveTheme()->getName()); $regions = \Drupal::theme()->getActiveTheme()->getRegions();
foreach (array_keys($regions) as $region) { foreach ($regions as $region) {
if (!empty($page[$region])) { if (!empty($page[$region])) {
$page[$region]['#theme_wrappers'][] = 'region'; $page[$region]['#theme_wrappers'][] = 'region';
$page[$region]['#region'] = $region; $page[$region]['#region'] = $region;
......
...@@ -73,6 +73,13 @@ class ActiveTheme { ...@@ -73,6 +73,13 @@ class ActiveTheme {
*/ */
protected $libraries; protected $libraries;
/**
* The regions provided by the theme.
*
* @var array
*/
protected $regions;
/** /**
* Constructs an ActiveTheme object. * Constructs an ActiveTheme object.
* *
...@@ -88,6 +95,7 @@ public function __construct(array $values) { ...@@ -88,6 +95,7 @@ public function __construct(array $values) {
'libraries' => [], 'libraries' => [],
'extension' => 'html.twig', 'extension' => 'html.twig',
'base_themes' => [], 'base_themes' => [],
'regions' => [],
]; ];
$this->name = $values['name']; $this->name = $values['name'];
...@@ -98,6 +106,7 @@ public function __construct(array $values) { ...@@ -98,6 +106,7 @@ public function __construct(array $values) {
$this->libraries = $values['libraries']; $this->libraries = $values['libraries'];
$this->extension = $values['extension']; $this->extension = $values['extension'];
$this->baseThemes = $values['base_themes']; $this->baseThemes = $values['base_themes'];
$this->regions = $values['regions'];
} }
/** /**
...@@ -177,4 +186,16 @@ public function getBaseThemes() { ...@@ -177,4 +186,16 @@ public function getBaseThemes() {
return $this->baseThemes; return $this->baseThemes;
} }
/**
* The regions used by the theme.
*
* @return string[]
* The list of region machine names supported by the theme.
*
* @see system_region_list()
*/
public function getRegions() {
return array_keys($this->regions);
}
} }
...@@ -214,6 +214,9 @@ public function getActiveTheme(Extension $theme, array $base_themes = []) { ...@@ -214,6 +214,9 @@ public function getActiveTheme(Extension $theme, array $base_themes = []) {
} }
$values['base_themes'] = $base_active_themes; $values['base_themes'] = $base_active_themes;
if (!empty($theme->info['regions'])) {
$values['regions'] = $theme->info['regions'];
}
return new ActiveTheme($values); return new ActiveTheme($values);
} }
......
...@@ -46,35 +46,16 @@ public function __construct(EntityManagerInterface $entity_manager, ThemeManager ...@@ -46,35 +46,16 @@ public function __construct(EntityManagerInterface $entity_manager, ThemeManager
$this->contextHandler = $context_handler; $this->contextHandler = $context_handler;
} }
/**
* Returns the human-readable list of regions keyed by machine name.
*
* @return array
* An array of human-readable region names keyed by machine name.
*/
protected function getRegionNames() {
return system_region_list($this->getTheme());
}
/**
* Gets the current theme for this page.
*
* @return string
* The current theme.
*/
protected function getTheme() {
return $this->themeManager->getActiveTheme()->getName();
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getVisibleBlocksPerRegion(array $contexts) { public function getVisibleBlocksPerRegion(array $contexts) {
$active_theme = $this->themeManager->getActiveTheme();
// Build an array of the region names in the right order. // Build an array of the region names in the right order.
$empty = array_fill_keys(array_keys($this->getRegionNames()), array()); $empty = array_fill_keys($active_theme->getRegions(), array());
$full = array(); $full = array();
foreach ($this->blockStorage->loadByProperties(array('theme' => $this->getTheme())) as $block_id => $block) { foreach ($this->blockStorage->loadByProperties(array('theme' => $active_theme->getName())) as $block_id => $block) {
/** @var \Drupal\block\BlockInterface $block */ /** @var \Drupal\block\BlockInterface $block */
// Set the contexts on the block before checking access. // Set the contexts on the block before checking access.
if ($block->setContexts($contexts)->access('view')) { if ($block->setContexts($contexts)->access('view')) {
......
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
namespace Drupal\Tests\block\Unit; namespace Drupal\Tests\block\Unit;
use Drupal\block\BlockRepository;
use Drupal\Core\Block\BlockPluginInterface; use Drupal\Core\Block\BlockPluginInterface;
use Drupal\Core\Plugin\ContextAwarePluginInterface; use Drupal\Core\Plugin\ContextAwarePluginInterface;
use Drupal\Tests\UnitTestCase; use Drupal\Tests\UnitTestCase;
...@@ -49,6 +50,13 @@ protected function setUp() { ...@@ -49,6 +50,13 @@ protected function setUp() {
$active_theme->expects($this->atLeastOnce()) $active_theme->expects($this->atLeastOnce())
->method('getName') ->method('getName')
->willReturn($this->theme); ->willReturn($this->theme);
$active_theme->expects($this->atLeastOnce())
->method('getRegions')
->willReturn([
'top',
'center',
'bottom',
]);
$theme_manager = $this->getMock('Drupal\Core\Theme\ThemeManagerInterface'); $theme_manager = $this->getMock('Drupal\Core\Theme\ThemeManagerInterface');
$theme_manager->expects($this->once()) $theme_manager->expects($this->once())
...@@ -62,17 +70,7 @@ protected function setUp() { ...@@ -62,17 +70,7 @@ protected function setUp() {
->method('getStorage') ->method('getStorage')
->willReturn($this->blockStorage); ->willReturn($this->blockStorage);
$this->blockRepository = $this->getMockBuilder('Drupal\block\BlockRepository') $this->blockRepository = new BlockRepository($entity_manager, $theme_manager, $this->contextHandler);
->setConstructorArgs([$entity_manager, $theme_manager, $this->contextHandler])
->setMethods(['getRegionNames'])
->getMock();
$this->blockRepository->expects($this->once())
->method('getRegionNames')
->willReturn([
'top' => 'Top',
'center' => 'Center',
'bottom' => 'Bottom',
]);
} }
/** /**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment