Seems that all found in the Internet does not work for my case. :/ In general, I need to display an Image and a Project, referenced by the node that has a reference field to my node.
I have:
- a node of CT Idea (here I want to display my content)
a node of CT Example, which has:
1) a ref field to Idea 2) a ref field to a Media, Image 3) a ref field to a CT Project
I want to display Examples on an Idea page, each Example displays: Description, Image, Project link
I've managed to display the Description, but can not display Image and Project.
Here is the code
in the *.theme
file:
function showcase_lite_preprocess_node__idea(&$variables) {
$current_node = $variables['node'];
$current_node_id = $current_node->id();
$query = \Drupal::entityQuery('node');
$query->condition('type', 'example');
$query->condition('status', 1);
$query->condition('field_example_ideas', $current_node_id);
$nids = $query->execute();
$relatednodes = Node::loadMultiple($nids);
if (!empty($relatednodes)) {
foreach ($relatednodes as $related_node) {
$related_node_image = $related_node->field_example_image->value;
$related_node_description = $related_node->field_example_description->value;
$related_node_project = $related_node->field_example_project->value;
$variables['related_node_image'] = $related_node_image;
$variables['related_node_description'] = $related_node_description;
$variables['related_node_project'] = $related_node_project;
}
}
}
In the node--idea.html.twig
file:
{% block content %}
{{ content }}
{{ related_node_description }}
{{ related_node_image }}
{{ related_node_project }}
{% endblock %}
This displays only the Idea content and the Example Description.
It seems to me that the problem is that the format of $related_node_image
and $related_node_project
does not correspond to what Twig is expecting. But I can not check what is inside the variable, because Kint gives an error of memory limit.
Drupal version: 8.5.3.