I am working to create a token that grabs the top taxonomy term synonym for a node to be used with Pathauto. I have tried using Custom Tokens (the module) but it only works when I do the Update Path Alias action from the content list. It does not work at the time the node is created. Is this because I don't have access to $node->taxonomy until after the node is saved?
Here's the code I have so far in my token starter module:
<?php
/**
* Implementation of hook_token_list().
*/
function token_synonym_token_list($type = 'all') {
$tokens = array();
if ($type == 'node' || $type == 'all') {
$tokens['node']['term-synonym'] = t("The top taxonomy term synonym");
}
return $tokens;
}
/**
* Implementation of hook_token_values().
*/
function token_synonym_token_values($type, $object = NULL) {
$values = array();
switch ($type) {
case 'node':
$node = $object;
foreach($node->taxonomy as $term){$term_array = taxonomy_get_synonyms($term->tid); }
$values['term-synonym'] = $term_array[0];
break;
}
return $values;
}
Comments
Comment #1
interestingaftermath CreditAttribution: interestingaftermath commentedOops, posted the code without the ending php tags
Comment #2
interestingaftermath CreditAttribution: interestingaftermath commentedNow I'm really lost. I figured I was doing something wrong in my code (even though it worked correctly when you'd update the path) so I copied the code from the token_node.inc file.
This does not work when the node is created or when the path is updated from the content list:
Comment #3
interestingaftermath CreditAttribution: interestingaftermath commentedAny ideas?
Comment #4
interestingaftermath CreditAttribution: interestingaftermath commentedI figured it out. Here my code was right but I was clearing it out with this
Since I wasn't setting "term" in my custom module, it was just clearing my new token. Oops! Here's the final code.
Comment #6
joachim CreditAttribution: joachim commentedinterestingaftermath -- not entirely sure what your code is doing.
I've achieved this with the following pretty simple patch. Works great for URL aliases.
Comment #8
interestingaftermath CreditAttribution: interestingaftermath commentedI don't doubt that the code I wrote is more than it needs to be but that's primarily because I do not know how to write a patch and didn't want to edit the module itself. I had to pull all the code from the taxonomy tokens function and recreate it in my own module.
I would much rather see this added as a patch like you're attempting.
Comment #9
joachim CreditAttribution: joachim commentedAh, that part of the module has changed a bit in dev.
Here is a reroll against git rather than the latest release.
Comment #11
joachim CreditAttribution: joachim commented*sigh*
I shouldn't complain -- changing $category to $term is a good fix :)
Comment #13
joachim CreditAttribution: joachim commentedAh. Yes.
This should pass tests, though it does raise the question of what the token should contain when there is no synonym. Any thoughts?
Comment #15
joachim CreditAttribution: joachim commentedO_o
I'm giving up on this for now at least...
Comment #16
Dave ReidMissing a { after the if statement. That's what's causing the fatal errors.
Powered by Dreditor.
Comment #18
interestingaftermath CreditAttribution: interestingaftermath commentedWhat happens when you use a token that doesn't have a value for the more "core" tokens? It could mimic that which I imagine would just be the print [token] or whatever. I know in my case, if I am using that token it's because every term possible for that node has a synonym.
Comment #19
interestingaftermath CreditAttribution: interestingaftermath commentedRe-uploading jaochim's patch with the syntax fix that Dave Reid noticed.
Comment #20
interestingaftermath CreditAttribution: interestingaftermath commentedOops. This should work
[edit] someone want to educate me on how to get the patch to test? I'm a patching noob.
Comment #21
joachim CreditAttribution: joachim commentedJust set the status to 'needs review'.
And thanks! I was getting really fed up of this patch, for all its 4 lines!!!
Comment #22
interestingaftermath CreditAttribution: interestingaftermath commentedNo, thank you for taking a look at this issue. Although my code works it's quite excessive. Going to upgrade to the latest dev now with this new patch. Thanks again
Comment #23
bmx269 CreditAttribution: bmx269 commentedAdded the patch to the node tokens as well against HEAD version 6.x-1.x-dev in git as of today.
Comment #24
bmx269 CreditAttribution: bmx269 commentedNot sure why I can not run the tests on my patch above, but please test. Thanks, I also took the previous patch and added that to it.
Comment #25
bmx269 CreditAttribution: bmx269 commentedFixed my patch above and resubmitted the revision here. This adds the synonym patch to the node path as well as the taxonomy.
Comment #26
bmx269 CreditAttribution: bmx269 commentedThis is a great patch. Thaks to all who contributed.
Comment #27
BarwonHack CreditAttribution: BarwonHack commentedProbably a great addition for Drupal Local SEO.
Can't wait until this is released - hint hint :D
Comment #28
Hylite CreditAttribution: Hylite commentedThe patch works fabulously. However I was wondering how would I go about simply having it not show "[cat-synonym]" if no synonym was entered for a specific term?
Comment #29
bmx269 CreditAttribution: bmx269 commentedYou are thinking a fallback right?
Comment #30
Hylite CreditAttribution: Hylite commentedCorrect, currently it displays [content-synonym], id imagine if it just omitted that token if there is no synonym specified that it would solve the issue.
Comment #31
jcamfield CreditAttribution: jcamfield commentedIs this patch or functionality available for 7.x?
Update: The patch in #18 here works for this in D7: http://drupal.org/node/741914