Problem/Motivation
We've come upon an issue with the syncing of langcode with preferred_langcode on the User entity.
When editing the preferred language of the User entity to an existing entity translation langcode, an exception is thrown:
InvalidArgumentException: A translation already exists for the specified language (pt-pt). in Drupal\Core\Entity\ContentEntityBase->onChange() (line 810 of drupal/core/lib/Drupal/Core/Entity/ContentEntityBase.php).
After investigating the cause for this issue, our findings point to the entity builder Account:syncUserLangcode(), which is syncing the langcode with the preferred_langcode. This results in a problem when changing the langcode if the new value points to an existing translation. If this operation is removed, the problem no longer occurs.
Reproduction steps
1. Enable content translation
2. Add new languages to the site (e.g. pt-pt).
3. Translate the user entity to pt-pt.
4. Edit the User entity in its default translation, through the UI, setting its preferred langcode to pt-pt.
5. Save
6. Observe the thrown exception
Proposed resolution
Remove Account:syncUserLangcode(), which is BC break. We need to find out why it was introduced in the first place.
See #2230637: Create a Language field widget and the related formatter where this has not been discussed in the comments.
Remaining tasks
User interface changes
API changes
Data model changes
Release notes snippet
| Comment | File | Size | Author |
|---|---|---|---|
| #5 | 3119224-2-8.9.x.patch | 5.3 KB | kishor_kolekar |
| #5 | 3119224-2-9.0.x.patch | 6.1 KB | kishor_kolekar |
| #5 | interdiff-1-2-9.0.x.txt | 6.03 KB | kishor_kolekar |
| #2 | 3119224-1-8.9.x.patch | 1.74 KB | gnunes |
| #2 | 3119224-1-9.0.x.patch | 3.45 KB | gnunes |
Comments
Comment #2
gnunes commentedI've removed the
Account::syncUserLandcode(), for both 8.9 and 9.0It needs review, please.
Comment #5
kishor_kolekar commentedplease review new patch.
Comment #6
hchonov@kishor_kolekar, even though they are correct your changes are out of scope here. Some of them are already covered in #3106228: \Drupal calls should be avoided in classes, use dependency injection instead in user module.
Comment #7
matsbla commentedThis is a duplicate of this issue:
#3005641: Exception is thrown on changing "Site language" setting of a user if user account is translated