0

Load additional profile user data

Posted July 2nd, 2010 in by thomas

if you want to load additional data to user object with hook_user (from content profile module for example) for using it in one of your custom function, the load order of the hooks can be problematic.

if you do

function site_helper_user($op, &$edit, &$account, $category = NULL) {
if($op=='load') {
$account->loaded_content_profile = content_profile_load('profile', $account->uid);
}
}
 
function site_helper_myfunc() {
// not set
$GLOBALS['user']->loaded_content_profile;
...
}

$GLOBALS['user']->loaded_content_profile will not be set because site_helper_myfunc will be executed before site_helper_user .

the trick is to call hook_user into hook_init to be sure it’s executed first ! (organic group module do that…) and save the result in a session variable to avoid a call on every page

function site_helper_init() {
 if ($GLOBALS['user']->uid) {
 site_helper_user('load', array(), $GLOBALS['user']);
 }
}
function site_helper_user($op, $edit, &$account, $category = NULL) {
 if($op=='load') {
 $account->loaded_content_profile = content_profile_load('profile', $account->uid);
 }
}
function site_helper_myfunc() {
// set
$GLOBALS['user']->loaded_content_profile;
...
}

notice we can’t no more pass $edit by reference…

Leave a Reply