I have a tranlatable node, and I need to get its translation language. You know, I must to know is current node is in Polish or English. So... below there are my devel dumps from a node in Polish and a node in English. I see that there is a difference in #activeLangcode
(x-default and en). But how to get that value? Or is there any better way to check current language of node in twig? Please help.
-
You can do this with a hook_preprocess_node. Take a look at this: stackoverflow.com/questions/37186520/…– user98716Apr 21, 2020 at 14:52
Add a comment
|
1 Answer
A more elegant Drupal way without typing code:
- Go to the Manage display of the content type of your node (/admin/structure/types/manage/YOUR_CONTENT_TYPE/display/full). Select your display mode (or use the default). You will notice the field 'Language', move it to the upper side so it gets out of 'Disabled'.
- Once you have done that, the language of the node is available in Twig. You can access the id this way
{{ node.langcode.langcode }}
; or you can access the whole Object to get other data like the name{{ drupal_dump(node.langcode) }}
-
This is the only simple way to get the langcode in <D 10. What a horrible desaster. Shouldn't be that complicated to get it right from the entity. However, thank you so much!– ThomasFeb 10 at 13:40
-