How do I get the output of a field of a node? I have some weird subpage construct using entity references (one 'main' node and a few 'sub'-nodes).

Each of these nodes have a header image to be displayed above the content. What I am trying to do is for the sub-nodes to 'inherit' the header image of the main node.

I implemented hook_node_view and I have got the node, I just don't know how to build the renderable array for it.

function hook_node_view(array &$build, \Drupal\Core\Entity\EntityInterface $entity, \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display, $view_mode) {
  if($view_mode == 'full'){
    if((!isset($build['field_headerimage']) || !isset($build['field_headerimage'][0])) && $entity->hasField('field_location')){
      $location = $entity->get('field_location')->referencedEntities();
      if(is_array($location) && isset($location[0])) {
        $node = $location[0];
        dsm('yay, now what?');

The answer to my own question seems to be like so:

$build['field_headerimage'] = $node->get('field_headerimage')->view();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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