18

I have created node template for my node, and its working fine.

I need to print content type name, which is like "Whitepaper" and has machine name "w1" in my node template. If I use variable {{ node.bundle }}, its printing the machine name. But I need the human readable name.

I tried many elements of node, content and page variables but I'm not able to print the human readable content type name.

Can anybody help?

2 Answers 2

31

You should be able to get the human-readable name of the content type (or the "label") by using the following snippet in your node Twig template.

{{ node.type.entity.label }} 
2
  • 2
    yes, this is the easiest solution, didn't cross my mind to use this field referencing the node type
    – 4uk4
    May 27, 2017 at 6:19
  • 2
    I tried, {{ node.label }} which was returning machine name instead, {{ node.type.entity.label }} fulfilled my purpose, thanks :)
    – Dev
    May 29, 2017 at 4:46
5

You can add the human readable name of the content type in a preprocess hook:

mytheme.theme

use Drupal\node\Entity\NodeType;

/**
 * Implements hook_preprocess_node().
 */
function mytheme_preprocess_node(array &$variables) {
  $node = $variables['node'];
  $variables['content_type'] = NodeType::load($node->bundle())->label();
}

and use it in the node template:

{{ content_type }}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.