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) {
// Move some variables to the top level for themer convenience and template cleanliness.
$variables['title'] = $variables['page']['#title'];
foreach (system_region_list(\Drupal::theme()->getActiveTheme()->getName()) as $region_key => $region_name) {
if (!isset($variables['page'][$region_key])) {
$variables['page'][$region_key] = array();
foreach (\Drupal::theme()->getActiveTheme()->getRegions() as $region) {
if (!isset($variables['page'][$region])) {
$variables['page'][$region] = array();
}
}
......
......@@ -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
// theme wrapper function that allows them to be consistently themed.
$regions = system_region_list(\Drupal::theme()->getActiveTheme()->getName());
foreach (array_keys($regions) as $region) {
$regions = \Drupal::theme()->getActiveTheme()->getRegions();
foreach ($regions as $region) {
if (!empty($page[$region])) {
$page[$region]['#theme_wrappers'][] = 'region';
$page[$region]['#region'] = $region;
......
......@@ -73,6 +73,13 @@ class ActiveTheme {
*/
protected $libraries;
/**
* The regions provided by the theme.
*
* @var array
*/
protected $regions;
/**
* Constructs an ActiveTheme object.
*
......@@ -88,6 +95,7 @@ public function __construct(array $values) {
'libraries' => [],
'extension' => 'html.twig',
'base_themes' => [],
'regions' => [],
];
$this->name = $values['name'];
......@@ -98,6 +106,7 @@ public function __construct(array $values) {
$this->libraries = $values['libraries'];
$this->extension = $values['extension'];
$this->baseThemes = $values['base_themes'];
$this->regions = $values['regions'];
}
/**
......@@ -177,4 +186,16 @@ public function getBaseThemes() {
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 = []) {
}
$values['base_themes'] = $base_active_themes;
if (!empty($theme->info['regions'])) {
$values['regions'] = $theme->info['regions'];
}
return new ActiveTheme($values);
}
......
......@@ -46,35 +46,16 @@ public function __construct(EntityManagerInterface $entity_manager, ThemeManager
$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}
*/
public function getVisibleBlocksPerRegion(array $contexts) {
$active_theme = $this->themeManager->getActiveTheme();
// 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();
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 */
// Set the contexts on the block before checking access.
if ($block->setContexts($contexts)->access('view')) {
......
......@@ -7,6 +7,7 @@
namespace Drupal\Tests\block\Unit;
use Drupal\block\BlockRepository;
use Drupal\Core\Block\BlockPluginInterface;
use Drupal\Core\Plugin\ContextAwarePluginInterface;
use Drupal\Tests\UnitTestCase;
......@@ -49,6 +50,13 @@ protected function setUp() {
$active_theme->expects($this->atLeastOnce())
->method('getName')
->willReturn($this->theme);
$active_theme->expects($this->atLeastOnce())
->method('getRegions')
->willReturn([
'top',
'center',
'bottom',
]);
$theme_manager = $this->getMock('Drupal\Core\Theme\ThemeManagerInterface');
$theme_manager->expects($this->once())
......@@ -62,17 +70,7 @@ protected function setUp() {
->method('getStorage')
->willReturn($this->blockStorage);
$this->blockRepository = $this->getMockBuilder('Drupal\block\BlockRepository')
->setConstructorArgs([$entity_manager, $theme_manager, $this->contextHandler])
->setMethods(['getRegionNames'])
->getMock();
$this->blockRepository->expects($this->once())
->method('getRegionNames')
->willReturn([
'top' => 'Top',
'center' => 'Center',
'bottom' => 'Bottom',
]);
$this->blockRepository = new BlockRepository($entity_manager, $theme_manager, $this->contextHandler);
}
/**
......
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