Hi everyone (anyone)

I have done my best to solve this issue of adding my own information into the $user object when it is loaded.
I checked the API for hook_user and implemented
what I thought was the correct solution but it didn't work. To ensure that I had done it correctly I made a clean install
of Drupal and built a whole new module based on block_example so that it was clear what I was doing and nothing else could
affect it.

I posted my original question node/46265 and my stripped down example module node/46414 but I haven't heard anything back from anyone. I did look at other modules like og
and I can't see what I am doing wrong. In fact I think I am doing the correct thing.

I would really like to solve this as it would stop me doing very many unnecessary db queries and vastly improve the quality of
my module.

Please, please could someone help me out here?

Thanks,

Mark

Comments

nevets’s picture

The setup in both examples look fine, but no where do you say why you think there is a problem. An example of what is not working would helpl.

Also, something to try, place a call to drupal_set_message in the user hook to make sure it is being entered, maybe something like drupal_set_message('you are here'). If you do not see the message on loading a new page, your module might not be enabled, the filename might not match the module name or for what ever reason the user information is not being loaded.

Also, if you do not see the message you might want to consider adding drupal_set_message calls in user_load (in user.module) to see what it is doing.

marknewlyn’s picture

Hi

Sorry - I didn't want to say too much but I should have said more.

The problem is that the information I add into the $user object during "load" isn't there when I try to use it later.

In my block example the "special string" is blank in the block even though I added it during "load".

I know that if I print_r($user) after I have added the string in _user it is indeed there but if I do the same when I am printing the block it isn't there anymore.

Its like the passing by reference doesn't work?

I am very stuck on this.

Mark

nevets’s picture

The other question would be are you declaring $user global in the block? Can you print $user->name?

marknewlyn’s picture

Hi

The username does print in the block correctly its just what I added that doesn't. Thats why I am so stuck.

:-/

Mark

nevets’s picture

Have you tried to print the user information in user_load after the call to module_invoke? Does it look ok there?

marknewlyn’s picture

Sorry about the delay (meeting at work).

I tried what you suggested and modified user.module by adding a simple
print_r($user);
right after the
user_module_invoke('load', $array, $user);

and the output on the screen includes the "specialstring" that I added. It still doesn't show up in my block so I added a
print_r($user);
right after the
global $user;
in the block and when it comes up on the screen there is no "specialstring".

Something gets rid of it?

Mark

nevets’s picture

Is the user id/namethe same for both cases (n code and in the block)? Which version of PHP are you using?

marknewlyn’s picture

The username that shows up in the block is the correct one for the logged in user.

I did:
/usr/bin/php -v
PHP 5.0.5-2ubuntu1.1 (cli) (built: Dec 23 2005 10:33:54)
Copyright (c) 1997-2004 The PHP Group
Zend Engine v2.0.5, Copyright (c) 1998-2004 Zend Technologies

Someone (at coffee) said that maybe the information isn't being added to the session. I am not sure how to check this though?

Thanks for all the help!

Mark

nevets’s picture

If I had a hook_user function AND I set two fields to string values I see the same behaviour. If I log out and log back in, I can see the values BUT only right after log in, if I change pages though they go away, suggesting the is a caching issue.

marknewlyn’s picture

Hi

I never see the correct string but it might be because of what first page I show for a logged in user if it is a caching thing?

I am really glad someone else sees it though :) I am not completely nuts yet.

Is it allowed for me to put a link to this discussion on the issue for drupal I submitted?

Thanks,

Mark

nevets’s picture

That would be a good to link to this discussion in the issue you submitted.

marcp’s picture

Hi Mark,

I've been trying to track down what is going on with this. So, at the beginning of just about every page that gets loaded, the function sess_read gets called. sess_read loads up the global $user object but doesn't call any user hooks. Note also that, after logging in, you will probably eventually hit a drupal_goto which is going to start another page loading (and stomp your previously loaded $user).

I don't know if this is the intended behavior. I would think that the core sess_read function should maybe call something (user_load???) in the user module to load up the user.

You might want to take a look at how profile.module does things. I haven't traced through a page to see if, on any page that you hit, the global $user object has its profile fields filled. I'm thinking that it probably doesn't.

So, it seems that if you want your fields loaded via 'load' through hook_user, you may need to call user_load yourself with the current $user->uid.

I will take a further look later on today, but maybe this is enough to get you a little further?

Marc

-------
http://www.funnymonkey.com
Tools for Teachers

marcp’s picture

Mark,

Take a look at this post by Raymondo. I think you may come to the conclusion that you will need to call user_load('uid' => $user->uid) in order to have your custom loading done...

- Marc

-------
http://www.funnymonkey.com
Tools for Teachers

marknewlyn’s picture

I think I understand now. Loading something into the $user isn't supposed to be "persistent" throughout the session in any way. I didn't really understand that from the API page.

My mistake was to think that the $user object would hang around in the session somehow and by adding something to it would mean it would hang around as well.

Thanks for looking into this. Its nice to be able to close it. I still have a lot to learn but I am getting there :)

Mark

lyricnz’s picture

In case anyone stumbles across this issue - it's covered again in http://drupal.org/node/282999