Some background info:
I have a site that uses termpath-raw aliases for some node types. I noticed that sometimes when the assigned terms changed the alias did not update accordingly or reverted to a very old version. Deleting existing aliases and recreating them didn't help either.
The issue lies in pathauto_token_values() function. For a node it attempts to retrieve taxonomy related data using the following query:
$category = db_fetch_object(db_query_range("SELECT t.tid, t.name FROM {term_data} t INNER JOIN {term_node} r ON r.tid = t.tid WHERE t.vid = %d AND r.nid = %d ORDER BY weight", $vid, $object->nid, 0, 1));
This query should account for different versions of the term assignment eg. if my node was tagged with "foo", then "bar" and eventually "baz" in the same vocabulary then this query will return the term based on term weight and not the currently assigned term.
The solution to fix it is actually simple:
$category = db_fetch_object(db_query_range("SELECT t.tid, t.name FROM {term_data} t INNER JOIN {term_node} r ON r.tid = t.tid WHERE t.vid = %d AND r.nid = %d ORDER BY r.vid DESC, weight", $vid, $object->nid, 0, 1));
Fixing this query is only half of the problem - Token module borrowed this functionality in node_token_values().
Instead of
$term = db_fetch_object(db_query_range("SELECT t.tid, t.name FROM {term_data} t INNER JOIN {term_node} r ON r.tid = t.tid WHERE t.vid = %d AND r.nid = %d ORDER BY weight", $vid, $object->nid, 0, 1));
it should be
if (!isset($term->name)) {
$term = db_fetch_object(db_query_range("SELECT t.tid, t.name FROM {term_data} t INNER JOIN {term_node} r ON r.tid = t.tid WHERE t.vid = %d AND r.nid = %d ORDER BY r.vid DESC", $vid, $object->nid, 0, 1));
}
else {
$term = db_fetch_object(db_query_range("SELECT t.tid, t.name FROM {term_data} t INNER JOIN {term_node} r ON r.tid = t.tid WHERE t.vid = %d AND r.nid = %d ORDER BY weight", $vid, $object->nid, 0, 1));
}
Since this issue spans across two different modules I'm not sure how to submit patches. Any suggestions?
Comment | File | Size | Author |
---|---|---|---|
#13 | 423132_pathauto_term_wrong_revisioning_compatible_v2.patch | 1010 bytes | eltrufa |
#11 | 423132_pathauto_term_wrong_revisioning_compatible.patch | 448 bytes | eltrufa |
#3 | 423132_pathauto_term_wrong_revision.patch | 1.27 KB | greggles |
#2 | pathauto.module_423132.patch | 1.21 KB | nonsie |
Comments
Comment #1
gregglesGreat find. Can you just make two issues and submit two patches?
Comment #2
nonsiePatch for Pathauto attached.
Patch for Token can be found here - http://drupal.org/node/423322
Comment #3
gregglesThis makes sense, but perhaps even better would be a change like the attached. Thoughts?
Comment #4
nonsie+1 This seems like a better solution than mine
Comment #5
gregglesApplied to
6.x-1.x: http://drupal.org/cvs?commit=199982
5.x-2.x: http://drupal.org/cvs?commit=199984
6.x-2.x: http://drupal.org/cvs?commit=199992
Comment #6
gregglesComment #8
greggles#485210: SQL Error - Unknown column 'r.vid' in 'where clause' query
I think the patches for 5.x should be reverted. Right?
Comment #9
gregglesFixed - http://drupal.org/cvs?commit=223522
Comment #11
eltrufa CreditAttribution: eltrufa commentedThe current implementation of pathauto_token_values() has a problem when the revisioning module is enabled.
In this case the where clause of the second query should filter by the actual node revision (the one indicated by $object->vid value).
I attached a very simple patch to resolve this issue.
Thanks for this excelent module.
Leandro
Comment #12
gregglesThe revisioning module?
Seems reasonable enough. Anyone else care to check?
Comment #13
eltrufa CreditAttribution: eltrufa commentedYes, the revisioning module.
In my previous patch, I only considered the bulk node alias generation, but I didn’t consider saving a pending revision. In this case, the $object->vid value corresponds to the saved revision, not to the current node revision (indicated by vid column value in the node table). For this reason, a join with node table is required.
The new node alias shouldn’t be generated until the new revision is approved. The alias could be generated using the rules module.
I attached a new version of the patch and also submited the corresponding patch to the token module (#423322: Incorrect retrival of node's taxonomy terms with revisions).
I hope this will be the good one.
Leandro
Comment #14
eft CreditAttribution: eft commentedI had same issue outlined at top of this thread. I already had the patched versions of pathauto so it seems the official patch has not resolved the issue in all cases. I did find, however, that Leandro's patch in comment #13 worked for me (even though I didn't have the revisioning module installed).
Comment #15
tseven CreditAttribution: tseven commentedI experienced the exact same issue with versions enabled. The patch in #13 fixed the issue using the current development version 6.x-1.x-dev.
Comment #16
Dave ReidThe token patch still has a follow-up like this to be committed. Does this still apply or cause problems?
Comment #18
Dave ReidStrike me silly, but shouldn't we be able to just use what's available in $node->taxonomy at that point? I'm trying to write tests to back this up but I think this should work:
We don't have to join on {term_node} or anything else. And this way it's up to taxonomy.module or whatever to make sure the right data is in $node->taxonomy.
EDIT: Nevermind, this failed horribly and only works on node load, not on node save.
Comment #19
Dave ReidComment #20
bgilday CreditAttribution: bgilday commentedsubscribing
Comment #21
marcoBauli CreditAttribution: marcoBauli commentedIn my case the [term-raw] token in the alias gets completely omitted (removed) as soon as a new revision is posted..
is there by chance a more recent patch that applies? thanks
PostScriptum: tried applying manually, but the Token patch related to this issue is in wrong format, so guess it cannot work anyway..
Comment #22
nonsieThis is resolved in 6.x-1.4 release