14

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?

20

You should be able to get the human-readable name of the content type (or the "label") by doing {{ node.type.entity.label }} in your node Twig template.

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

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

mytheme.theme

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

and use it in the node template:

{{ content_type }}
  • Just for the record node_type_load is deprecated you should use NodeType::load() instead. – Insasse Nov 5 '18 at 15:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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