In a module I developed, I need to change $user->language
for those users who don’t have the “change own user language” permission, a permission the module itself defines. The module is actually doing two things:
- Hiding the language selector in the user edit form for those users who don’t have the permission to set a language by setting its #access property to
FALSE
- Loading the default language for those users
The first part works. For the second part, I was first using hook_user_load() but due to the following warning shown in the documentation page, I changed the code to use hook_init()
.
Due to the static cache in
user_load_multiple()
you should not use this hook to modify the user properties returned by the {users} table itself since this may result in unreliable results when loading from cache.
The problem is that with hook_init()
I am not doing anything when a module loads a user object, sets that as current user, does something, and then changes back the global $user
.
I know I could load the language in a custom property, but I should still need to change the $user->language
property before Drupal or any module uses it.
How can I change a user object property that is loaded from the users table?
Sponsored by SupremePR