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

Comments

gnunes created an issue. See original summary.

gnunes’s picture

Status: Active » Needs review
StatusFileSize
new3.45 KB
new1.74 KB

I've removed the Account::syncUserLandcode(), for both 8.9 and 9.0
It needs review, please.

The last submitted patch, 2: 3119224-1-9.0.x.patch, failed testing. View results

Status: Needs review » Needs work

The last submitted patch, 2: 3119224-1-8.9.x.patch, failed testing. View results

kishor_kolekar’s picture

Status: Needs work » Needs review
StatusFileSize
new6.03 KB
new6.1 KB
new5.3 KB

please review new patch.

hchonov’s picture

@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.