Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By David Fiaty on
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
Or more precisely: what's the
Or more precisely: what's the best approach to display some fields of the currently viewed node in a block in Drupal 8?
Just in case it makes a
Just in case it makes a difference, I'm using a custom block with the PHP filter, not the templating system.
Ok, I now realise that what
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!
just create a view
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 ...
Try fieldblock module
Check below module.
https://www.drupal.org/project/fieldblock