I have a content type that contains a field that is a reference to another node. I'm trying to include a field from the referenced node within the page for the main node, but I can't figure out how to add that. Here's how I'm getting the value within my theme:
function mytheme_preprocess_node(array &$variables) {
$node = $variables['node'];
if ( $node->get('field_testimonial') ) {
$referenced_nodes = $node->get('field_testimonial')->referencedEntities();
if ( count($referenced_nodes) > 0 ){
$referenced_node = $referenced_nodes[0];
//this is providing the value I want. how can I add that back to my page?
error_log($referenced_node->body->value);
}
}
}
Please help me add that value back to my variables so I can use it in my theme! Thank you for your help.
Just do the following
$variables['referenced_body'] = $referenced_node->body->value;
In your Twig-Template you can do this:
{{ referenced_body }}