From 91542ece95d99eda6f336f50d414855a0b74b1a9 Mon Sep 17 00:00:00 2001
From: Nathaniel Catchpole <catch@35733.no-reply.drupal.org>
Date: Tue, 25 Feb 2014 13:24:07 +0000
Subject: [PATCH] =?UTF-8?q?Issue=20#1810178=20by=20sidharthap,=20swentel,?=
 =?UTF-8?q?=20Xano,=20Rajesh=20Ashok,=20David=20Hern=C3=A1ndez,=20durifal,?=
 =?UTF-8?q?=20yched,=20guy=5Fschneerson:=20=5Ffield=5Finfo=5Fcollate=5Ffie?=
 =?UTF-8?q?lds()=20is=20not=20language-aware,=20may=20return=20wrong=20val?=
 =?UTF-8?q?ues.?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 core/modules/field/field.services.yml         |  2 +-
 .../field/lib/Drupal/field/FieldInfo.php      | 22 +++++++++---
 .../lib/Drupal/field/Tests/FieldInfoTest.php  | 36 +++++++++++++++++++
 .../field_ui/Tests/ManageFieldsTest.php       |  1 +
 4 files changed, 56 insertions(+), 5 deletions(-)

diff --git a/core/modules/field/field.services.yml b/core/modules/field/field.services.yml
index 86155170c594..43d3d1bb9652 100644
--- a/core/modules/field/field.services.yml
+++ b/core/modules/field/field.services.yml
@@ -1,5 +1,5 @@
 services:
   field.info:
     class: Drupal\field\FieldInfo
-    arguments: ['@cache.field', '@config.factory', '@module_handler', '@plugin.manager.field.field_type']
+    arguments: ['@cache.field', '@config.factory', '@module_handler', '@plugin.manager.field.field_type', '@language_manager']
 
diff --git a/core/modules/field/lib/Drupal/field/FieldInfo.php b/core/modules/field/lib/Drupal/field/FieldInfo.php
index f68ceedb8d95..72d4af5c07ba 100644
--- a/core/modules/field/lib/Drupal/field/FieldInfo.php
+++ b/core/modules/field/lib/Drupal/field/FieldInfo.php
@@ -12,6 +12,7 @@
 use Drupal\Core\Config\ConfigFactoryInterface;
 use Drupal\Core\Field\FieldTypePluginManagerInterface;
 use Drupal\Core\Extension\ModuleHandlerInterface;
+use Drupal\Core\Language\LanguageManagerInterface;
 
 /**
  * Provides field and instance definitions for the current runtime environment.
@@ -60,6 +61,13 @@ class FieldInfo {
    */
   protected $config;
 
+  /**
+   * The language manager.
+   *
+   * @var \Drupal\Core\Language\LanguageManager
+   */
+  protected $languageManager;
+
   /**
    * Lightweight map of fields across entity types and bundles.
    *
@@ -134,12 +142,15 @@ class FieldInfo {
    *   The module handler class to use for invoking hooks.
    * @param \Drupal\Core\Field\FieldTypePluginManagerInterface $field_type_manager
    *   The 'field type' plugin manager.
+   * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
+   *   The language manager to use.
    */
-  public function __construct(CacheBackendInterface $cache_backend, ConfigFactoryInterface $config, ModuleHandlerInterface $module_handler, FieldTypePluginManagerInterface $field_type_manager) {
+  public function __construct(CacheBackendInterface $cache_backend, ConfigFactoryInterface $config, ModuleHandlerInterface $module_handler, FieldTypePluginManagerInterface $field_type_manager, LanguageManagerInterface $language_manager) {
     $this->cacheBackend = $cache_backend;
     $this->moduleHandler = $module_handler;
     $this->config = $config;
     $this->fieldTypeManager = $field_type_manager;
+    $this->languageManager = $language_manager;
   }
 
   /**
@@ -527,8 +538,11 @@ public function getBundleExtraFields($entity_type, $bundle) {
       return $this->bundleExtraFields[$entity_type][$bundle];
     }
 
-    // Read from the persistent cache.
-    if ($cached = $this->cacheBackend->get("field_info:bundle_extra:$entity_type:$bundle")) {
+    // Read from the persistent cache. Since hook_field_extra_fields() and
+    // hook_field_extra_fields_alter() might contain t() calls, we cache per
+    // language.
+    $langcode = $this->languageManager->getCurrentLanguage()->id;
+    if ($cached = $this->cacheBackend->get("field_info:bundle_extra:$langcode:$entity_type:$bundle")) {
       $this->bundleExtraFields[$entity_type][$bundle] = $cached->data;
       return $this->bundleExtraFields[$entity_type][$bundle];
     }
@@ -543,7 +557,7 @@ public function getBundleExtraFields($entity_type, $bundle) {
 
     // Store in the 'static' and persistent caches.
     $this->bundleExtraFields[$entity_type][$bundle] = $info;
-    $this->cacheBackend->set("field_info:bundle_extra:$entity_type:$bundle", $info, Cache::PERMANENT, array('field_info' => TRUE));
+    $this->cacheBackend->set("field_info:bundle_extra:$langcode:$entity_type:$bundle", $info, Cache::PERMANENT, array('field_info' => TRUE));
 
     return $this->bundleExtraFields[$entity_type][$bundle];
   }
diff --git a/core/modules/field/lib/Drupal/field/Tests/FieldInfoTest.php b/core/modules/field/lib/Drupal/field/Tests/FieldInfoTest.php
index 32ffe10adc4d..fc782a694f2f 100644
--- a/core/modules/field/lib/Drupal/field/Tests/FieldInfoTest.php
+++ b/core/modules/field/lib/Drupal/field/Tests/FieldInfoTest.php
@@ -7,6 +7,8 @@
 
 namespace Drupal\field\Tests;
 
+use Drupal\Core\Language\Language;
+
 class FieldInfoTest extends FieldUnitTestBase {
 
   public static function getInfo() {
@@ -377,5 +379,39 @@ protected function getExpectedFieldTypeDefinition() {
     );
   }
 
+  /**
+   * Tests that the extra fields can be translated.
+   */
+  function testFieldInfoExtraFieldsTranslation() {
+    $this->enableModules(array('language', 'locale'));
+    $this->installSchema('locale', array('locales_source', 'locales_target', 'locales_location'));
+    foreach (array('en', 'hu') as $id) {
+      $language = new Language(array(
+        'id' => $id,
+      ));
+      language_save($language);
+    }
+    $locale_storage = $this->container->get('locale.storage');
+
+    // Create test source string.
+    $en_string = $locale_storage->createString(array(
+      'source' => 'User name and password',
+      'context' => '',
+    ))->save();
+
+    // Create translation for new string and save it.
+    $translated_string = $this->randomString();
+    $locale_storage->createTranslation(array(
+      'lid' => $en_string->lid,
+      'language' => 'hu',
+      'translation' => $translated_string,
+    ))->save();
+
+    // Check that the label is translated.
+    \Drupal::translation()->setDefaultLangcode('hu');
+    $field_info = \Drupal::service('field.info');
+    $user_fields = $field_info->getBundleExtraFields('user', 'user');
+    $this->assertEqual($user_fields['form']['account']['label'], $translated_string);
+  }
 
 }
diff --git a/core/modules/field_ui/lib/Drupal/field_ui/Tests/ManageFieldsTest.php b/core/modules/field_ui/lib/Drupal/field_ui/Tests/ManageFieldsTest.php
index 70017fd34a7a..c9c30ebb988b 100644
--- a/core/modules/field_ui/lib/Drupal/field_ui/Tests/ManageFieldsTest.php
+++ b/core/modules/field_ui/lib/Drupal/field_ui/Tests/ManageFieldsTest.php
@@ -15,6 +15,7 @@
  * Tests the functionality of the 'Manage fields' screen.
  */
 class ManageFieldsTest extends FieldUiTestBase {
+
   public static function getInfo() {
     return array(
       'name' => 'Manage fields',
-- 
GitLab