I just can't find the right magic to set my node-title with php-code using the data in the other cck-fields of my node.
Could you please post a simple sample, I need something really urgent.

Thank you.

Comments

fago’s picture

I've never tried getting the node data with some php code.
Related issue: http://drupal.org/node/121216

However, I think you could get the global $form_values, which should do it in most cases. However, these are "raw form data" and not processed in any way.

I'll suggest you use the token module instead.. For sure it's the easier way.

ray007’s picture

Using the token-module is definitely easier for the simple things you can do with it.
But, if I want different title-setting depending on some input-values, I need to use php-control structures ("if" and/or "switch" statement), and so far I haven't found a way to combine those with tokens ...

fago’s picture

hm, yes that would be a nice feature. However I see currently no way for it, for this I would need a not existing option for the token module, which allows to replace the values without any tags..
So for now, that's not possible.

So yes, you would need php..
try something like that: (untested)

global $form_values;
$node = (array)$form_values;
ray007’s picture

Thank a lot, that got me almost to where I want to be.

Question: do you invoke the php-code at a different time than the token replacement?
The reason I ask is that content_format(...) on a date field brings me back to the old problem of returning "January 1. 2007" while that's already working now when using the token module ...

Is it possible to call this php-code in a context with variables according to the possible cck-fields in this node-type available?

fago’s picture

no, it gets called exactly at the same time. First the tokens are replaced then the php code is evaled. Have a look at _auto_nodetitle_patternprocessor() if you are interested.

It would be great if you could share a working example with us!

ray007’s picture

I still think a context providing the variables would make life easier, but I got it working now:

<?php
global $form_values;
$node = (array)$form_values;
$gegner = content_format('field_gegner',
                         $node['field_gegner'][0]);
$heim = $node['field_heimspiel']['keys'];
if($heim) {
  print "MyTeam - $gegner";
} else {
  print "$gegner - MyTeam";
}
?> am [field_datum_0-view]

Depending on the value of field_heimspiel (integer - checkbox), the home-team or the visitor-team is printed first in the title, followed by the date (field_datum_0) the match is to be played on.

Instead of evaluating 'field_gegner' myself I could also have changed context to html and printed the token:

... ?>[field_gegner-formatted]<?php ...

You only have to take care the place of the token could be replaced by a php print-statement.

Getting the right incantation $node['field_heimspiel']['keys']; took me some print_r(...) debugging since the devel-module shows different properties for then node there, but otherwise it's quite straight-forward since you can use the tokens outside of php-context.

moshe weitzman’s picture

Title: sample for using php code » sample for using php code (wrong order token replace)
Category: support » bug

fago said: "First the tokens are replaced then the php code is evaled"

thats what i expected, but the code does it backwards. see _auto_nodetitle_patternprocessor()

fago’s picture

Title: sample for using php code (wrong order token replace) » sample for using php code
Category: bug » support
Priority: Critical » Normal

ah, damn of course, I've changed that some time ago, sry. (commit )

So now: php is evaled, then tokens are replaced.
so there is still no bug in the module..

fago’s picture

Status: Active » Fixed

I've just changed the execution order again: Now first tokens are replaced then php is evaluated, and then tags are stripped. This enables one to combine token replacement with php evaluation.

I've put an example for this into the README and also I've added a note how to get $node...

Anonymous’s picture

Status: Fixed » Closed (fixed)