node:source:menu-link returns translated menu, not source menu

Created on 8 January 2020, almost 5 years ago
Updated 28 February 2024, 9 months ago

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?

🐛 Bug report
Status

Needs work

Version

1.0

Component

Token Actions

Created by

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇷🇴Romania bboty

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

  • Status changed to RTBC over 1 year ago
  • Status changed to Needs work over 1 year ago
  • 🇨🇭Switzerland berdir Switzerland
    +++ 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.

  • First commit to issue fork.
  • Open in Jenkins → Open on Drupal.org →
    Core: 10.1.4 + Environment: PHP 8.1 & MariaDB 10.3.22
    last update 9 months ago
    78 pass
  • Pipeline finished with Success
    9 months ago
    Total: 317s
    #105594
Production build 0.71.5 2024