Hi,
I am using the Automatic Nodetitles module to automatically generate a node title based on some CCK-fields. It works as expected for ordinary text fields, but accessing the selected value of a select list always results in an empty string.
Here is the code I use in the Automatic Nodetitles module:
<?php
global $form_values;
$node = (array)$form_values;
$casenumber = $node['field_caseno_type'][0]['value'] . ' ' . $node['field_caseno_seqno'][0]['value'] . '/' . substr($node['field_caseno_year'][0]['value'],-2);
$headword = $node['field_headword'][0][value];
if ($headword) {
print $casenumber . ' - ' . $headword;
} else {
print $casenumber;
}
?>
The fields caseno_seqno and caseno_year are text fields and appear in the node title. The field caseno_type is a select list with text keys. The selected field value is correctly shown in the node itself, but the value does not show up in the node title.
Does anyone have a clue what I am doing wrong here? I suppose I am doing something wrong in accessing the selected value, but I don't know how to make it work correctly.
Thanks,
Mark
Comments
Comment #1
rick24 commentedI finally got this to work by installing both the Automatic Nodetypes module and the Token module. Then in the CCK data type / Automatic title generation, I set the pattern to "[field_X-raw]", where X is the name of your CCK field. The '-raw' or other flag is evidently required.
If you want to set the title to a selected Taxonomy term, use "[term-raw]". If you have multiple taxonomies, see http://drupal.org/node/210319 .
Comment #2
groenm commentedHi Rick,
Thanks for looking into it. Unfortunately, it is still not working.
I already had the token-module activated. When I use: [field_X], all fields work except the select list (field caseno_type) which outputs an empty string. When I use [field_X-raw] or [field_X-formatted], the result is an empty string for all fields, also the ones that work with [field_X].
I am using Drupal 5.7, CCK 5.x-1.6-1, Automatic Nodetitles 5.x-1.1 and Token 5.x-1.10.
Mark
Comment #3
mooffie commentedYou should use $node, not $form_values. CCK, in the 'validation' phase --which is also the phase in which auto_nodetitle operates-- copies the values from the selectbox to the underlying field. But it does this on this same $node object, not on the global $form_values. Do a
drupal_set_message(print_r($node,1))to see what it contains.You say that tokens don't work. That's bad. It's worth to investigate this.
Comment #4
groenm commentedHi Mooffie,
Thanks for your input. I will give your directions a go in about 2 weeks when I have returned from travelling.
Greetings,
Mark
Comment #5
mooffie commentedIt turns out '$node' is not accessible in auto_nodetitle's PHP pattern. I've just reported this and provided a patch.
You're saying that tokens don't work for you. I've just tried this and they do work for select lists.
However, I did find another user that complained that tokens for select lists don't work for him:
http://drupal.org/node/176491
Could you try disabling all modules you can and see if the problem persists?
Also, could you check the 'weight' of the 'auto_nodetitle' module in your {system} table? It should be '5'.
Comment #6
groenm commentedHi Mooffie,
Thanks for investigating.
Interesting that somebody else experienced this too. Before posting I did some searching on select list, but did not find his post.
I found some time to set up a copy of my drupal database for a test. (Runs off the same server and same code base as my production site.)
I disabled almost all modules. I left these enabled:
Core optional:
CCK:
Other:
Result
The same as before. Token is still not working. The field with the select list stays blank in Automatic Nodetitles. The other fields work without '-raw' or '-formatted', however, with '-raw' or '-formatted', they blank too as before.
As for the weight of Automatic Nodetitles:
Mark
P.S. I might be slow in responding the next one and a half week when I am on the road.
Comment #7
groenm commentedHi all,
Got it working with tokens. I actually used tokens the wrong way. I only replaced "field_caseno_type" in "$node['field_caseno_type'][0]['value']" with "field_caseno_type-raw" instead of replacing the whole string "$node['field_caseno_type'][0]['value']" with "[field_caseno_type-raw]". I myself hadn't used token before.
It is still unclear (at least to me) why the select list was not working, while the ordinary text fields did. I applied Mooffie's patch and tried to do it by using $node, but could not get it to work. Probably, due to my lack of knowledge on the inner workings of Drupal.
As far as my production site is concerned, it looks like my problem is solved. Now, I have to learn a bit more on the workings of $node and $form_value so I can solve my problems myself in the future.
Rick24 and Mooffie: thanks for your help.
Mark
Comment #8
mooffie commentedOh. There's a help text there that shows you the correct syntax.
That's because you accessed the form data (the HTML form) as if it were the node. But in case of a selectbox, the HTML form doesn't reflect exactly the data as it's organized in the node. That's why it's inelegant to access the form.
OK, so I'll close this issue :-)
Comment #9
Anonymous (not verified) commentedAutomatically closed -- issue fixed for two weeks with no activity.