I'm using token [term] to generate node title ([term-raw] get same error).

When I create node, select taxonomy term and post the node it's shown with empty node title. When I edit this node and post it second time (without changing anything - just reporting) it will be generated correctly. Just the first time I create any node of this type it will be without a title.

It's not to big deal, but nodes without titles could behave quite oddly with some modules, like for example pathauto.

Files: 
CommentFileSizeAuthor
#12 rulesettings.png16.67 KBdreeds
#12 settitle.png6.84 KBdreeds

Comments

I am having a similar problem... just not with a taxonomy token...

With autonode title enabled and set to auto generate the title using a token....

upon posting the page... there is initially no title... if i choose to edit, make no changes and submit... the title shows correctly... the token is set to use the entry from menu title... [menu-link-title]

NOW...
Within the content type i am using tokens in two places to automatically fill data into fields...
For the second token i have tried using same value [menu-link-title] which for that entry worked fine, and also tried using an alternate [menu-link-title-raw] to see if it was causing a conflict with the first one being used by autonode title...

Either results in the title initially not getting set and ending up blank.

Any ideas here?
Hope this all was clear

Best,
Mike

I think the problem is (at least at my case) that taxonomy term is sent only after the title. I thought that it could maybe be fixed by putting taxonomy term on first place in CCK fields in node type setting but in D5 this just isn't possible.

Another observation: when I have already created node with say term "abcd", posted two times, so the title really is "abcd" and then I change the term to "xyz" and click send the title will still be "abcd" after first posting and only after reposting will change to "xyz".

I had the same problem with Drupal 6. My solution was to use Custom Tokens, create a custom token there with the following code to get the term from the vocabulary I wanted (vocab 1 and assumes there is only one term from this vocabulary on this node):

<?php
$terms
= taxonomy_node_get_terms_by_vocabulary($node, 1);
foreach (
$terms as $term) {
  return (
$term->name);
}
?>

then in auto nodetitle use my new custom token, and finally add || $op == 'load' to function auto_nodetitle_nodeapi in auto_nodetitle.module:

<?php
/**
* Implementation of hook_nodeapi().
*/
function auto_nodetitle_nodeapi($node, $op) {
  if ((
$op == 'presave' || $op == 'load') && auto_nodetitle_is_needed($node)) {
   
auto_nodetitle_set_title($node);
  }
}
?>

Thanks for answer! I tested it and it works. But if you make that change in .module code, the module won't be functional for other tokens unfortunately.

BTW is the Custom Tokens module necessary? Why not just use this php code in "Pattern for the title" and check "Evaluate PHP in pattern"? Don't get me wrong, I'm just asking that out of curiosity, don't really understand that code very much :]

uh, guess it's not necessary :P
Here's the code I kludged together (thanks for the tip, I can disable that module now!):

<?php
   $terms
= taxonomy_node_get_terms_by_vocabulary($node, 1);
   foreach (
$terms as $term) {
    
$titleterm = $term->name;
   }
  
$terms = taxonomy_node_get_terms_by_vocabulary($node, 2);
   foreach (
$terms as $term) {
    
$agencyterm = $term->name;
   }
   return
$titleterm . ', ' . $agencyterm . ', ' . '[field_date-view]';
?>

Actually I did some more research after posting my previous comment and found out that i missed this topic - http://drupal.org/node/137669#comment-543797

I added following code in my autonode field and to my surprise it worked (for 5.x):

<?php
 
global $form_values;
  return
$form_values[taxonomy]['tags'][2];
?>

EDIT:
previous code only works for vocabularies with freetaging enabled. If you don't want to use freetagging it should look like this:

<?php
 
global $form_values;
 
$tid = $form_values['taxonomy'][3];
 
$term = taxonomy_get_term($tid);
  print
$term->name;
?>

So if you want to use both:

<?php
 
global $form_values;
 
$firstterm = $form_values[taxonomy]['tags'][2];
 
$tid = $form_values['taxonomy'][3];
 
$term = taxonomy_get_term($tid);
  return
$firstterm." :: ".$term->name;
?>

Moreover, I'm curious as to why I can't get the term name from the php code because it says that $node is available.

In the body of the node, this php code returns the taxonomy term name:

<?php
foreach($node->taxonomy as $tid=>$term) {
print
$term->name;
}
?>

If $node is avaliable, why wouldn't it automatically print on the title. I'm finding it quite impossible to get the current taxonomy term into the title of the node.

Any thoughts?

Thanks.

Hey, I've been through multiple threads in the forums, and I don't think I've found a straight solution to this problem. As of my best efforts, none of the solutions provided here allowed me to have the current taxonomy term populate in the page title.

This issue is also touched on here: http://drupal.org/node/137669#comment-543797

It seems that because auto_nodetitles uses $op == 'validate', its problematic to isolate the taxonomy term which I guess is placed after $op == 'submit'. I'm struggling to find a solution to this problem.

Has anyone sucessfully found a way to get the taxonomy term of the current node to submit in the node title? Please tell me if you have. I'm using 5x and am very curious to find a solution.

Thanks!

I wanted to create a multiple taxonomy named title appended with a custom cck alternate title (textfield) and it worked out fine.

I have a fresh D5 installation: cck, contemplate, automatic nodetitles, pathauto, and tokens.

    Three of the vocabularies heirachy are disabled, and 'Free Tagging' is selected.
    One vocabulary heirachy is disabled, 'Free Tagging' is not selected.
    CCK text field, used as an alternate title.

When editing the content type, I checked off "Automatically generate the title and hide the title field".
Then I created a CCK textfield and named it 'inv_title' (alternate title name).
Using the token provided by automatic nodetitles for my CCK textfield, along with accessing the vocabulary ID in $form_values[taxonomy]['tags'][DESIRED-VOCAB-ID], resulted in the following title:

    "taxo-make taxo-model taxo-year taxo-color - alternate title"
    VW JETTA 2006 Blue - Check this car out!

I added an alternate title field because when there are multiple titles with the same name, especially using term names, pathauto appends the title url with -0, -1, -2, etc etc.

<?php
global $form_values;
 
$make = $form_values[taxonomy]['tags'][2];
 
$model = $form_values[taxonomy]['tags'][1];
 
$year = $form_values[taxonomy]['tags'][4];
 
$tid = $form_values['taxonomy'][3];
 
$color = taxonomy_get_term($tid);
return
$make.' '.$model.' '.$year.' '.$color->name.' - [field_inv_title-raw]';
?>

Thanks to (http://drupal.org/comment/reply/373978/1468260#comment-1282938) svihel comment above

Subscribing, is this also working on D6?
greetings, Martijn

sub

StatusFileSize
new6.84 KB
new16.67 KB

This was frustrating me too. I've got it working in 6.9 with Rules and Tokens.

Here's how I did it, let me know if it works for you (I'm still learning Drupal, so if you see a way to improve or want to correct me or add better details, please do):

- enable Tokens and Rules modules
- create a new Rule (eg. "Fix Title")
- set the event to be "After saving new content"
- tick the "this rule is active" checkbox
- save the new rule
- on the resulting page add a condition, such as which content type this rule applies to
- add an action "set created content's title"
- use the replacement term "[node:term]" (made available by the token module)
- save and test

Caveats:
This sets the deepest term as the title - if you need something else, I think all the taxonomy-related tokens should work. My guess is this works because the node has already been saved and therefore the taxonomy term(s) have been set. The trouble with trying to use taxonomy tokens right in automatic nodetitles is that the tax. terms haven't been set by the time autonodetitles tries to access them.

I've attached a few screengrabs to help explain. Let me know if this works for you, or if I can add anything else.

Cheers,
Dean

This problem also appears on 6.x version.

subscribing

There's a workaround (also using a PHP pattern) that worked for me on Drupal 6.12 at http://drupal.org/node/137669#comment-1142596

I used dreeds suggestion as I couldn't get any of the PHP patterns to work for me

subscribing

Subscribing

Version:5.x-1.2» 6.x-1.2

Tried both this and the Module_Order solution found at http://drupal.org/node/476644#comment-3392994 just now.
Rules seems to, well, rule. :D

Kudos to 'dreeds' in #12. Thanks for posting the rules idea. It's working great for me.

Thanks for #12. Definitely helped me.

#12 worked! thanks.

#12 worked for me, thanks

[EDIT]

I had some problem with that solution, I think if user does set the title, it will overwrite it?

Thanks to dreeds and his #12 answer,

It works for me and D6.14 and add_nodetitle 6.x-1.2 It's still a workaround that work. Dreeds has a good feeling.

Either terms seems to be "attached" to a node after the node is saved. (suggested by this old page http://www.unibia.com/unibianet/drupal/how-create-drupal-nodes-script)

Or the presave isn't fired up in the taxonomy module (suggested by this comment http://drupal.org/node/743920#comment-2782572)

I'm not sure either.

To fix auto title even for taxonomy the auto title module has to be called after token module. In my case token had weight 10 and auto title had 1. So I changed the weight to 11 in the systems table.

In the module you could use code from this example to be used when installing the module:

<?php
function auto_nodetitle_install() {
 
$reference = "token";
 
$my_module = "auto_nodetitle";
 
$differing_weight = 1;
 
$weight = (int) db_result(db_query("SELECT weight FROM {system} WHERE name = '%s'", $reference));
 
db_query("UPDATE {system} SET weight = %d WHERE name = '%s'", $weight + $differing_weight, $my_module);
 
drupal_set_message(t('Has set module weight on %my_module to plus %differing_weight from %reference (%weight) to be sure to override %reference functions', array('%weight' => $weight, '%reference' => $reference, '%my_module' => $my_module, '%differing_weight' => $differing_weight)), 'status');
}
function
auto_nodetitle_uninstall() {
 
$my_module = "auto_nodetitle";
 
drupal_set_message('Uninstalled '.$my_module, 'status');
}
?>

Sub

#12 works like a charm, thanks!
But don't forget to create a rule also for the "After updating existing content" event!

suscribing

I had a similar problem with tokens not being accessible to Automatic Nodetitles when updating a node. For me, it wasn't taxonomy terms, but CCK text fields (both in a Content Profile node and a normal node). I managed to make a generic workaround that still maintains the settings from Automatic Nodetitles using Rules (similar to #12):

- ON event After updating existing content
- DO Execute custom PHP code:

<?php
/** DON'T INCLUDE PHP OPENING AND CLOSING TAGS IN ACTUAL RULE **/
// Check that the node is set up to use Automatic Nodetitles
if(auto_nodetitle_is_needed($node)){
 
// Now the node has been updated, set the title again
 
auto_nodetitle_set_title($node);
 
// Save the changes
 
return array("node" => $node);
}
?>

That seems to do the trick nicely for me and means I don't have to fiddle about copying the settings from Automatic Nodetitles into any Rules if I ever change them. I haven't tested it out for taxonomy term tokens, but it might work.

I didn't have an issue when creating new content (the tokens worked for that), but the same code could be used with the 'After saving new content' event in Rules.

EDIT: I accidentally left out a check in the initial code to see whether the node is set up to use Automatic Nodetitles, which meant that nodes with automatic titles set to 'disabled' were getting overridden by the php code above. I've updated the code and it all works fine now isn't working properly. It seems I need to work on this a little more. I'll post again soon.

It seems the problem lay in the $node->auto_nodetitle_applied flag inside auto_nodetitle_is_needed($node), so I just copied the function into my Rule and left that check out. The tested and working code is now:

<?php
if (($setting = auto_nodetitle_get_setting($node->type)) && !($setting == AUTO_NODETITLE_OPTIONAL && !empty($node->title))){
 
auto_nodetitle_set_title($node);
  return array(
"node" => $node);
}
?>

I did try and put this in a hook_nodeapi on an update operation, but it didn't seem to work. Perhaps it has something to do with my module's weight compared to Rules, but I'm happy enough to stick with the Rules workaround for now.

#42 works for me. I'm using Auto_Nodetitle along with Node Repeat to create multiple nodes from a repeating date. The title is created from PHP code executed by auto_nodetitle which adds the CCK date value for that node. After adding the code from #42 into a rule action (minus the <?php ?> tags) the date value was the correct value and not a repeat of the initial, starter node. #37 did not work for me but I might have changed the value differently or that solution was used in an older version of this module. Thanks LGLC I'll post back if I run into any glitches. Oh I am also using the latest dev release of Auto node title.

After reading several issue posts (and the comments) from three different modules (node clone, token, and node autotitle), I just wanted to put up a summary of what solutions are out there so others wouldn't have to search all over looking for a solution.

As of today, it's still not fixed, and based on the fact that this issue was initially discovered and posted about here at least five YEARS ago. According to http://drupal.org/node/338853, there has been at least one attempt to fix the problem, but it didn't work.

The problem: As described at http://drupal.org/node/137669#comment-1812364, the $node object does not have the [term] value when node_autotitle tries to access $node to get the taxonomy term text. Apparently, node_autotitle looks in $node->taxonomy at the nodeapi 'presave' stage of processing (line 65 of auto_nodetitle.module in v. 6.x-1.2), and all that is there is the vocabulary id (vid) => term id (tid), as array elements. Later in processing, $node->taxonomy has been turned into an object with the proper term_id, as needed by the autotitle module.

The real problem may be in both token as well as auto_nodetitle because it IS getting a [term] value on each save somehow, despite the value not being in $node, but it's the term's value from the LAST time I saved the node. In other words, the title is one "edit and save" iteration behind. If I create a node with term "Alpha" selected to fill [term] for an autotitle, it's blank after clicking Save and viewing the node. If I edit the node and change the term to "Beta" and save, I get "Alpha" in the title - the initial value. If I AGAIN edit it and save it, without changing it (leaving it set to "Beta"), the title is THEN saved as Beta. So the token module is getting the term_data table for that value.

Solutions: There are two (2) feasible workarounds.

Workaround #1 - Use the Rules module, per this thread's comment #12 and others (#41, #42, etc.). Remember to create two rules - one for creation of a node and one for updating a node. I didn't use this method because I wanted to avoid installing another module for a problem that's totally unrelated to it.

Workaround #2 - Do some custom PHP in the content type itself. Go to the content type and edit it. Put your custom code (including open/close php tags) in the "Pattern for the title:" textarea for the content type in the Automatic title generation field, and check the "Evaluate PHP in pattern" box. There are many different iterations of the code other people have posted here and there, but basically it needs to query the DB with taxonomy_get_term to get the updated term text which isn't in $node. Here's my hack job (sans the php tags):

$aa = (array) $node->taxonomy; // get array
reset($aa); // reset to 1st array element
$term1 = taxonomy_get_term(current($aa)); // get term based on $aa, returns object
return $term1->name;

Or from this comment (same link as above)

$tax = $node->taxonomy[{desired vocab id}];
$terms = array_values($tax);  // needed b/c my vocab allows multiple terms
$tid = $terms[0];
$term = taxonomy_get_term($tid)->name;

That solution required pre-choosing the vocabulary id rather than pulling in the 1st term in $node->taxonomy, but I wanted this to dynamically pick the 1st term. (If you wanted the last term, I suppose you could use end() instead of reset() in my code).

Or try this one (requires only having one term... could be modified for more than one term with some basic concantenation and a return statement? Didn't try it).

foreach($node->taxonomy as $tid=>$term) {
print $term->name;
}

Anyways, these two types of fixes are the only ones out there, as of today. Pick one and save your time instead of researching this more. :)

Note: The fix of re-ordering the execution of the modules by altering their respective weights in the system table (putting the auto_nodetitle after token) didn't work for me.

Thank you so much for this useful summary of your research. I have the same problem since I'm generating node titles using taxonomy terms amongst other cck fields.

Thanks to dreeds, your method is working,..BUT once the node is updated, title change to something like this: [Content-type -name]/[node id]!
I cant understand why.. should I disable automatic nodetitle to get your method working after node update?

If I want to have the field "[field_profile_address-raw]" as a tittle, how can I do that with php code? I think I need the vid, tid.. but I don't know how to get it.

If I put the [field_profile_address-raw] directly as a pattern it only have effect when I update the profile. Thx.