Displaying tokens in Drupal 11

Created on 4 February 2025, 3 months ago

Can't display tokens in the body of an article

Installed Token version 8.x-1.15 in Drupal 11.1.1 and created an article content type. Any tokens I attempt to add to the body don't get replaced by the expected term. For example [site:name], [user:uid], [current-user:name], [block_content:changed] just appear exactly as entered. They are not translated.


I had assumed that the Token filter module only added a menu to make it easy to select tokens, but that it wasn't required. I added this module just in case it did anything else to make tokens work as expected. It did give me menus to help select tokens, but none of the tokens I added through this menu got translated.


My ultimate goal is to use a twig template to add some logic to the display of one of my content types. I created a template called node--myContentType.html.twig and tried to get any of these tokens to display there. None of them did. I was able to display the author name using {{ author_name }}. The one token I need is [user:uid]. I tried to get this to display in the twig template using {{ user_uid }} as well as every possible combination of bracket, colon, underscore and period that I could imagine and none of them worked.


I would be grateful for any insights anyone can offer.

💬 Support request
Status

Active

Version

1.15

Component

Miscellaneous

Created by

🇨🇦Canada bcurry

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

Comments & Activities

  • Issue created by @bcurry
  • 🇨🇭Switzerland berdir Switzerland

    You're mixing up quite a few different things.

    The most important one being, token and twig have absolutely thing in common, they are two completely different systems, you can't use nor do you need to use tokens in twig templates.

    In twig templates, you have direct access to the relevant objects, so in a node template, you have access to node and access methods and fields. In your case, there's a method for accessing the user/owner of a node, so that would be {{ node.getOwnerId }}.

    You didn't specify if you want the current user UID or the node author, but I'm assuming the second. The current user would be quite bad for caching as it would be different for every authenticated user, then you need to consider cache contexts and things get more complex.

    Also, this module adds a token UI and more tokens, it does not support replacing tokens in content, that would be token_filter as you mentioned, so if you have a problem with that, you'll need to ask support there. That said, issues are a not a good place for support requests. Try slack, reddit, drupal stackexchange or something like that.

  • 🇨🇦Canada bcurry

    Thank you so much for these insights.

    In Drupal 7, I had a template file (node--myContentType.tpl.php) which used PHP to add logic based on $user->uid to a node. Online PHP to twig converters suggest that references to $user->uid would convert to user.uid in twig. Trying to reference the user's uid is what led me to Tokens.

    I'm pleased to see {{ node.getOwnerId }} does show in my node, but I actually need the user's uid not the author's. If anyone could point me in the right direction to adding that to a node would be greatly appreciated. Apparently adding [user:uid] to the body of an article through the Token module is not an option in Drupal 11.

  • 🇨🇭Switzerland berdir Switzerland

    You can make the current user information available in your template with preprocess: https://www.drupal.org/docs/8/theming-drupal-8/modifying-attributes-in-a... , so you implement preprocess, call \Drupal::currentUser() and store its id there.

    The problem as mentioned is caching. Drupal 8+ cache tons of elements automatically and it doesn't know that it depends on the current user. That's where things get more complicated.

    The quickfix would be to do https://stackoverflow.com/questions/42166081/preprocess-node-is-entered-..., tell it that it depends on the user cache context and then it works. by by making each node type unique to user, you will create a cached version of every node for every user and mostly break dynamic page caching as that refuses to cache for each unique user.

    More complex solutions involve using lazy builders to embed the part that''s per user dynamically later on, but I don't know what you're doing and we're already way, way outside of the scope of "token support". I suggest you use one of the mentioned channels above and actually describes why you need that user id I assume it's not only the ID you need but that you want to do something with it, and there might be better approaches to do this. You could post a link to that question and I might follow-up there

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

Production build 0.71.5 2024