5

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
                )
}
11

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()

|improve this answer|||||
  • the above code was part configuration setting form . I got it work by using $node->get('title')->value . – Aditya Joshi Jan 20 '17 at 20:31
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.

|improve this answer|||||

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.