Here is a small patch to add a token to get the email address of a user referenced with a CCK user reference field. This is especially useful with workflow_ng to send email to an "assigned user" or similar in a workflow.

I originally did the patch for token in D5, have been requested to submit this for CCK in D6 as well

Anton

Comments

asciikewl’s picture

asciikewl’s picture

For some reason the patch isn't attaching, lets try again

asciikewl’s picture

StatusFileSize
new814 bytes

Sigh, The attachments are landing up in attachment heaven. One last try and if that doesn't work, here is the diff as past of the comment (it is small enough):

--- content.token.inc.orig      2008-06-20 17:16:22.000000000 +0200
+++ content.token.inc   2008-06-20 17:17:18.000000000 +0200
@@ -132,6 +132,7 @@
 
       $tokens['user reference']['uid']   = t('Referenced user ID');
       $tokens['user reference']['name']  = t('Referenced user name');
+      $tokens['user reference']['mail']  = t("Referenced user email address");
       $tokens['user reference']['link']  = t('Formatted HTML link to referenced user');
 
       return $tokens;
@@ -142,8 +143,10 @@
     if ($type == 'field') {
       $item = $object[0];
 
+      $refuser = user_load(array('uid'=>$item['uid']));
       $tokens['uid']   = $item['uid'];
       $tokens['name']  = strip_tags($item['view']);
+      $tokens['mail']  = $refuser->mail;
       $tokens['link']  = $item['view'];
 
       return $tokens;
greggles’s picture

subscribe.

@asciikewl - if you use the "attach" or preview buttons then the attachments disappear. If you just hit "post comment" they'll go through.

matt_paz’s picture

+1 This is a great addition!

yched’s picture

Only thing is that it requires a call to user_load() (which isn't cached). even if the token that is actually used doesn't need it. Do we want to go that route ?

greggles’s picture

Well - how about a straight query to get it.

I'm not sure how long user_load takes but I agree that we should be careful with making "expensive" tokens...

matt_paz’s picture

Good call.

asciikewl’s picture

StatusFileSize
new854 bytes

Here it is with a query instead of the user_load. Is this still the right way to do it in D6?

--- content.token.inc.orig      2008-07-10 16:46:45.000000000 +0200
+++ content.token.inc   2008-07-10 16:46:52.000000000 +0200
@@ -132,6 +132,7 @@
 
       $tokens['user reference']['uid']   = t('Referenced user ID');
       $tokens['user reference']['name']  = t('Referenced user name');
+      $tokens['user reference']['mail']  = t("Referenced user email address");
       $tokens['user reference']['link']  = t('Formatted HTML link to referenced user');
 
       return $tokens;
@@ -142,8 +143,10 @@
     if ($type == 'field') {
       $item = $object[0];
 
+      $refuser_mail = db_result(db_query("select mail from {users} where uid=%d",$item['uid']));
       $tokens['uid']   = $item['uid'];
       $tokens['name']  = strip_tags($item['view']);
+      $tokens['mail']  = $refuser_mail;
       $tokens['link']  = $item['view'];
 
dopry’s picture

Status: Needs review » Needs work

how does this patch handle multiple value user reference fields?

Anonymous’s picture

I'd be interested in this if it in fact deal with multiple users.

B4n7o’s picture

sorry, how do i implement this code, do i make a new module and paste is, or add this to existing cade

clarkburbidge’s picture

I'd be interested in more information on how to use the user reference to gather custom profile information in addition to email. I'd love to see a generic query that I could use in the cck computed module. I also am interested in how a multi-select user field is treated.

smokris’s picture

Version: 6.x-2.0-beta » 6.x-2.1
Status: Needs work » Needs review
StatusFileSize
new1.84 KB

The above patch only tokenizes the first user in multi-user fields.

I've attached a new patch which concatenates email addresses (comma-separated), ready for use in an email Action for example. For the remaining fields, only the first user is tokenized.

Could this patch be reviewed for inclusion in cck?

Heilong’s picture

Hi,

I applied the patch to cck and I got this output message :

patching file content.token.inc
Hunk #2 FAILED at 171.
1 out of 2 hunks FAILED -- saving rejects to file content.token.inc.rej

Here is what I have in the .rej file :

***************
*** 170,183 ****
  
    function userreference_token_values($type, $object = NULL) {
      if ($type == 'field') {
-       $item = $object[0];
- 
-       $tokens['uid']   = $item['uid'];
-       $tokens['name']  = isset($item['view']) ? strip_tags($item['view']) : '';
-       $tokens['link']  = isset($item['view']) ? $item['view'] : '';
-       $tokens['path'] = url('user/' . $item['uid']);
-       $tokens['url'] = url('user/' . $item['uid'], array('absolute' => TRUE));
- 
        return $tokens;
      }
    }
--- 171,191 ----
  
    function userreference_token_values($type, $object = NULL) {
      if ($type == 'field') {
+       $first=true;
+       foreach($object as $item) {
+         if($first) {
+           $first=false;
+           $tokens['uid']   = $item['uid'];
+           $tokens['name']  = isset($item['view']) ? strip_tags($item['view']) : '';
+           $tokens['link']  = isset($item['view']) ? $item['view'] : '';
+           $tokens['path']  = url('user/' . $item['uid']);
+           $tokens['url']   = url('user/' . $item['uid'], array('absolute' => TRUE));
+           $tokens['mail']  = '';
+         }
+         $user_mail = db_result(db_query("select mail from {users} where uid=%d",$item['uid']));
+         $tokens['mail'] .= $user_mail.',';
+       }
+       $tokens['mail'] = trim($tokens['mail'],',');
        return $tokens;
      }
    }

The token "username-mail" which one I need is appearing but as I got failing messages I'm not sure i can use properly.

otsuarez’s picture

@Heilong
the code do work, but since the code on the module had changed, you cannot apply the patch as it is.
this two lines (in the patch):
- $tokens['path'] = url('user/' . $item['uid']);
- $tokens['url'] = url('user/' . $item['uid'], array('absolute' => TRUE));
should be replace by this ones:
- $tokens['path'] = is_numeric($item['uid']) ? url('user/' . $item['uid']) : '';
- $tokens['url'] = is_numeric($item['uid']) ? url('user/' . $item['uid'], array('absolute' => TRUE)) : '';
so basically, you can use the code from the patch and either, create a new patch or just change the code.
regards,
osvaldo

markus_petrux’s picture

Status: Needs review » Needs work

This needs work, but since token module generates all possible values even when you may need just one, I think adding queries here to read the user mail will affect performance a lot, not to mention if this integration deals with multiple value userrefs.

So I would really say won't fix, and think about it in contrib, where this feature can be enabled or disabled on a per field basis.

greggles’s picture

Note that in 7.x tokens are generated on an as-needed basis so that is no longer a concern.

Note also that in 7.x there are not tokens for the cck fields in core, if anyone wants to work on that it would be great ;)

markus_petrux’s picture

Status: Needs work » Postponed

Right, and I think token stuff in D7 is going to be backported to D6, so this issue could be awaken when that happens.

markus_petrux’s picture

Version: 6.x-2.1 » 6.x-2.x-dev
Component: userreference.module » Token Integration

eaton wrote:

#113614: Add centralized token/placeholder substitution to core has the current state of the patch. It's also being kept in sync in the HEAD branch of token module. The 2.x branch for Drupal 6 will be backported when the final fate of token in D7 is ironed out.

However, I'm not really sure which is the issue in the Token module queue where this task is taking place. ¿?

tomsm’s picture

subscribing

joetsuihk’s picture

A note for later comer:
as of cck 2.6, and if your user_reference only takes 1 user, #9 will work

as of cck 2.6, no matter how, user_reference token always returns first user token, which should be a bug, may refer to #15 for temp. solution.

in a way, i dont know why the development of this patch have to wait for D7...(D7 is out, we can throw D6 anyway)

my-family’s picture

subscribe, I would appreciate the possibility of multiple users token (for Rules etc.) very much

ayalon’s picture

Category: feature » task
Status: Postponed » Needs review
StatusFileSize
new1.91 KB

I have re-rolled this patch against the current version of cck.

This patch is very important for Issue / Task Management and rules. You will be able to send an email, if the assigned person has changed.

This patch is carefully tested on a live page and works well.

Please review it and commit the patch to the next release.

bitbaud’s picture

Greetings,

Can someone please tell me how to implement this patch? I haver never patched a module before.

I am using drupal 6 with CCK, tokens, and rules. I am trying to send an email to the mail addresses of mutiple users referenced by the user reference cck field. Thanks!

Clif

josefo@btinternet.com’s picture

Title: Token for mail address of user in CCK user reference field » How can I install this patch?
Category: task » support
Priority: Normal » Major
Status: Needs review » Active

Hello,

How do I install this patch on my Drupal 6.16? Sorry for this simple question but I am quite new within using Drupal 6.16.

content.token_.inc-6x-1.13-referenced-mail.patch

Thank you.

greggles’s picture

Title: How can I install this patch? » Token for mail address of user in CCK user reference field
Category: support » task
Priority: Major » Normal
Status: Active » Needs review

Please don't hijack issues. The answer to your question is easily found in the search: http://drupal.org/search/apachesolr_search/apply%20patch

mr.andrey’s picture

Status: Needs review » Reviewed & tested by the community

tested. works great.

marcp’s picture

This patch still applies (with offsets), but I think the handling of multiple values belongs in a separate issue. I'd also rather see the user_load() call than a separate SQL call -- in D7, at least, user_load() will cache user objects.

But I guess what I'd really like to know is if this has any chance of getting into 6.x for CCK. It's similar to #800646: Nodereference author as token, and I think they both belong in CCK, but if there are performance concerns or other reasons why it makes sense to keep these out, then I'd be happy to create a separate "cck_reference_tokens" module where these things can live.

Any chance this (or a variant) would get into CCK 6.x?

marcp’s picture

Status: Reviewed & tested by the community » Needs review
StatusFileSize
new1.2 KB

Attached is a patch that applies cleanly to DRUPAL-6--2 but it has user_load() and only gives you the email address of the first userreference (in the case of a multiple value userreference field).

For dealing with multiple values, it seems like the Array Tokens module might be the place to go.

Note that you can apply the patch by putting it in your sites/all/modules/cck directory and running:

patch -p 0 < cck-userreference-mail-token-272949-30.patch
koleary’s picture

Status: Needs review » Reviewed & tested by the community

This patch worked well for me. Thank you for your efforts!

radj’s picture

This is for 6.x but I think this issue also needs to be in 7.x Fields. Is there an existing issue for 7.x? I can't find it, I always keep going back to this page.

Ndesign’s picture

rjs.martins’s picture

Hi, i only have a question.

If i use the token that is created i want to know if:

The token contains only the email of the users that were selected in the referenced field?

Or

The token contains the email of all the users that exist in the referenced field, even if they were not selected?

Thanks!

rjs.martins’s picture

The patch from #24 worked perfectly for me and it only sends email to the referenced users you select and that's what i wanted.

Thank you so much for this patch aylaon. :-)