Early Bird Registration for DrupalCon Portland 2024 is open! Register by 23:59 UTC on 18 March 2024, to get $100 off your ticket.
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
Comment #1
yched CreditAttribution: yched commentedFirst 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
Comment #2
SomebodySysop CreditAttribution: SomebodySysop commentedWill upgrade. Thanks for the code!
Comment #3
SomebodySysop CreditAttribution: SomebodySysop commentedOK, 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:
Problem is, of course, that the nid saved in above code is not the nid of this node once it is submitted.
Any suggestions?
Comment #4
SomebodySysop CreditAttribution: SomebodySysop commentedAnswered my own question again.
Need to use Computed Field: http://drupal.org/project/computed_field
Comment #5
yched CreditAttribution: yched commentedUsing 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)
Comment #6
yched CreditAttribution: yched commentedcrap, I did not see your update :-)
Comment #7
(not verified) CreditAttribution: commentedComment #8
anawillem CreditAttribution: anawillem commentedI 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:
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:
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.
Comment #9
kbk CreditAttribution: kbk commentedFor 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:
Notice 'value' is replaced by 'email'...
Comment #10
manoloka CreditAttribution: manoloka commentedI can confirm that 9# it's true for me
Comment #11
Dret CreditAttribution: Dret commentedHi,
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?
Comment #12
KarenS CreditAttribution: KarenS commentedThe D5 version is no longer being supported. Sorry.
Comment #13
Dret CreditAttribution: Dret commentedI would like to have support for Drupal 6.x
Should I open a new issue?
Thanks!
Comment #14
azzerpa CreditAttribution: azzerpa commentedHow 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).