It is very nice that the new Token Starter submodule makes it easier to add your own tokens.
To synchronize the URL's of the original node and its translations for a multilingual site, I'm trying to add a [tnid] token (tnid is the original node's nid for all translated nodes) and use it in pathauto (e.g. by setting URL's to 'languageprefix/contenttype/[tnid]').

I'm adding to the tokenSTARTER_token_list function the following string:

// Node tokens here.
$tokens['node']['tnid'] = t("Common nid for a set of translations.");

and another string to the tokenSTARTER_token_values function:

// Node tokens here.
$values['tnid'] = $node->tnid;

Now the new [tnid] token is visible in the list, but it does not seem to get the value.
Am I doing anything wrong here? 'tnid' values are kept in the 'node' table; should I use a more complicated expression to get the values from there?
Thank you!

Comments

dawehner’s picture

I think the problem is that if you don't have created a second transaltion $node->tnid is empty.
So i think

<?php
$values['tnid'] = $node->tnid ? $node->tnid : $node->nid;
?>
GN’s picture

Thank you for the suggestion Daniel, but it does not work.
It occurred to me, too, that tnid may be not available yet when you are creating a new translation, but this token does not work even when you save the new node and then reopen it for editing.

terry22’s picture

Hi,
did you manage to create a tnid token?
I need the same thing.

Thank you

jay_N’s picture

Right, I think I know what's gone wrong. See the function definition:

function tokenSTARTER_token_values($type, $object = NULL) {

This means that the $object variable will hold the values for the $node object (i.e. the node object is passed into the function as the second argument, which inside the function you refer to as $object).

Therefore your code should be:

$values['tnid'] = $object->tnid;

GN’s picture

Thanks a lot Jeroen!
It seems to work – but, as I actually expected, it requires reopening and resaving nodes after translation:
when you create an original node and save it, first you get [tnid]=0; then, if you open it and save again, you get [tnid]=nid; and when you create a translation and save it for the first time, [tnid]=0 – you need to open and save it again to get [tnid]=tnid.

dawehner’s picture

@GN
Thats why is suggested


$values['tnid'] = $object->tnid ? $object->tnid : $object->nid;
GN’s picture

Thank you Daniel, it's much better. Now [tnid] is first set to nid instead of 0, so that we need to reopen and resave only the translation to get [tnid]=tnid instead of the second nid.

knowledges33ker’s picture

Issue tags: +subscribe

Is there a way to do something similar to this, but to use token to set the user (in the Rules module) to the node author? I've read some posts that suggest this is possible, but I can't figure out how to do it.

Many thanks for any pointers or suggestions.

Dave Reid’s picture

Status: Active » Closed (duplicate)

Marking this as a duplicate of #736178: Add a [node:source] token for source node of a translated node since there was more work done recently there.