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:

  1. $source_node is correct and is the English version
  2. $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.

Issue fork token-3298787

Command icon 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:

Comments

leolandotan created an issue. See original summary.

leolandotan’s picture

Status: Active » Needs review
StatusFileSize
new923 bytes

I created a patch with the changes mention in the issue description.

Hope everything is in order.

Thank you very much!

anoopsingh92’s picture

Assigned: leolandotan » anoopsingh92

Hi @leolandotan, Thanks for the patch I am reviewing this.

anoopsingh92’s picture

Hi @leolandotan, Your patch has been applied successfully and it looks good to me.

Lenovo@LAPTOP-PDE747K8 MINGW64 /c/xampp/htdocs/drupal-9/web/modules/contrib/token-3298787 (3298787-nodesourceurl-is-not)
$ git apply -v token-translation_source_node_url-2729727-2.patch
Checking patch token.tokens.inc...
warning: token.tokens.inc has type 100644, expected 100755
Applied patch token.tokens.inc cleanly.

Thanks

anoopsingh92’s picture

Assigned: anoopsingh92 » Unassigned
Status: Needs review » Reviewed & tested by the community

I have created an MR for this. Moving it to RTBC. Thanks.

berdir’s picture

Status: Reviewed & tested by the community » Needs work

This 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.