The [node:source:url] is not generating the correct node URL.
I have two languages enabled: English and Spanish then a node with the two translations. I'm using the Metatag module as well with this. So when I visit the Spanish translation of the article, the token outputs the Spanish URL.
I checked this part of the code in token.tokens.inc:
// Chained token relationships.
if (($parent_tokens = \Drupal::token()->findWithPrefix($tokens, 'source')) && $source_node = $node->getUntranslated()) {
$replacements += \Drupal::token()->generate('node', $parent_tokens, ['node' => $source_node], $options, $bubbleable_metadata);
}
So I tried to look into the code's values and noticed that:
- $source_node is correct and is the English version
- $options specifically $options['langcode'] is set to 'es'
So what I did was this:
// Chained token relationships.
if (($parent_tokens = \Drupal::token()->findWithPrefix($tokens, 'source')) && $source_node = $node->getUntranslated()) {
// Update the langcode value to the source node's langcode.
$options['langcode'] = $source_node->language()->getId();
$replacements += \Drupal::token()->generate('node', $parent_tokens, ['node' => $source_node], $options, $bubbleable_metadata);
}
With this, the token now shows the correct source node URL.
Additional testing info
I tried without this new line update, if we use `[node:source:title]`, it returns the correct value from the source because the value is taken from the passed node value from token.tokens.inc into node.tokens.inc (`$node = $data['node'];` then `$replacements[$original] = $node->getTitle();`. While in node.tokens.inc URL, the code is like this `$replacements[$original] = $node->toUrl('canonical', $url_options)->toString();` so when $url_options is passed from token.tokens.inc with "es" in it, it is then generating a wrong value which is from the ES language.
| Comment | File | Size | Author |
|---|---|---|---|
| #2 | token-translation_source_node_url-2729727-2.patch | 923 bytes | leolandotan |
Issue fork token-3298787
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
- 3298787-nodesourceurl-is-not
changes, plain diff MR !28
Comments
Comment #2
leolandotan commentedI created a patch with the changes mention in the issue description.
Hope everything is in order.
Thank you very much!
Comment #3
anoopsingh92Hi @leolandotan, Thanks for the patch I am reviewing this.
Comment #4
anoopsingh92Hi @leolandotan, Your patch has been applied successfully and it looks good to me.
Thanks
Comment #6
anoopsingh92I have created an MR for this. Moving it to RTBC. Thanks.
Comment #7
berdirThis should not change the current $options as that might also affect other tokens being replaced. Instead, copy it or merge the options inline.
Also, a test would be useful, for example by extending Drupal\Tests\token\Kernel\NodeTest::testNodeTokens. Note that it tests source already, but without actually having translations, that likely doesn't do much, so to reproduce this problem, a translation of $node needs to be added and saved and possibly multilingual configuration is necessary too.