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?

  • I don't know if it could be useful, but perhaps you should look at EntityAPI module.
    – arrubiu
    Sep 11 '13 at 9:11
  • hook_entity_load() doesn't have the same warning, maybe that's the way to go?
    – Clive
    Sep 11 '13 at 12:30
  • It seems the same problem would be present for that hook, since Drupal first load the user objects from the cache, then loads the the user objects left from the database, and invokes hook_entity_load() and hook_user_load() for those objects loaded from the cache. The problem is that: Objects retrieved from the cache are not passed to those hooks.
    – apaderno
    Sep 11 '13 at 19:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.