Hi

I am using 4.7-beta3 and I want to add some data to $user as its database intensive but changes very slowly so I don't want to recalculate it every time the block renders.

I added to my_user() the following:

  case 'load': // insert into the user object hte list of books that have been subscribed to
   $user->fhsst_books = $books;
   $user->fhsst_stats = _user_fhsststatistics();
  //var_dump($user);
  break;

The var_dump shows exactly what I expect. When I try to access $user->fhsst_stats in my_block() it isn't there anymore!

Is there some special syntax for adding to $user so that the information doesn't disappear? From what I've read it seems as though I've done the right thing but it isn't working and I can't find an example thats more complicated that what I have done that I understand :-/

Any advice or pointers would be most welcome.

Thanks,

Mark

Comments

tdailey’s picture

in your block you need to declare $user.

global $user;
var_dump($user);

works, just calling $user without declaring it global first won't work.

DriesK’s picture

the important thing is that you pass $user by reference in the hook_user function definition:

function yourmodule_user($op, &$edit, &$user) {

(note the ampersand '&')

edit: see profile module as an example

marknewlyn’s picture

I think I have all of that covered, here is my _user:

function fhsstadmin_user($op, &$edit, &$user, $category = NULL)
{
$books = _findbooks();
// return list of books selected by user
switch($op)
{
case 'register':
 $form['fhsstadmin_register']['fhsstadmin_register'] = array(
  '#type' => 'radios',
  '#title' => t('Choose the book you would like to start with:'),
  '#options' => $books,
  '#description' => t('Users with the <em>administer nodes</em> permission will be able to override these options.'),
);
return $form;
break;
case 'login':
  break;
  case 'load': // insert into the user object hte list of books that have been subscribed to
 $user->fhsst_books = $books;
 $user->fhsst_stats = _user_fhsststatistics();
//var_dump($user);
break;
case 'view':
  $form['fhsstadmin'] = array(
    '#type' => 'fieldset',
    '#title' => t('Book subscriptions'),
    '#collapsible' => FALSE,
    '#collapsed' => FALSE,
  );
  foreach (array_values($books) as $book) {
  $form['fhsstadmin'][$book] = array(
  '#value' => $books,
  );
  }
 return $form;
break;
case 'insert':
  db_query("INSERT INTO fhsstadmin_userbooks (uid, bookparentid) VALUES (%d, %d)",$user->uid,$edit['fhsstadmin_register']);
  $edit['fhsstadmin_register'] = NULL;
break;
} // end switch
}

I was just following the other examples but I thought that maybe there was something missing?

--
Mark Horner
Jabber/AIM/Yahoo/Gizmo/Skype/Google: marknewlyn

Co-author:
http://www.nongnu.org/fhsst
http://savannah.gnu.org/projects/fhsst

"Life is but a seg-fault away ...

Life received signal SIGSEGV, Segmentation fault.
0x42074d40 in