Currently, the help text under a cck text field's default value by php reads

Advanced Usage Only: PHP code that returns a default value. Should not include <? php ?> delimiters.
If this field is filled out, the value returned by this code will override any value in the textfield above.
Expected format : array( 0 => array('value' => ...), 1 => ...).

A little more elaboration on "Expected format:" like a simple, useful example to replace or augment "expected format" would be very helpful. Or, if this is common forms api speak, then a link out to a handbook would also be beneficial.

Specifically, I'm looking for how to reference itself and another field (title) in the same node being created to populate this field with the value of another when no value is entered:

($node->this_field) ? $node->this_field : $node->title;

Maybe the solution to this example would be a valid, common code example?

CommentFileSizeAuthor
#12 Export_Simple_Dropdown.txt1.63 KBmgifford
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

rjleigh’s picture

yched’s picture

Status: Active » Fixed

Yup - thanks rleigh

Anonymous’s picture

Status: Fixed » Closed (fixed)
Eep²’s picture

That doesn't help, and is for database queries anyway. Specific non-database examples would be nice... See content default value automation for another thread about this.

yched’s picture

"... and is for database queries anyway".

The examples happen to build the values from a db query, but that's not important, and this gives you the information you're looking for anyway...

Bottomline here is we can't give you a general rule for how what the php 'default value' should look like, for it changes for every field type. Text fields have to define 'value' and 'format' values, nodereferences use 'nid', imagefields still something different.
The most reliable way is to
- install devel module,
- go on the 'devel load' tab on a node that contains the field you're interested in
- see what the field_youfieldname array looks like for that node. There you got it, this is the kind of array you need to build in the PHP default value setting.
Then you probably see why it's called 'advanced'...

Eep²’s picture

Well, why can't this Devel module be integrated into CCK's "default value" field (wherever) so all the end-user needs to do is stick in, at most, the very basic PHP variable (whatever) that directly relates to what is desired?

This goes back to my other comment about Drupal still being by developers for developers, and not for the average non-programming end-user...

goose2000’s picture

I was the guy who got referenced above. I still agree with these folks, this format just isn't acceptable.
Expected format : array( 0 => array('value' => ...), 1 => ...)

Really, take a cue from the auto-node title module instructions:
"Put PHP code above that returns your string, but make sure you surround code in and "

yched’s picture

The auto node title example you're using is moot, the complexity of the information that needs to be provided is nothing comparable.

This field is marked "advanced", obviously because you need to know a little bit about CCK concepts (multiple values, columns...) to use it. I welcome any constructive suggestion here, but am starting to be a little fed up with "why don't you just (insert nonsense here)" and "this unacceptable" stuff.

goose2000’s picture

A simple static example that works, maybe clear things? :

$salaries["Bob"] = 2000;
$salaries["Sally"] = 4000;
$salaries["Charlie"] = 600;
$salaries["Clare"] = 0;

return array(array('value' => $salaries[Bob]));

So here we have a little associative, and the value for Bob will go into the CCK field.
An index array would work just the same, using the numeric index key to get the to the value.

$hopeyoulikearrays[0] = "hell no.";
$hopeyoulikearrays[1] = "98% of PHP is all about arrays!";
$hopeyoulikearrays[2] = "ohh, a list of things";
$hopeyoulikearrays[3] = "I'm not sure, but we can fix it with an array";

return array(array('value' => $hopeyoulikearrays[2]));
yched’s picture

This is exactly what we can't afford. As I explained in comment #5 above, there is no way CCK can provide a one-fits-all 'static' example : *this changes for every field type*. An example based on a text field will be irrelevant for a nodereference field, and thus simply add confusion.

I committed a slightly more explicit *generic* help text in the 1.x-dev branch - I'm afraid that's all I can offer.

goose2000’s picture

I know, people get frustrated. And I could think of several example 'cases' , just for beginners. Still ( I understand ), you'd have to have a structure like this for say a select drop-down, makes sense. It is a bit more advanced and the less skilled panic I think when they see the format. It's not that hard to learn. I'm leaning and this is alway a small miracle. Don't want to cause troubles, appreciate the CCK work, thank you. Thanks for the help text update too.

mgifford’s picture

FileSize
1.63 KB

Ok.. Let's take a very, very simple case like the one I've just attached.

I want to use the the PHP code in the Default value to return one of the items from the list. I stuff this into the field hoping that it will set the default to be Blue:
return array(array('key' => 'b'));

It doesn't give me an error, but it also doesn't work.

I've provided a Diff comparing the Simple Dropdown with the default selected manually (attached) and my array above.

diff Export_Simple_Dropdown.txt Export_Simple_Dropdown_PHP.txt
47c47
< 'key' => 'c',
---
> 'key' => false,
50c50
< 'default_value_php' => '',
---
> 'default_value_php' => 'return array(array(\'key\' => \'b\'));',
66,69d65
< 0 =>
< array (
< 'value' => 'c',
< ),

Please provide me with a simple working example that I can just import into my cck and compare with what I've done.

xtfer’s picture

Having wrestled with this for hours, I finally discovered that I needed to include a "return" in my code.

$default_value = array( array('value' => 1) );
return $def_value;

Can that be included in the example in the module somehow? I realise that this is for the so-called "advanced user", however this "advanced user" was misled by trying to follow the current example.

Thanks, xtfer.

lucidD’s picture

i'm having trouble getting the default field for a node reference cck field to default to it's own nid. can someone help me with this?

AlbertAlpha’s picture

You can use something like this:

$sql = "SELECT nid FROM node WHERE type='page' ORDER BY created DESC";
return db_fetch_all_as_arrays($sql);

The function 'db_fetch_all_as_arrays()' is included in the Helpers module.

anasynth’s picture

I'm having the same problem with Content Taxonomy for Drupal 6, which gives the same expected format rule. Is it just me, or do all the solutions offered seem like they should work but in fact don't? Every php snippet I've tried either produces a complaint about not matching the expected format, or produces no complaint but then seems to not return any default value to the field when I try and create a piece of content to which the field applies.

This is the php snippet I'm trying at the moment. Its supposed to return the value of the "personal tag" profile field, which I created using the Profile module, for the currently logged in user. It does not cause an error due to the input format, so I assume that's OK, but it doesn't return a value to the field by default when I go to create content.

global $user;
profile_load_profile($user);
$tag = $user->profile_personaltag;

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

I've tried simply "return $tag;" with the rest of the code in a normal node body with the input set to php and it returns the value I expect fine.

If any one can correct my code or can provide a link to a definitive answer, it would be greatly appreciated.

anasynth’s picture

I've just tried out the Devel module, as it suggests using "Dev load", which tells me that the value of my Content Taxonomy field is (NULL) which I guess confirms the fact that my php snippet is returning nothing...

anasynth’s picture

I managed to write a php script that consistently returns the expected value and enters it as a default value into my field. The module that I was having trouble with was Content Taxonomy, which I know isn't the subject of this thread but I was having exactly the same problem with the same default php code submission rules so I think this is relevant.

global $user;
profile_load_profile($user);

$current = $user->uid;

$term = db_query("SELECT tid FROM {term_user} WHERE uid='$current'");

$result = db_fetch_object($term);

$result = $result->tid;

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

This is the php script that I am using, I suppose the most relevant part is the form of the return statement return array(array('value' => $result));. I think that array('value' => $result) this section assigns the contents of the variable $result to the value member of the array. The contents of the value member are then returned in an array format by the return statement return array(...));

This is what I believe is happening, but I'm a little rusty on coding (C, php or otherwise) so correct me if I'm wrong. Any way I arrived at the form of the return statement after a lot of trial and error and it works consistently on my site so I thought it would be worth sharing. I hope people find it useful.

1kenthomas’s picture

Version: 5.x-1.x-dev » 6.x-2.5
Status: Closed (fixed) » Active

Marking active.

This has not been resolved.

This is how I define a "problem:" when multiple people clearly cannot understand what's going on or easily find an answer or clear explanation-- leading to unnecessary frustration and lost time.

Equally, just because we cannot find a clear explanation at the moment, does not mean that a problem has been solved.

In this case, my idea of a solution would be a summary explanation (similar to what is currently on PHP input, but... sorry, the current summary is a tad unclear) and a link to further information (examples!) on d.o. or through advanced help. A page that begins something like "we are sorry, but setting default values via PHP is sometimes complex. Here are some examples to get you started."

Comments and input always welcome ;)

markus_petrux’s picture

Status: Active » Closed (fixed)

A rant is not enough reason to re-open an issue. Hints, examples and a method to figure out the exact format of the array have been given.

Raf’s picture

Category: support » bug
Status: Closed (fixed) » Active

Reopened. Got the same problem, and I AM an advanced user. According to the "shut up you're stupid it's only for advanced users" explanation above, I SHOULD be able to use this.

I've actually struggled with this on and off for as long as I've been using Drupal, regularly using hook_form_alter instead of this, since I couldn't get this to work. The explanation simply isn't clear. The examples given above don't help either.

For example:
Right now, I got a content type that has a simple, single-row textfield with plain text as input. I think that's about as basic as it can get.

What I'm trying to do, is have the currently logged-in user's email address filled in there, but can't get that to work.

To see if, maybe, there're some problems using the $user object, I replaced it with "raf@test.com". I used the Dev Load tab to check the format. The format's correct, yet it just doesn't work. My best bet right now, from everything I've tried, from all the instructions I've followed, and from everything I've learned since I've first started using Drupal, I'd say PHP Input is simply broken and needs to be fixed.

Here's what Dev Load says:
field_profile_email (Array, 1 element)
--> 0 (Array, 1 element)
----> value (String, 21 characters ) raf@mycustominput.com

This is what I filled in as PHP Input:
return array(array('raf@test.com'));
(And variations, like return array(0 => array('raf@test.com'));, return array(0 => array('value' => 'raf@test.com')); and return array(array('value' => 'raf@test.com'));)

If I create a new node of that type, it just shows an empty field, not 'raf@test.com' prefilled in.

I'd say the thing's pretty damn broken because of this. I've tried everything, followed all instructions and examples, and it doesn't work. Obviously, reading this thread, I'm not the only one with this problem. Saying: "Shut up, you're stupid, only smart people can use it!" doesn't actually fix the thing. The least thing to do, is at least check to see if it actually does work, or check this or previous examples that were said to not work and see if they work. Right now, everybody who says it doesn't work, gets a kick in the nuts, which doesn't help anybody at all.

Raf’s picture

Ok, now it works.

return array(0=> array('value' => 'test')); does the trick

But... what's the difference between
return array(0=> array('value' => 'test'));
and
return array(0 => array('value' => 'raf@test.com'));

The first one works, but when I first tried the second one, it didn't. There's no flushing caches between trying the two, just trying YET another time. Why does it magically work one time and not at all another (actually months and months and months and endless amounts of trying that very same syntax)?

Joni Haeck’s picture

Hi! I'm looking for a way to set the default value of a field to the name of the logged in user. I keep getting server errors or syntax errors in de php field in the default value section in my field... Can anybody help me please?

benchesters’s picture

I've been scratching my head for ages with this..This works fine, as you say:

return array(0=> array('value' => 'test'))

But how do I print the node title out?

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

this won't work and I tried various different versions...of course, I am no php genius as you can see!