Commit 91542ece authored by catch's avatar catch

Issue #1810178 by sidharthap, swentel, Xano, Rajesh Ashok, David Hernández,...

Issue #1810178 by sidharthap, swentel, Xano, Rajesh Ashok, David Hernández, durifal, yched, guy_schneerson: _field_info_collate_fields() is not language-aware, may return wrong values.
parent cb566fab
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']
......@@ -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];
}
......
......@@ -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);
}
}
......@@ -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',
......
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