1

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

$refer = $_SERVER[HTTP_REFERER];
$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

Thanks

1 Answer 1

2

$dest_node->get('body')->getValue();

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'];

1
  • 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
    May 3, 2016 at 10:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.