From cf4f00393f000ec5f14a2165404d9d68f3be53ef Mon Sep 17 00:00:00 2001
From: Nathaniel Catchpole <catch@35733.no-reply.drupal.org>
Date: Tue, 9 Jun 2015 15:24:55 +0100
Subject: [PATCH] Issue #2497259 by catch, tim.plunkett, alexpott:
 system_region_list() unnecessarily translates region names

---
 core/includes/theme.inc                       |  6 ++---
 .../Core/Render/MainContent/HtmlRenderer.php  |  4 +--
 core/lib/Drupal/Core/Theme/ActiveTheme.php    | 21 ++++++++++++++++
 .../Drupal/Core/Theme/ThemeInitialization.php |  3 +++
 core/modules/block/src/BlockRepository.php    | 25 +++----------------
 .../tests/src/Unit/BlockRepositoryTest.php    | 20 +++++++--------
 6 files changed, 41 insertions(+), 38 deletions(-)

diff --git a/core/includes/theme.inc b/core/includes/theme.inc
index fa83a1b634b8..121da17467d7 100644
--- a/core/includes/theme.inc
+++ b/core/includes/theme.inc
@@ -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();
     }
   }
 
diff --git a/core/lib/Drupal/Core/Render/MainContent/HtmlRenderer.php b/core/lib/Drupal/Core/Render/MainContent/HtmlRenderer.php
index 5f848573337f..d61bb24d811b 100644
--- a/core/lib/Drupal/Core/Render/MainContent/HtmlRenderer.php
+++ b/core/lib/Drupal/Core/Render/MainContent/HtmlRenderer.php
@@ -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;
diff --git a/core/lib/Drupal/Core/Theme/ActiveTheme.php b/core/lib/Drupal/Core/Theme/ActiveTheme.php
index aace6f9c1c6b..c35afad78d9d 100644
--- a/core/lib/Drupal/Core/Theme/ActiveTheme.php
+++ b/core/lib/Drupal/Core/Theme/ActiveTheme.php
@@ -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);
+  }
+
 }
diff --git a/core/lib/Drupal/Core/Theme/ThemeInitialization.php b/core/lib/Drupal/Core/Theme/ThemeInitialization.php
index a77271eee8fd..3f055c14c1ac 100644
--- a/core/lib/Drupal/Core/Theme/ThemeInitialization.php
+++ b/core/lib/Drupal/Core/Theme/ThemeInitialization.php
@@ -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);
   }
diff --git a/core/modules/block/src/BlockRepository.php b/core/modules/block/src/BlockRepository.php
index 485a66fedd0b..980e0adb8795 100644
--- a/core/modules/block/src/BlockRepository.php
+++ b/core/modules/block/src/BlockRepository.php
@@ -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')) {
diff --git a/core/modules/block/tests/src/Unit/BlockRepositoryTest.php b/core/modules/block/tests/src/Unit/BlockRepositoryTest.php
index f589e82bad0c..56b90caae261 100644
--- a/core/modules/block/tests/src/Unit/BlockRepositoryTest.php
+++ b/core/modules/block/tests/src/Unit/BlockRepositoryTest.php
@@ -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);
   }
 
   /**
-- 
GitLab