Taxonomy token not working every time

svihel - February 13, 2009 - 15:55
Project:Automatic Nodetitles
Version:5.x-1.2
Component:Code
Category:bug report
Priority:normal
Assigned:Unassigned
Status:active
Description

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.

#1

revimage - February 16, 2009 - 06:08

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

#2

svihel - February 19, 2009 - 10:59

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".

#3

sapark - February 20, 2009 - 21:11

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);
  }
}
?>

#4

svihel - February 23, 2009 - 17:01

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 :]

#5

sapark - February 23, 2009 - 18:02

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]';
?>

#6

svihel - February 23, 2009 - 21:48

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;
?>

#7

corona ronin - April 11, 2009 - 23:31

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.

#8

corona ronin - April 13, 2009 - 02:37

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!

#9

zedquark - May 12, 2009 - 03:59

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

#10

Summit - May 14, 2009 - 11:27

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

#11

artatac - May 14, 2009 - 12:19

sub

#12

dreeds - June 2, 2009 - 10:13

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

AttachmentSize
rulesettings.png 16.67 KB
settitle.png 6.84 KB

#13

Rosamunda - June 15, 2009 - 19:45

This problem also appears on 6.x version.

#14

Encarte - June 27, 2009 - 20:12

subscribing

#15

jeff124578 - June 29, 2009 - 00:12

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

 
 

Drupal is a registered trademark of Dries Buytaert.