I've been using the newest code for the token module with the autotitle module to create node titles based on taxonomy terms so that I can then easily access the node using the views module. The problem I'm having is that when a user creates a new content page (in my case a content type called selectives) and assigns taxonomy terms to it, the title is not generated when I use the [term] token when you submit the content.

However, when you edit that page again one more time and submit it, it correctly titles the node based on the taxonomy term associated with it. I did some digging around in the database tables and it appears that on the first submit no title is being created. I wonder if this has something to do with the node not have a taxonomy term associated with it until you press submit and than when you click edit again it now recognizes the node taxonomy terms. I've tried it with other tokens like username and node id and they work fine so it must be something unique to way that taxonomy terms are generated and how the token module extracts that information from Drupal's execution path.

Any help in overcoming this problem would be much appreciated. I'm attaching a screen shot of what my problem looks like.

Comments

greggles’s picture

When you say "newest" do you mean 5.x-1.6 (as you selected) or HEAD. I have a feeling that HEAD will fix this.

If not, we'll need more information (like what kind of taxonomy you are using that causes this problem).

Thanks.

J3’s picture

Version: 5.x-1.6 » 5.x-1.7

Yes, the issue occured on version 1.7. I have just tried it.
here are my steps.
1. set auto title as [term] for a node type
2. create a new node, select a term such as "abc"
3. save the node, the title is empty (wrong)
4. edit the node, don't change anything, save it again, the title is "abc" (correct).

greggles’s picture

What kind of taxonomy (taxonomies) are you using?

Single, multiple, required/optional, free tagging? Multiple taxnomies? etc.

Thanks.

J3’s picture

Title: Term Token Does Not Work Until User Edits a content type twice. » Term Token Does Not Work Until User Edits a content type

I tested
1. Single, Multiple select
2. Single, Required
3. Multiple, Required
4. Multiple, Free tagging
and all got the same results.
I think there is no difference for other cases.

csc4’s picture

Same problem.

I have the auto node title set to
TEST [term] [field_reference-formatted] [field_inscription_surnames-formatted]

On creation the title is TEST nnn surname
On edit the title is TEST termname nnn surname

I've tried previewing before saving but that makes no difference. Taxonomy in question is a non heirarchical and non-free tagged.

Taxonomy 5.1
Automatic Nodetitles 5.x-1.0
Token 5.x-1.7

greggles’s picture

Title: Term Token Does Not Work Until User Edits a content type » autonodetitle: Term Token Does Not Work Until User Edits a content type

Just changing the title to reflect that this issue affects a subset of the token consumers.

csc4’s picture

Are you suggesting this is an autonodetitle issue then? I thought it was pretty clearly a token / taxonomy issue which will affect ALL token users relying on taxonomy support.

eaton’s picture

Are you suggesting this is an autonodetitle issue then? I thought it was pretty clearly a token / taxonomy issue which will affect ALL token users relying on taxonomy support.

There's an important addition to that: 'all token users relying on taxonomy support during the window of time after a node has been populated but before it's been saved to the database. The majority of token implementations are NOT doing things during that window, and thus won't run into this issue.

I'm not saying it on't be fixed, but I am also trying to figure out the best approach. Token is relatively lightweight, and it depends on modules giving it data that makes sense. In this case, the data isn't yet in a format that makes sense -- it won't be until the node is saved. The question is whether Token.module should special-case for this scenerio, or whether it should be up to modules to hand Token a *node*, not *an object that will soon become a node*.

csc4’s picture

I think I understand your drift - but why is [term] not iopulated at this point when [field_inscription_surnames-formatted] is?!

It's not as simple as needing to have a heavier module weight than taxonomy is it?

csc4’s picture

Which module should have the heavier weight?

Has anyone else found out anything about why this happens - and more importantly how it can be fixed?

csc4’s picture

Still looking for some help on this - can it be fixed with module weightings and if so which module needs to be first?

If not, is it a bug and if so is there going to be a fixed as currently [term] is unusable - I can't tell my users they have to create a node, save it, edit it, change nothing and save it again!

bomarmonk’s picture

I'm having similar difficulties. Thanks to anyone who can create a fix for this.

Mac Clemmens’s picture

Yes, I'm having a similar issue -- not only with Autotitle but also with Upload paths. When files are uploaded, they are supposed to go into a directory /[term]/ but they don't because token hasn't gotten the term yet. For my programming experience, I realize this is a probably tricky thing to fix. I do believe that token should take the lead on fixing this issue because many modules depend on token in this way.

bomarmonk’s picture

Is there a way to do a cron hook for a temporary fix? Let's say after the node has been saved, the term has been set, and then a cron run can come back and properly populate the titles of the nodes, etc? This idea, however, wouldn't seem to work for a directory path, so a different fix would be needed there... so maybe this needs a more robust solution than a cron hook. Just brainstorming possible fixes...

greggles’s picture

I think the better solution is to improve the taxonomy parsing code in token to handle a more broad set of cases, but that is quite painful and I haven't had the time/desire to do that.

guardian’s picture

subscribing

guardian’s picture

the issue was discussed on the auto_nodetitle module issues queue: http://drupal.org/node/147595

i'm not familiar at all with the token module but maybe a new type could be introduced: $type = 'form' ? that would extract the terms from $node->taxonomy ?

guardian’s picture

for those wanting a workaround: in fact fago documented README.TXT as an answer to http://drupal.org/node/121216

in my very specific content type example, the workaround consisted in using the php replacement feature of auto_nodetitle:

  global $form_values;
  return $form_values[taxonomy]['tags'][2];

now, even at preview time, the title is correct. use the devel module to find out which data to retrieve from $form_values.

bomarmonk’s picture

Thank you! I'll have to give this a try!

stormer’s picture

I've been having the same problem and am massively relieved that there seems to be a work-around. Guardian, are you able to expand on how to implement your solution? Unfortunately I've not been able to figure it out.

many thanks

Ole

summit’s picture

Hi,

Indeed, pPlease explain what to alter.

Thanks in advance,
greetings,
Martijn

ellanylea’s picture

Thanks guardian for the work-around!

Installed the Devel module, which gave 2 more tabs next to the 'View' and 'Edit' tabs. Click on the 'Dev load' tab and see your field.

field_post_date
Array
(
    [0] => Array
        (
            [value] => 2007-03-29T00:00:00
        )
)

In admin/settings/devel, checked the box 'Display form element keys and weights'. Then in the node edit page, I saw (key=mon, weight=0) (key=mday, weight=1) (key=year, weight=2) next to my fields.

So in my content type, in the 'Automatic title generation' box, I wrote:

<?php
  global $form_values;
  return $form_values[field_post_date][0]['value']['mon'] . ' ' . $form_values[field_post_date][0]['value']['mday'] . ', ' . $form_values[field_post_date][0]['value']['year'];
?>

... this gave me 3 29, 2007. But I wanted March 29, 2007. So I changed the code to:

<?php
  global $form_values;
  return date("F", mktime(0, 0, 0, $form_values[field_post_date][0]['value']['mon'], 1, 0)) . ' ' . $form_values[field_post_date][0]['value']['mday'] . ', ' . $form_values[field_post_date][0]['value']['year'];
?>
dmitrig01’s picture

I've reproduced this. here's how

  1. Fresh install with token+auto node title
  2. set up two vocabularies, test and test2. #1 is normal, with terms dog, cat, fish and #2 is freetagging. #2 also has a weight of -9.
  3. edit page content type. enable autonodetitle. pattern is test [term]
  4. Create a node. Fill nothing in but make vocab #1 dog.
  5. Node title is "test ".
  6. Edit the node, and just click submit
  7. Node title is "test dog" (intended).
dmitrig01’s picture

I found the root of this bug.

Auto_nodetitle operates on $op == 'validate'.
Taxonomy operates on $op == 'submit'.

So, there is *no* way we can get preview working, however we can get submit working, and here's how (patch attached).

dmitrig01’s picture

Assigned: Unassigned » dmitrig01
Priority: Normal » Critical
Status: Active » Needs review
greggles’s picture

Title: autonodetitle: Term Token Does Not Work Until User Edits a content type » Term Token Does Not Work Until User Edits a content type
Project: Token » Automatic Nodetitles
Version: 5.x-1.7 » 5.x-1.x-dev

Reassigning issue to proper queue.

fago’s picture

Priority: Critical » Normal
Status: Needs review » Closed (works as designed)

your patch can't help, as on nodeapi insert or update the node has already been saved. Furthermore as this issue only affects terms, it's not critical.

So apart from improving the token parsing code, I see no clean solution for this. Regenerating the node title on op submit would set another title as has been previewed - not very intuitive. Furthermore it might break existing auto nodetitle configurations.

So if you need this, I suggest you make use of of the php code feature and figure out a working php code.

fago’s picture

Status: Closed (works as designed) » Postponed

I'm setting this to postponed to make the issue visible for others. It's still considered to be "by design". :)

greggles’s picture

Project: Automatic Nodetitles » Token
Assigned: dmitrig01 » greggles
Status: Postponed » Needs review
StatusFileSize
new1.93 KB

I mostly agree with fago. This patch doesn't fix this problem, though dmitrig01's advice and debugging are enormously valuable.

I'm not sure that we'll ever be able to "solve" this for freetagging. The problem is that freetags can be new tags in which case they don't have a vid/tid until they are saved but we need them before they are saved. Bummer. If you use freetagging please be sure that the term you want to show up for the autonodetitle is of a lower weight than your freetagging vocab.

For regular vocabs and tags, though, the code can be better, and with this patch I think it is.

I'd appreciate any help folks can give in testing this.

samuelet’s picture

subscribing

greggles’s picture

@samuelet - no need to "subscribe" - there is a patch. Test it. Review it. Let us know how it goes.

samuelet’s picture

supposing this bug is related to my issue at http://drupal.org/node/186506, then the last patch does not work for me on 5.x.1.9. Should it work only for the dev release?

greggles’s picture

It depends on what you mean by "does not work".

If you mean "hunks of it fail the patch isn't even applied" well then yeah, try the dev.

If you mean "the patch applied fine, but the function still isn't there" then I'd say this is a related but slightly different bug ;)

samuelet’s picture

I mean it applies but nothing change in pathauto alias building.
It applies with:
Hunk #2 succeeded at 83 (offset -1 lines).
Hunk #3 succeeded at 95 (offset -1 lines).

bomarmonk’s picture

Greggles, I have tried to patch the token.module with your patch, but Hunk #1, #2, and #3 fail. I have tried this with both the development (5.x-1.x-dev and the official 5). I did notice that CVS shows some last changes for Drupal 6, so maybe this is why this patch no longer applies? Let me know! Thanks.

greggles’s picture

Status: Needs review » Patch (to be ported)

I applied this. Thanks dmitrig01 and fago for your investigation and insight.

@bomarmonk - If you wait for the 5.x-1.x-dev tarball to get rerolled tonight then you can just download that tomorrow to test.

greggles’s picture

Status: Patch (to be ported) » Fixed

Ported in eaton's work this past weekend.

dmitrig01’s picture

Assigned: greggles » dmitrig01
Status: Fixed » Reviewed & tested by the community

I'd recommend we apply my patch. It not only affects terms, it affects anything that gets saved with the node, including the NID!

greggles’s picture

dmitrig01, did you test your patch?

In my testing it doesn't seem to actually do or help anything.

ANT works fine for me now that token has been patched.

greggles’s picture

Status: Reviewed & tested by the community » Fixed

changing back to fixed. I don't think there's anything else to do here.

incaic’s picture

Looking for an answer on this and think this is the right place to ask it.

I've created a file type field so someone can upload a file. I would like
the directory of the uploaded file to be in XXX where XXX is specified
in another form input field.

How can I do this?
I only seem to have access to $user in myModule_token_values ()
How can I get form values?

Seems like something that someone would have done, so as to keep
uploaded files organized.

greggles’s picture

@incaic - take a look at http://drupal.org/project/uploadpath which deals with (and struggles with) the same problem.

Also, this issue is marked "fixed". There are certainly more scenarios that token needs to handle but I'd like to let this issue die and create new, concise, clear issues for related problems.

incaic’s picture

Thanks greggles!

Just read the description, sounds like a winner.

Sorry about posting my problem here.

Anonymous’s picture

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for two weeks with no activity.

dpollen’s picture

Status: Closed (fixed) » Active

Hi... I have been working through a number of related issues related to this.

I have applied both patches...
I have tried the latest DEV versions of the 2 modules as well ( Token & Automatic Nodetitles ).

The node title I am trying to use is:

[term-raw] . [field_job_number-raw] - [field_job_description-raw]

The [term-raw] token is not evaluated on CREATE... but after EDIT.
Could someone please tell me what I'm doing wrong?

greggles’s picture

Status: Active » Closed (fixed)

@dpollen - please create a new issue for your problem. Please provide details about the taxonomy configuration (types, weights) and the taxonomy selection when you create the node.

Also, I'm frankly not likely to work on fixing this issue. It's quite difficult to parse tokens properly and accurately before the node is created which is when ANT needs it to be done. So...

seddonym’s picture

A simple workaround for the [term] tag that I used in Drupal 6. It's more flexible as you can use it for any vocabulary.

Ensure 'Evaluate PHP' is checked and place in the 'Pattern for the title' field:

//Template - replace {Vocabulary id} with the id for the vocabulary you're using:
<?php  return taxonomy_get_term($node->taxonomy[{vocabulary id}])->name; ?>

{Vocabulary id} is where you put the id for the vocabulary you're using, so if I wanted to use the [author-name] token and then the term from Vocabulary 1, I'd use:

[author-name] - <?php  return taxonomy_get_term($node->taxonomy[1])->name; ?>

Simple!

glennr’s picture

This worked perfectly for me too. Thanks for the workaround, seddonym.

jeff124578’s picture

My content type allows multiple taxonomy term selection, so I had to tweak seddonym's code a bit (now that $node->taxonomy[{vocabulary id}] is an array):

<?php  $terms = array_values($node->taxonomy[1]);
       print taxonomy_get_term( $terms[0] )->name;    ?> - [author-name]

Seems to me that we also need to use print instead of return so that subsequent text/tags in the autotitle pattern get included.

laken’s picture

Thanks, this tip solved it for me too - works fine on Drupal 5 as well.

laken’s picture

On second glance (again for Drupal 5), I can say the solution in #49 works when the node is saved from the node edit form, bit it doesn't work when using the 'update automatic node titles' action from the admin/content/node page. This seems to be due to a discrepancy in the makeup the $node->taxonomy element. When the node is being saved from the form, $node->taxonomy is a simple array with the first key the vid, and the second key the tid, so the code above works.

Inspecting $node->taxonomy while the form is being submitted yields:

Array
(
    [2] => Array     (this is the vid)
        (
            [6] => 6   (this is the tid)
        )

)

But when then node is already saved, and retrieved via node_load(), $node->taxonomy has a different form:

Array
(
    [6] => stdClass Object
        (
            [tid] => 6
            [vid] => 2
            [name] => Fairness
            [description] => 
            [weight] => -7
        )

)

We need to take this into account when making an auto node title template that will work both when the node form is submitted and for the 'update automatic nodetitle' action from the Content administration page (admin/content/node)

Here's what worked for me:

    $tax = $node->taxonomy[{vocab id}];
    if (!empty($tax) && is_array($tax)) {  // if it's an array and not an object, we're saving from the node form
      $terms = array_values($tax);  // needed b/c my vocab allows multiple terms
      $tid = $terms[0];
      $term = taxonomy_get_term($tid)->name;
    }
    else {   // empty or not an array, we're updating from /admin/content/node
     $tax = array_values($node->taxonomy);
     $term = $tax[0]->name;
    }
kompressaur’s picture

Hello i am trying this and the title is being generated as-

//Template - replace {Vocabulary id} with the id for the vocabulary you're using:

I am on the latest drupal 6. Am i doing something wrong? thanks

big_smile’s picture

I am using Drupal 6.
What code do you need to use if you want to reference terms from several different vocabularies?
The code seddonym posted only seems to let you use one vocabulary.

Thanks for any help!

jeffschuler’s picture

@kompressaur: just leave that line out, entirely. It's a php comment, but it's not inside the <?php ?> block.

Comments in #47 and #49 provided a suitable workaround for me.

Note that #338853: Token malfunction with Auto NodeTitle: [term] replacement fix patch and #476644: Does not see term-raw first time may be duplicates of this issue.

davideads’s picture

#49 worked for me. printing the string in the PHP snippet is critical if you wish to mix-n-match tokens and PHP code in any order.

Rob T’s picture

A combination of #47 and #49 worked for terms for me.

Stomper’s picture

I am having similar issues as I have "[nid]" in my autonode title, I too have edit and save to have [nid] show up, oddly enough, I also have a "node field" as part of the title - it is generated on submit.

Can a chron run be set to update them?

wind_kind’s picture

The code at #49 works for me but how would I go about selecting the term and its child. My vocabulary looks like this:

Vehicle make
-Alfa
--156
--157
-Aston Martin
--DB5

and I want to get only for example Alfa and 156. With the code from #49 I get only Alfa.

wind_kind’s picture

I don't know if this is the best way of doing it but I played around with that piece of code until I came up with this and it works, if you can make this any better pleace do, I have no clue about PHP I just got lucky.

<?php  $terms = array_values($node->taxonomy[1]);
       print taxonomy_get_term( $terms[0] )->name;
       print taxonomy_get_term( $terms[1] )->name;    
?>
aubjr_drupal’s picture

Go to my summary on available hacks/fixes as of this posting for this problem. I put it together after coming across a lot of posts on this issue and wanted to summarize it for others.