diff --git a/core/modules/field/field.services.yml b/core/modules/field/field.services.yml index 8615517..43d3d1b 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 f68ceed..72d4af5 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. @@ -61,6 +62,13 @@ class FieldInfo { protected $config; /** + * The language manager. + * + * @var \Drupal\Core\Language\LanguageManager + */ + protected $languageManager; + + /** * Lightweight map of fields across entity types and bundles. * * @var array @@ -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 32ffe10..fc782a6 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 70017fd..c9c30eb 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',