How to display the user's preferred language in a User profile page
Question
In a Drupal 8/9 site, I can select the User's preferred language.
In the "Manage form display" page (for user's fields), there are two rows which are about language:
Language settings
(settings = --no settings available--)Language code
(settings = "language select" as the only option)
Question 1 : What do these two options produce?
In the "Manage display" page, I can display only one item about the language : Language code
As a result, if I visit my user profile page, I can see:
Language code: French
But if I change my preferred language in my user edit profile page into Dutch, there is no effect in my "User view" page: it is always "French"
ok, I can preprocess something and adapt the twig template to display my preferred language but this is not the answer I would like to have.
Question 2: I would like to understand this behavior because, as always in Drupal, I am sure there is a good reason for that.
Solution
I think I could answer myself to most of my questions:
1-The Language code
field store the initial language of a user... it looks like it can't be changed after all. For my use case, it is an useless field.
2-The Language settings
options display the Preferred language widget and the Administration pages language widget. I think that this last widget is there only if you check Account administration pages language setting. in admin/config/regional/language/detection
3-The most important part: How to display the preferred language of a user in the the display user page (/user/xxx
)
You can do it with this hook:
function mymodule_entity_base_field_info_alter(&$fields, EntityTypeInterface $entity_type) {
if ($entity_type->id() == "user") {
$fields['preferred_langcode']->setDisplayConfigurable('view', TRUE);
}
}
I really don't know why it is not a default... (don't hesitate to add any information on this point)