I am using drupal 6.3 and php 5.25. When trying to use the function user_current_load(), I get the following error:

Fatal error: Call to undefined function user_current_load() in C:\wamp\www\drupal-6.3\sites\all\modules\emailusers\emailusers.module on line 158

Does this function not exist? The book I am reading uses this function, but it's not working, and I couldn't find any documentation on the function in api.drupal.org.

Comments

Jaypan’s picture

Upon further research, I have found the function user_load_self(). This seems to do approximately the same thing. I had to tweak my code a little to get at the data I wanted, but I was able to get it to work.

But does anybody have any idea what happened to user_current_load()?

RTauson’s picture

I'm getting the same problem, is this solved or should I use the user_load_self() instead, in that case how do I get the users email address out of the returnment of user_load_self() ?

gpk’s picture

just do

global $user; // get the global user object for the current user
print $user->mail; // user's email address

You only need to do a full user_load if you need additional user properties such as those provided by other modules.

gpk
----
www.alexoria.co.uk

gpk’s picture

I guess you're using the Packt book? I've heard it's rather full of errors - I think it was written before the D6 APIs were finalised and so contains a lot of out-of-date information.

gpk
----
www.alexoria.co.uk

Chill35’s picture

It is most probably the book Learning Drupal 6 Module Development we're talking about here. Although fantastically written, and invaluable in how it explains Drupal concepts and APIs, most of the examples do not work as is, and there are several typos in the code snippets as well.

So, how does one use user_load_self() to get the current user object?

Here is the function definition:

function user_load_self($arg) {
  $arg[1] = user_load($GLOBALS['user']->uid);
  return $arg;
}

And here is how I use it:

// Look up current user's email address.
$user_current_array = user_load_self(NULL);
$user_current = $user_current_array[1];
$message['headers']['bcc'] = $user_current->mail;

One can also use global $user as well, as explained by gpk. Here is an alternate way of reading the global user:

$message['headers']['bcc'] = $GLOBALS['user']->mail;

Caroline
11 heavens.com

gpk’s picture

I never could quite work out what all that $arg stuff is in user_load_self() since $arg is never used and the return value ends up as an array with 1 element which is the object you actually want. Unless you actually pass a $arg, but I can't see a use case for that. Maybe some Drupal/PHP subtlety I'm not aware of!

gpk
----
www.alexoria.co.uk

Chill35’s picture

Yes, that function looks weird... well beyond weirdness.

As it stands now, it is not even called once in Drupal core (6.4). That tells me that it will probably be removed anyway, or changed into being more useful.

Better read from the global user here, anyway, as you suggest.

Caroline
11 heavens.com

gpk’s picture

OK glad it's not just me then!. Looks like it's a remnant of something else that never quite saw the light of day.

gpk
----
www.alexoria.co.uk

gtakacs’s picture

I'm also working through this tutorial at the time.

It seems like the hook_mail breaks other mailings. If I have this particular module enabled every e-mail that goes out (new user registration, lost pasword, etc.) loses the text of the e-mail and it is replaced with the word "Array" It seems like the mailer ends up with an array of e-mails and doesn't know what to do with it. Note that all the admin e-mails end up getting the same disclaimer text that this module adds so it seems like hook_email_alter is also affecting the situation.

Any solution to this one?

gtakacs’s picture

The problem with the code is this in emailusers_mail():

<?php
$message['body'] = $params['body'];
?>

In emailusers_mail_alter():

<?php
$message['body'] .= t($append, $args);
?>

But if you look in drupal's mail.inc file in drupal_mail() you will find this:

<?php
$message = array(
    'id'       => $module .'_'. $key,
    'to'       => $to,
    'from'     => isset($from) ? $from : $default_from,
    'language' => $language,
    'params'   => $params,
    'subject'  => '',
    'body'     => array()
  );
?>

So it is clear that Drupal is expecting an array of elements for the body object and all internal Drupal mailers do generate arrays. But the code in the example module generates a single string for the $body. So the fix is:
In emailusers_mail():

<?php
$message['body'][] = $params['body'];
?>

In emailusers_mail_alter():

<?php
$message['body'][] = t($append, $args);
?>

The only problem still remaining is that emailusers_mail_alter() actually modifies every single e-mail that is sent from now on by attaching the footer to the bottom of every single message that is sent by Drupal. To remedy this problem you can modify the emailuser_mail_alter() hook to only append the disclaimer for the correct message key such as:

<?php
  if ($message['id'] == 'emailusers_composemessage'){
    $message['body'][] = t($append, $args);	
  }
?>

devGuy’s picture

One slight typo, though... should be appending to the body, so use ".=" instead of plain old "=", as follows.

  if ($message['id'] == 'emailusers_composemessage'){
    $message['body'][] .= t($append, $args);   
  }

devGuy

cincy_kid’s picture

So yea, I am going through the 2008 Packt book as well and am on this part.

I made the changes...

In emailusers_mail():

<?php
$message['body'][] = $params['body'];
?>

And in emailusers_mail_alter():

<?php
$message['body'][] = t($append, $args);
?>

and it works fine, the problem I am having is that I am not getting copied on the email per this line:

<?php
$message['headers']['bcc'] = $GLOBALS['user']->mail;
?>

I have also tried:

<?php
global $user;
$message['headers']['bcc'] = $user->mail;
?>

But I never get copied.

Any idea why?

esod’s picture

It was difficult to add the bcc to the message header since "user_current_load()" doesn't (no longer?) exist, and "user_load_self()" is awkward to use.

Utilizing the global $user as previously suggested in this discussion:

$message['headers']['bcc'] = $GLOBALS['user']->mail;

added the current user's email address to the message header bcc.

At least that's what solved it for me on my Dreamhost shared server. Although I doubt you are experiencing a php.ini issue, since Drupal uses its own mail library.

RoloDMonkey’s picture

Before you come down too hard on Packt, you should know that all technical publishers maintain Errata pages. It is always a good idea to look those over, especially with an older book.

Take a look at this link:

http://www.packtpub.com/support?nid=1105#errata

And you will find that the issues discussed in this thread are corrected in the sections for Page 167 and 173.

--

Read more at iRolo.net

esod’s picture

Thanks. I'll check the errata page again. I'm getting a lot out of the book. I enjoyed the author's "Drupal 6 JavaScript and jQuery" book very much as well.