Skip to content
Snippets Groups Projects
Unverified Commit 9940ccf0 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3048196 by tetranz, Tom Konda, Berdir: A field title isn't translated...

Issue #3048196 by tetranz, Tom Konda, Berdir: A field title isn't translated if "Account administration pages" plugin is enabled

(cherry picked from commit d9519bae)
parent db53fd77
No related branches found
No related tags found
No related merge requests found
......@@ -325,10 +325,11 @@ protected function buildBaseFieldDefinitions($entity_type_id) {
* {@inheritdoc}
*/
public function getFieldDefinitions($entity_type_id, $bundle) {
if (!isset($this->fieldDefinitions[$entity_type_id][$bundle])) {
$langcode = $this->languageManager->getCurrentLanguage()->getId();
if (!isset($this->fieldDefinitions[$entity_type_id][$bundle][$langcode])) {
$base_field_definitions = $this->getBaseFieldDefinitions($entity_type_id);
// Not prepared, try to load from cache.
$cid = 'entity_bundle_field_definitions:' . $entity_type_id . ':' . $bundle . ':' . $this->languageManager->getCurrentLanguage()->getId();
$cid = 'entity_bundle_field_definitions:' . $entity_type_id . ':' . $bundle . ':' . $langcode;
if ($cache = $this->cacheGet($cid)) {
$bundle_field_definitions = $cache->data;
}
......@@ -341,9 +342,9 @@ public function getFieldDefinitions($entity_type_id, $bundle) {
// base fields, merge them together. Use array_replace() to replace base
// fields with by bundle overrides and keep them in order, append
// additional by bundle fields.
$this->fieldDefinitions[$entity_type_id][$bundle] = array_replace($base_field_definitions, $bundle_field_definitions);
$this->fieldDefinitions[$entity_type_id][$bundle][$langcode] = array_replace($base_field_definitions, $bundle_field_definitions);
}
return $this->fieldDefinitions[$entity_type_id][$bundle];
return $this->fieldDefinitions[$entity_type_id][$bundle][$langcode];
}
/**
......
......@@ -3,6 +3,9 @@
namespace Drupal\Tests\language\Functional;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\language\Entity\ContentLanguageSettings;
use Drupal\node\Entity\Node;
......@@ -186,4 +189,76 @@ public function testUrlContentTranslationWithSessionLanguage() {
$assert_session->pageTextContains('Powered by');
}
/**
* Tests translation of the user profile edit form.
*
* The user profile edit form is a special case when used with the preferred
* admin language negotiator because of the recursive way that the negotiator
* is called.
*/
public function testUserProfileTranslationWithPreferredAdminLanguage() {
$assert_session = $this->assertSession();
// Set the interface language to use the preferred administration language.
/** @var \Drupal\language\LanguageNegotiatorInterface $language_negotiator */
$language_negotiator = \Drupal::getContainer()->get('language_negotiator');
$language_negotiator->saveConfiguration('language_interface', [
'language-user-admin' => 1,
'language-selected' => 2,
]);
// Create a field on the user entity.
$field_name = mb_strtolower($this->randomMachineName());
$label = mb_strtolower($this->randomMachineName());
$field_label_en = "English $label";
$field_label_es = "Español $label";
$field_storage = FieldStorageConfig::create([
'field_name' => $field_name,
'entity_type' => 'user',
'type' => 'string',
]);
$field_storage->save();
$instance = FieldConfig::create([
'field_storage' => $field_storage,
'bundle' => 'user',
'label' => $field_label_en,
]);
$instance->save();
// Add a Spanish translation.
\Drupal::languageManager()
->getLanguageConfigOverride('es', "field.field.user.user.$field_name")
->set('label', $field_label_es)
->save();
// Add the new field to the edit form.
EntityFormDisplay::create([
'targetEntityType' => 'user',
'bundle' => 'user',
'mode' => 'default',
'status' => TRUE,
])
->setComponent($field_name, [
'type' => 'string_textfield',
])
->save();
$user_id = \Drupal::currentUser()->id();
$this->drupalGet("/user/$user_id/edit");
// Admin language choice is "No preference" so we should get the default.
$assert_session->pageTextContains($field_label_en);
$assert_session->pageTextNotContains($field_label_es);
// Set admin language to Spanish.
$this->drupalPostForm(NULL, ['edit-preferred-admin-langcode' => 'es'], 'edit-submit');
$assert_session->pageTextContains($field_label_es);
$assert_session->pageTextNotContains($field_label_en);
// Set admin language to English.
$this->drupalPostForm(NULL, ['edit-preferred-admin-langcode' => 'en'], 'edit-submit');
$assert_session->pageTextContains($field_label_en);
$assert_session->pageTextNotContains($field_label_es);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment