I'm adding a field called "Contact Email", and all I want to do is automatically insert the e-mail address from the user's account.

This is the code I'm trying to use, based upon what I've gleaned from searching for a solution to this:

global $user;
return array($field_contact_email[0]['value'] = $user->mail);

This is the error I get:

The default value php code must return an array like array(0 => array('value' => 'myvalue')) but returned Array ( [0] => user@mydomain.com ) 

How do I format this code so that will will correctly insert the user's e-mail address? Thanks.

Comments

yched’s picture

Status: Active » Fixed

First of all, you should upgrade to CCK 1.4 - amongst other reasons (bugfixes, new features...), the error message for bad PHP default value is much more explicit there, and would probably have saved you the trouble asking :-)

And the answer is : it depends on the type of the field, but generally (and for 'text' fields, for instance), you'd use something like

global $user;
return array(0 => array('value' => $user->mail));
SomebodySysop’s picture

Will upgrade. Thanks for the code!

SomebodySysop’s picture

Version: 5.x-1.3 » 5.x-1.4

OK, updated to 1.4

Using this same logic, what I wanted to do next was create a field that contains a link to a process using this node's nid. I create a plain text field.

Here's the default php code I'm using:

// I found this somewhere.  It says to save the node to give it a nid
  if (!$node->nid) node_save($node);
// This creates the link I want: /mailview/mail/<node id> 
  $value = "<a href=/mailview/mail/" . $node->nid . ">Click Here to create Email Message to send to this Group</a>";

  return array(0 => array('value' => $value));

Problem is, of course, that the nid saved in above code is not the nid of this node once it is submitted.

Any suggestions?

SomebodySysop’s picture

Answered my own question again.

Need to use Computed Field: http://drupal.org/project/computed_field

yched’s picture

Using a computed field might be more suitable than default values for this sort of things. By setting it not to store the data, the value gets computed on node load (well, not on _each_ node load, the value is cached), so you'd get the nid. But then the value would not be available in Views.

Maybe with a regular text field, and a custom hook_nodeapi in a helper module of your own, forcing the value of the field on 'insert' time (in hook_node_api($op = 'insert') the new node has received a nid)

yched’s picture

crap, I did not see your update :-)

Anonymous’s picture

Status: Fixed » Closed (fixed)
anawillem’s picture

I created a node-type.tpl.php for a specific php node type (node-projects.tpl.php), and set a cookie to remember the title of the node:

<?php 
global $auto_projects
$auto_projects = $title;
setcookie("project_name", $auto_projects, time()+3600);
?>

On that same page, I display some views where children nodes of that node are displayed (activities), and you can click 'here' to add a new activity. When you add a new activity, there is of course a node-reference field (field_il_project) where the user has an opportunity to reference a 'projects' node.

What I want is to set the default for that field to be the cookie that I set. That way, when a person click to add a child node from the 'projects' page, 'field_il_project' is automatically populated with the cookie that was set. The code I am using (among many I have tried) to automatically populate the 'field_il_project' field is:

array(
global $auto_projects
return array('field_il_project' = $auto_projects->project_name);
);

It fails. Doesn't even let me save the field...

Am I going about this correctly? Is there an easier way? Is there a mistake I am making in the PHP? Any help would be greatly appreciated.

kbk’s picture

For the record, the code in #1 will not work if you are using Email Field, here is the code needed to pull in a user's email address and put it into a CCK Email Field:

global $user;
return array(
  0 => array('email' =>$user->mail)
);

Notice 'value' is replaced by 'email'...

manoloka’s picture

I can confirm that 9# it's true for me

Dret’s picture

Status: Closed (fixed) » Active

Hi,

I would like to print the node's creation date inside a cck field but i can't reach this goal with PHP defualt value.

Someone have any idea about?

KarenS’s picture

Status: Active » Closed (won't fix)

The D5 version is no longer being supported. Sorry.

Dret’s picture

Version: 5.x-1.4 » 6.x-2.9
Status: Closed (won't fix) » Active

I would like to have support for Drupal 6.x

Should I open a new issue?

Thanks!

azzerpa’s picture

How would I set a default "now" value for the CCK Time Field so that the default value is always the current time? Does anyone know any PHP code for that?

(I don't want to use the Date field for the time).