6

How do I print the title of a node entity? Printing the node object, I get this output.

stdClass Object
(
    [__CLASS__] => Drupal\node\Entity\Node
    [in_preview] => 
    [values] => Array
        (
            [vid] => Array
                (
                    [x-default] => 2
                )

            [nid] => Array
                (
                    [x-default] => 2
                )

            [type] => Array
                (
                    [x-default] => common
                )

            [title] => Array
                (
                    [x-default] => Terms of Use
                )
}

2 Answers 2

16

Are you in a twig template rendering a node? If so:

{{ label }} or {{ node.label }}

Via code in PHP:

$node->label(); via label() and/or also $node->getTitle(); via getTitle()

2
  • the above code was part configuration setting form . I got it work by using $node->get('title')->value . Jan 20, 2017 at 20:31
  • {{ label }} gives me an object. {{ node.label }} worked for getting a bare string.
    – leymannx
    Apr 26 at 13:47
0

Tested: look for your node--.html.twig file, find the {{ content }}, then add this above or below, or anywhere you like to print the title: {{ node.label }}, it will show the current page/node title in your template, make sure you empty your cache to see the changes.

Your Answer

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

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