2

I would like to update the preferred_langcode of a user as soon as he changes the language in a language switcher block.

I'm already aware of the getPreferredLangcode method mentioned in https://api.drupal.org/api/drupal/core!modules!user!src!Entity!User.php/function/User%3A%3AgetPreferredLangcode/8.6.x and looking for a setPreferredLangcode method, if one exists.

If not, what other options do I have?

1 Answer 1

4

Seems D8 doesn't have a setPreferredLangcode method yet. So, the preferred approach IMHO would be to use the default set method of D8 Entity API as below

$user = \Drupal\user\Entity\User::load('uid');
$user->set('preferred_langcode', 'en or whatever langcode we want');
$user->save();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.