Token 8.x-1.6

Using Twig Tweak (8.x.2-4) I put the following at the top of my page.html.twig template:

<div class="alert alert-primary">
	{{ drupal_token('node:title', {node: node}) }}<br />
	{{ drupal_token('node:menu-link:parents', {node: node}) }}<hr />
	{{ drupal_token('node:source:title', {node: node}) }}<br />
	{{ drupal_token('node:source:menu-link:parents', {node: node}) }}
</div>

I have the following English Menu:
-Home
-What
--About

On the "About" page, in English, I therefore see "About", "What", "About", and "What"

The menu also exists in French:
-Home
-Quoi
--Nous

On the "Nous" page (the translation of "About"), I would expect to see "Nous", "Quoi", "About", "What".
But instead I see "Nous", "Quoi", "About", "Quoi", with the value of [node:source:menu-link:parents] being incorrect

This would suggest that the token path for [node:source:menu-links] is not looking at the source translation, and is instead giving the same output as [node:menu-links] with the current language menu links provided.

Is there an alternative token that can be used, or does this require a patch?

CommentFileSizeAuthor
#5 3105028_defaultLanguage_menu_links.patch958 bytespgrandeg

Issue fork token-3105028

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:

  • 8.x-1.x Comparechanges, plain diff MR !14
  • token-3105028 Comparecompare

Comments

philkay created an issue. See original summary.

philkay’s picture

As we are looking to have the same URLs for English and French pages (/en/what/about and /fr/what/about), I tried looking at [node:source:url:path].

[node:source:url:absolute]
EN: http://localhost/drupal/en/what/about
FR: http://localhost/drupal/en/what/about

[node:source:url:path]
EN: /what/about
FR: /quoi/about

So it looks like the node:source:url:path is having the same issue as node:source:menu-links.

philkay’s picture

Priority: Normal » Major
pgrandeg’s picture

Hello,

Same issue here. We are trying to keep URLs on original version for every language. Example:

http://localhost/en/parent/child
http://localhost/es/parent/child

And we are getting:
http://localhost/en/parent/child
http://localhost/es/padre/child

This is the token string we are using in our pattern: [node:source:menu-link:parents]/[node:source:title]

Could anyone advice us please?
Thank you in advance

pgrandeg’s picture

StatusFileSize
new958 bytes

Hello,

It would be improved by mantainers, but it still works.

I hope it helps

nwom’s picture

Status: Active » Needs review

Setting to Needs Review, since a patch is attached.

bohus ulrych’s picture

Hello @pgrandeg
thanks for patch #5, seems to be working well - tested with D8.9.14

bboty’s picture

Thank you, the patch has been working well on my client's website for 3 years now.

bboty’s picture

Status: Needs review » Reviewed & tested by the community
berdir’s picture

Status: Reviewed & tested by the community » Needs work
+++ b/token.tokens.inc
@@ -680,12 +680,17 @@ function token_tokens($type, array $tokens, array $data = [], array $options = [
     foreach ($tokens as $name => $original) {
+      if (preg_match('/\:source\:/', $original)) {
+        $langcode = $defaultLanguage->getId();
+      }

this makes the assumption that the source is the same as the default translation, but that's necessarily the case.

What should be done instead I think is that the node:source token needs to update the langcode option to match its active language, that should be passed through both to [node:source:menu-link] (which this patch doesn't yet handle) and nested tokens.

Also, tests would be great, but source tests are quite lacking as we don't actually add translations, so it can't really be verified that this works.

hswong3i made their first commit to this issue’s fork.

hswong3i’s picture

Status: Needs work » Needs review
berdir’s picture

Status: Needs review » Needs work

This changes langcode for all tokens, that's not OK.

This needs to be fixed where source gets passed through, with getUntranslated(), this is done correctly for nodes and needs to be the same here.

santanu mondal’s picture

Assigned: Unassigned » santanu mondal
santanu mondal’s picture

Assigned: santanu mondal » Unassigned