setup token

use the token custom module to create a new custom token with name "nodeemail" of TYPE: custom

and use php filter with following code

if(arg(0)!="node")
return "not viewing a node";

$node = node_load(arg(1));
return $node->field_email['und'][0]['email'];

now wherever you have tokens available you can use [custom:nodeemail]

NOTE1: assuming that email field has machine name field_email and is not translatable ('und') otherwise change accordingly

NOTE2: to get more node fields use echo print_r($node); in the custom token or look at http://drupal.org/node/49768

Who needs this custom token ?

webform 4.x adds support for tokens but if webform is attached to node using "webform block" the tokens are not populating the webform fields as reported in #1544044: Attach webform to node, email to field from node and #1617086: Webform > E-mails: Add "Content values" (ie: node field) support so custom token is needed

This custom token might not be needed after this issue is resolved #919760: Add a [current-page:object:?] dynamic token

Comments

blakedesign’s picture

I used this method to get it to work with "hidden element" display type but the email address still shows up in the source. which could be bad if a web scraper comes along. Is there a way to get it to work when using the "Secure value" hidden type so that it doesn't show up in the source.

If i select Hidden element it emails the page node author. Works but it shows the email in the source

If i select Secure value it emails the webform node author instead but at least it doesn't show the email in the source.

Here is the modified code I used.

<?php
if(arg(0)!="node")
return "not viewing a node";

$node = node_load(arg(1));
//$node_author = user_load($node->uid);
//echo print_r($node_author->mail); 
//return $node_author->mail;
$wrappedNode = entity_metadata_wrapper('node', $node);
echo $wrappedNode->author->mail->value();
?>
froboy’s picture

This entity_metadata_wrapper code was not working for me. I received an error "Notice: Undefined property: stdClass::$field_my_field in eval()". Any idea why that might be?

nocean’s picture

I'm showing the same behavior -- if set to "secure value" it pulls the token value from the Webform and not the node the block is displayed on. If set to "hidden element", it works as expected ... but is obviously not optimal, as it shows the e-mail in the source.

Any chance you came up with a solution for this blakedesign?

jnorell’s picture

As the title says, this applies to the current node only, it does not work when adding a new node. You can have it work in both places with:


if ( "node" != arg( 0 ) )
  return "not viewing a node";

if ( "add" == arg( 1 ) )
  $node = $data['node'];
else
  $node = node_load( arg( 1 ) );

// dump out the node contents if you need
// drupal_set_message( "<pre>\n" . print_r( $node, true ) . '</pre>', 'error');

return $node->field_email['und'][0]['email'];
ace11’s picture

How about if node does not have any value in field_email? Then it throws notice:
Notice: Undefined index: und in eval() (line 13 of /modules/php/php.module(80) : eval()'d code).

Because I have +300 workshops and some of those does not have email address. So how to clear that notice on workshop nodes which does not have email address?