I am trying to print the node body in a custom block, but it's not showing up. I'm I doing this correctly?

$parsed = parse_url($refer);
$alias = array_pop($parsed);
$dst = \Drupal::service('path.alias_manager')->getPathByAlias($alias , $langcode);
$nid = array_pop(explode('/', $dst));
$dest_node = node_load($nid);

$body = $dest_node->get('body')->getValue();
print $body; //have tried other printing methods also but invain


1 Answer 1



this will return an array. So,$body is an array. If you want the Body field values, then get it by accessing the proper index of the $body array. i.e $body[0]['value']

just replace print $body; with print $body[0]['value'];

  • That's right, getValue() gets an array with value, summary and format as keys. To access the body value there is a shorter way: $body_value = $dest_node->body->value;
    – 4uk4
    Commented May 3, 2016 at 10:04

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.