I have a node id. In my code I want to output this node to the screen using the standard template for the node. How do I print the node to the screen?
Consider the following: $node = \Drupal::routeMatch()->getParameter('node'); if ($node instanceof \Drupal\node\NodeInterface) { $nid = $node->id(); // Do whatever you need to do with the node ID here... }
How to find a node ID. To find the node ID of a particular node, go to the edit page for the node. The URL of the edit page for each node looks like demo.uiowa.edu/node/NID/edit, where NID is a number which is the node ID.
Body node ID class module is used to add node ID (nid) and node type as a class to <body> tag on node pages. In Drupal 7 core there was a unique node ID class in the <body> tag. This is forward-port of that functionality for Drupal 8.
print_r() works fine if you want to just look at the object structure (and using the devel module, the dpm() function that passes that output through krumo is even better).
To view the rendered version of the node, you should call the Drupal API function that is used to take a node object and run all the processing and theming routines used to generate the node output. In that case, it is node_view():
node_view(node_load(###));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With