Hi,
I've created a custom block in Drupal 8 and I want to use it to display some node fields when the user is on the node page.
I've tried loading the node with the deprecated approach:

node_load($nid);

I can load any random node by it's id but if I try to load the currently viewed node, I get a blank page with no error in the logs.
Same result using the recommended approach. Any node works, but not the currently viewed node:

use Drupal\node\Entity\Node;
$node = Node::load($nid);

Why is Drupal 8 accepting to load any node in my block but not the one I'm currently viewing?
Thanks a lot for your answers.

Comments

David Fiaty’s picture

Or more precisely: what's the best approach to display some fields of the currently viewed node in a block in Drupal 8?

David Fiaty’s picture

Just in case it makes a difference, I'm using a custom block with the PHP filter, not the templating system.

David Fiaty’s picture

Ok, I now realise that what was giving me a blank page is trying to dump the node in the block with PHP.
The Drupal 8 code above works but trying var_dump($node) or print_r($node, 1) in the block will not work.
Thanks!

sprite’s picture

Rather than writing PHP, just create a View with a Block presentation and format the details to your liking.

spritefully yours
Technical assistance provided to the Drupal community on my own time ...
Thank yous appreciated ...

drupalfox’s picture