Problem/Motivation

When using node_singles tokens in Pathauto bulk generate, the output is always from the default language node, even when generating aliases in another language context.

Steps to reproduce

  • Create a node single entity with multiple language translations.
  • Set a pathauto pattern using [node_singles:[bundle]:title].
  • Generate all aliases in bulk
  • Alias is using default-language title for translated nodes.

Proposed resolution

In node_singles.tokens.inc, node_singles_tokens() fetches the single node but does not switch to the requested language before generating tokens. It always passes the base node entity, causing language-insensitive field output.

Adding the following code on line 61 in node_singles.tokens.inc fixes this problem for me.

    $token_langcode = $options['langcode'] ?? \Drupal::languageManager()->getCurrentLanguage()->getId();
    if ($node->hasTranslation($token_langcode)) {
      $node = $node->getTranslation($token_langcode);
    }
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

matthiaso created an issue. See original summary.

sourav_paul’s picture

Checking...

sourav_paul’s picture

Status: Active » Needs review

I reproduced the same failing code path through Drupal’s token service with an explicit langcode, which is the part Pathauto relies on. I enabled node_singles plus core multilingual modules with fixed code, created a single bundle home_single, and added a French translation.

Observed output from the live site:

service-fr=Accueil FR
service-en=Home EN
token-fr=Home EN
token-en=Home EN
manual-node-token-fr=Accueil FR
current-content-lang=en

That means the single service can load the French translation correctly, and core node token generation also works correctly when given the translated node.

dieterholvoet’s picture

Status: Needs review » Reviewed & tested by the community

This sounds like a core issue, because Drupal\node\Hook\NodeTokensHooks::tokens() loads the node translation for the body and summary tokens, but not the title token. But I'm okay with committing this since it's such a straightforward workaround.

dieterholvoet’s picture

Status: Reviewed & tested by the community » Fixed

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.