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

5

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(); 

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.