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);
}Issue fork node_singles-3579427
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
Comment #2
sourav_paulChecking...
Comment #4
sourav_paulI 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.
Comment #5
dieterholvoet commentedThis sounds like a core issue, because
Drupal\node\Hook\NodeTokensHooks::tokens()loads the node translation for thebodyandsummarytokens, but not thetitletoken. But I'm okay with committing this since it's such a straightforward workaround.Comment #7
dieterholvoet commented