Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
could you please provide me with an example on how to provide a default-value with php-code?
I'm trying to use it with a noderef-field. When using the "simple" option and choosing a node from the select-list to use as default it works, but when I try to supply the same value with php-code, I always get "<none>" (id = 0) as my preselected value.
My current try is
return array(0 => array('value' => '38'));
Where '38' is the existing node I want to point at, while what I really want to do is something like
return array(0 => array('value' => arg(3)));
so I can supply the default value to point at with the request.
I'd be grateful for any hints what I'm doing wrong.
Comments
Comment #1
yched CreditAttribution: yched commentedThe correct code actually depends on the columns declared by the field - so it's hard to provide a 'one-fits-all' example.
For nodereference, that would be :
I'm not sure whether the
arg(3)
will work - keep in mind that 'default values' are only used at node _creation_ time. So i'm not sure what you expect to find in arg(3) at that time (since the regular path will be something like node/add/my_type.Comment #2
ray007 CreditAttribution: ray007 commentedThe point is, arg(3) should give me the first paramter after node/add/my_type.
Unfortunately, the form-validation doesn't let me enter this, because when editing the node-type arg(3) returnes something enirely different ...
After some thinking I got it working in this case:
does the trick.
What am I doing here: In the some other node I have a link to
node/add/my_type/$nid
and I get a default-value pointing to the node I'm coming from.And another question: how do I know to use 'nid' as key, and not 'value' as the error-message told me?
Thanks a lot for you help!
Comment #3
yched CreditAttribution: yched commentedThe error message actually provides a mere example - but does not clearly say so. I enhanced the help text below the 'php code' textarea and the error message to provide the actual columns required by the field type.
Comment #4
ray007 CreditAttribution: ray007 commentedSounds good, thanks a lot.
Will update once I have time to verify the views filter patch still applies ;-)
May I suggest to add the leading return-statement and the final semicolon to the sample-text?
On my first few tries I just entered an expression and not a complete statement there ... but maybe that's just me ;-)
Comment #5
(not verified) CreditAttribution: commentedComment #6
strellman CreditAttribution: strellman commented#2 was a big help! This got me most of the way. I was trying to use rules redirect to create a 2nd node after saving the 1st and wanted to bring over several defaults in the URL
return array(0 => array('nid' => ((int) arg(3))));
Then I went on to pass other variables and used:
return array(0 => array('value' => arg(4)));
Somehow it never clicked that 'value' was a constant for all situations when I look at the php hint.
I was trying
return array(0 => array('field_account' => arg(4)));
which didn't work of course.
So this is for anybody else who doesn't understand PHP but wants to do some simple snippets.
Where do we go to get a bunch of little snippets like this for different situations?