Display a Media Entity Image from other (referencing) node in a Twig template
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
2
down vote
favorite
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.
theming entities media
add a comment |Â
up vote
2
down vote
favorite
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.
theming entities media
For better debugging you can temporarily set the levels Kint will go deep to a lower number, see drupal.stackexchange.com/a/224931/15055
â leymannx
Aug 6 at 20:15
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
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.
theming entities media
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.
theming entities media
edited Aug 6 at 20:17
leymannx
4,90532254
4,90532254
asked Aug 6 at 20:09
Oksana
155
155
For better debugging you can temporarily set the levels Kint will go deep to a lower number, see drupal.stackexchange.com/a/224931/15055
â leymannx
Aug 6 at 20:15
add a comment |Â
For better debugging you can temporarily set the levels Kint will go deep to a lower number, see drupal.stackexchange.com/a/224931/15055
â leymannx
Aug 6 at 20:15
For better debugging you can temporarily set the levels Kint will go deep to a lower number, see drupal.stackexchange.com/a/224931/15055
â leymannx
Aug 6 at 20:15
For better debugging you can temporarily set the levels Kint will go deep to a lower number, see drupal.stackexchange.com/a/224931/15055
â leymannx
Aug 6 at 20:15
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
2
down vote
accepted
To get a renderable output for Twig use EntityViewBuilder::viewMultiple()
$variables['related_nodes'] = Drupal::entityTypeManager()
->getViewBuilder('node')
->viewMultiple($relatednodes, $view_mode, $current_node->language()->getId());
and configure the three fields in the view mode you've specified here.
Thank you. But where should it be added? After$relatednodes = Node::loadMultiple($nids);
line?$view_mode
is not defined. Should I write$view_mode = 'full'
? Should Iuse onlyrelated_nodes
in the twig file (without project, desctiption, image)?
â Oksana
Aug 8 at 13:31
Yes, but use$view_mode = 'custom_view_mode'
and configure the three fields in this custom view mode. Withfull
you would get all fields. And then you can print the related nodes as configured in this custom view mode withrelated_nodes
in the twig template.
â 4k4
Aug 8 at 13:52
It works! But it displays all node fields [drive.google.com/file/d/1EkKDpZ1fjTCVEp5-ol-drD_QYlQZs3xE/⦠and if I edit a view mode, I can edit only a title [drive.google.com/file/d/1qgBmQiYMD19-QXCtFwvfxN0mMnZZ3X89/â¦. How to hide some fields? In code somewhere? @4k4
â Oksana
Aug 8 at 14:29
When you add a view mode you can only edit the name, this is correct. After that go to Manage Display of the content type, enable the new view mode in Custom Display Settings at the bottom and then you'll find a new tab at the top to configure the fields for the new view mode.
â 4k4
Aug 8 at 14:35
I've found the tab where to disable fields with Drag&Drop and where to hide labels. Saved, cleared caches, but no changes on my node page. The view mode is created in Content section, machine name is hasnode
at the beginning:$view_mode = 'node.example_img_description_project'
. Is it right?
â Oksana
Aug 8 at 15:05
 |Â
show 4 more comments
up vote
1
down vote
Does this necessarily need to be done in Twig? As you can achieve this without any coding with just field formatters and view modes. It will even cover the publication status.
Apart from that, you don't need to fire a query to get the referenced node IDs. You already have the node IDs as target_id
in the reference field's values.
$nids = ;
if (!$node->field_example_ideas->isEmpty())
$references = $node->field_example_ideas->getValue();
foreach ($references as $reference)
$nids = $reference['target_id'];
// This will give you all node IDs.
ksm($nids);
Approach without any code:
You have content type A
and content type B
. Content type A
has a node reference field, referencing content type B
. Content type B
has an image and now - for simplicity - let's say you want that content type A
only displays the image and nothing else of content type B
.
Go to Structure > Content types >
B
> Manage display tab > and add a new view mode (on the very bottom), or use an existing one, let's sayTeaser
. There you hide all fields except the image field.Go to Structure > Content types >
A
> Manage display tab > and here for your reference field choose "Rendered entity", then click on the small gear icon and select a view mode. Now it should say "Rendered as Teaser".Done :)
Avoid editing templates when it can be done from the UI
There are other alternatives as well, codeword "Pseudo Fields", that prevent using templates to override the UI and therefore make the UI useless in the end. Drop a comment, if the above isn't sufficient.
Yes, UI is probably the best approach for this example (although questions are often simplified and in reality there might be good reasons to do this in code). But I think you need the database query of the code example to get the related nodes and then you need a View to replace this with a UI solution, which you can attach to the node with EVA.
â 4k4
Aug 6 at 21:17
@4k4 â Fired a vanilla D8 to be 100% sure. You really don't need an extra query. Updated snippet slightly.
â leymannx
Aug 6 at 21:47
Thank you @leymannx, but you have misunderstood an important point. You write "Content type A has a node reference field, referencing content type B. Content type B has an image" This would be more easy. But the situation is: "Content type A DOES NOT HAVE a node reference field, referencing content type B. "Content type B has a node reference field, referencing content type A. Content type B has an image". This is needed to simplify the editorial work, so that all references are visible in one place.
â Oksana
Aug 8 at 13:58
@4k4 â Ah I see, now I got it. Reverse entity reference! Well, yeah, then there's maybe no pure UI way to accomplish this (,yet. At least until drupal.org/project/reverse_entity_reference would add a dedicated field type for this).
â leymannx
Aug 8 at 19:34
@Oksana â Now I got it. Thanks for clarifying. Well, yeah, then 4k4's answer probably is the way to go :)
â leymannx
Aug 8 at 19:41
 |Â
show 3 more comments
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
To get a renderable output for Twig use EntityViewBuilder::viewMultiple()
$variables['related_nodes'] = Drupal::entityTypeManager()
->getViewBuilder('node')
->viewMultiple($relatednodes, $view_mode, $current_node->language()->getId());
and configure the three fields in the view mode you've specified here.
Thank you. But where should it be added? After$relatednodes = Node::loadMultiple($nids);
line?$view_mode
is not defined. Should I write$view_mode = 'full'
? Should Iuse onlyrelated_nodes
in the twig file (without project, desctiption, image)?
â Oksana
Aug 8 at 13:31
Yes, but use$view_mode = 'custom_view_mode'
and configure the three fields in this custom view mode. Withfull
you would get all fields. And then you can print the related nodes as configured in this custom view mode withrelated_nodes
in the twig template.
â 4k4
Aug 8 at 13:52
It works! But it displays all node fields [drive.google.com/file/d/1EkKDpZ1fjTCVEp5-ol-drD_QYlQZs3xE/⦠and if I edit a view mode, I can edit only a title [drive.google.com/file/d/1qgBmQiYMD19-QXCtFwvfxN0mMnZZ3X89/â¦. How to hide some fields? In code somewhere? @4k4
â Oksana
Aug 8 at 14:29
When you add a view mode you can only edit the name, this is correct. After that go to Manage Display of the content type, enable the new view mode in Custom Display Settings at the bottom and then you'll find a new tab at the top to configure the fields for the new view mode.
â 4k4
Aug 8 at 14:35
I've found the tab where to disable fields with Drag&Drop and where to hide labels. Saved, cleared caches, but no changes on my node page. The view mode is created in Content section, machine name is hasnode
at the beginning:$view_mode = 'node.example_img_description_project'
. Is it right?
â Oksana
Aug 8 at 15:05
 |Â
show 4 more comments
up vote
2
down vote
accepted
To get a renderable output for Twig use EntityViewBuilder::viewMultiple()
$variables['related_nodes'] = Drupal::entityTypeManager()
->getViewBuilder('node')
->viewMultiple($relatednodes, $view_mode, $current_node->language()->getId());
and configure the three fields in the view mode you've specified here.
Thank you. But where should it be added? After$relatednodes = Node::loadMultiple($nids);
line?$view_mode
is not defined. Should I write$view_mode = 'full'
? Should Iuse onlyrelated_nodes
in the twig file (without project, desctiption, image)?
â Oksana
Aug 8 at 13:31
Yes, but use$view_mode = 'custom_view_mode'
and configure the three fields in this custom view mode. Withfull
you would get all fields. And then you can print the related nodes as configured in this custom view mode withrelated_nodes
in the twig template.
â 4k4
Aug 8 at 13:52
It works! But it displays all node fields [drive.google.com/file/d/1EkKDpZ1fjTCVEp5-ol-drD_QYlQZs3xE/⦠and if I edit a view mode, I can edit only a title [drive.google.com/file/d/1qgBmQiYMD19-QXCtFwvfxN0mMnZZ3X89/â¦. How to hide some fields? In code somewhere? @4k4
â Oksana
Aug 8 at 14:29
When you add a view mode you can only edit the name, this is correct. After that go to Manage Display of the content type, enable the new view mode in Custom Display Settings at the bottom and then you'll find a new tab at the top to configure the fields for the new view mode.
â 4k4
Aug 8 at 14:35
I've found the tab where to disable fields with Drag&Drop and where to hide labels. Saved, cleared caches, but no changes on my node page. The view mode is created in Content section, machine name is hasnode
at the beginning:$view_mode = 'node.example_img_description_project'
. Is it right?
â Oksana
Aug 8 at 15:05
 |Â
show 4 more comments
up vote
2
down vote
accepted
up vote
2
down vote
accepted
To get a renderable output for Twig use EntityViewBuilder::viewMultiple()
$variables['related_nodes'] = Drupal::entityTypeManager()
->getViewBuilder('node')
->viewMultiple($relatednodes, $view_mode, $current_node->language()->getId());
and configure the three fields in the view mode you've specified here.
To get a renderable output for Twig use EntityViewBuilder::viewMultiple()
$variables['related_nodes'] = Drupal::entityTypeManager()
->getViewBuilder('node')
->viewMultiple($relatednodes, $view_mode, $current_node->language()->getId());
and configure the three fields in the view mode you've specified here.
answered Aug 6 at 20:37
4k4
46.1k34478
46.1k34478
Thank you. But where should it be added? After$relatednodes = Node::loadMultiple($nids);
line?$view_mode
is not defined. Should I write$view_mode = 'full'
? Should Iuse onlyrelated_nodes
in the twig file (without project, desctiption, image)?
â Oksana
Aug 8 at 13:31
Yes, but use$view_mode = 'custom_view_mode'
and configure the three fields in this custom view mode. Withfull
you would get all fields. And then you can print the related nodes as configured in this custom view mode withrelated_nodes
in the twig template.
â 4k4
Aug 8 at 13:52
It works! But it displays all node fields [drive.google.com/file/d/1EkKDpZ1fjTCVEp5-ol-drD_QYlQZs3xE/⦠and if I edit a view mode, I can edit only a title [drive.google.com/file/d/1qgBmQiYMD19-QXCtFwvfxN0mMnZZ3X89/â¦. How to hide some fields? In code somewhere? @4k4
â Oksana
Aug 8 at 14:29
When you add a view mode you can only edit the name, this is correct. After that go to Manage Display of the content type, enable the new view mode in Custom Display Settings at the bottom and then you'll find a new tab at the top to configure the fields for the new view mode.
â 4k4
Aug 8 at 14:35
I've found the tab where to disable fields with Drag&Drop and where to hide labels. Saved, cleared caches, but no changes on my node page. The view mode is created in Content section, machine name is hasnode
at the beginning:$view_mode = 'node.example_img_description_project'
. Is it right?
â Oksana
Aug 8 at 15:05
 |Â
show 4 more comments
Thank you. But where should it be added? After$relatednodes = Node::loadMultiple($nids);
line?$view_mode
is not defined. Should I write$view_mode = 'full'
? Should Iuse onlyrelated_nodes
in the twig file (without project, desctiption, image)?
â Oksana
Aug 8 at 13:31
Yes, but use$view_mode = 'custom_view_mode'
and configure the three fields in this custom view mode. Withfull
you would get all fields. And then you can print the related nodes as configured in this custom view mode withrelated_nodes
in the twig template.
â 4k4
Aug 8 at 13:52
It works! But it displays all node fields [drive.google.com/file/d/1EkKDpZ1fjTCVEp5-ol-drD_QYlQZs3xE/⦠and if I edit a view mode, I can edit only a title [drive.google.com/file/d/1qgBmQiYMD19-QXCtFwvfxN0mMnZZ3X89/â¦. How to hide some fields? In code somewhere? @4k4
â Oksana
Aug 8 at 14:29
When you add a view mode you can only edit the name, this is correct. After that go to Manage Display of the content type, enable the new view mode in Custom Display Settings at the bottom and then you'll find a new tab at the top to configure the fields for the new view mode.
â 4k4
Aug 8 at 14:35
I've found the tab where to disable fields with Drag&Drop and where to hide labels. Saved, cleared caches, but no changes on my node page. The view mode is created in Content section, machine name is hasnode
at the beginning:$view_mode = 'node.example_img_description_project'
. Is it right?
â Oksana
Aug 8 at 15:05
Thank you. But where should it be added? After
$relatednodes = Node::loadMultiple($nids);
line? $view_mode
is not defined. Should I write $view_mode = 'full'
? Should Iuse only related_nodes
in the twig file (without project, desctiption, image)?â Oksana
Aug 8 at 13:31
Thank you. But where should it be added? After
$relatednodes = Node::loadMultiple($nids);
line? $view_mode
is not defined. Should I write $view_mode = 'full'
? Should Iuse only related_nodes
in the twig file (without project, desctiption, image)?â Oksana
Aug 8 at 13:31
Yes, but use
$view_mode = 'custom_view_mode'
and configure the three fields in this custom view mode. With full
you would get all fields. And then you can print the related nodes as configured in this custom view mode with related_nodes
in the twig template.â 4k4
Aug 8 at 13:52
Yes, but use
$view_mode = 'custom_view_mode'
and configure the three fields in this custom view mode. With full
you would get all fields. And then you can print the related nodes as configured in this custom view mode with related_nodes
in the twig template.â 4k4
Aug 8 at 13:52
It works! But it displays all node fields [drive.google.com/file/d/1EkKDpZ1fjTCVEp5-ol-drD_QYlQZs3xE/⦠and if I edit a view mode, I can edit only a title [drive.google.com/file/d/1qgBmQiYMD19-QXCtFwvfxN0mMnZZ3X89/â¦. How to hide some fields? In code somewhere? @4k4
â Oksana
Aug 8 at 14:29
It works! But it displays all node fields [drive.google.com/file/d/1EkKDpZ1fjTCVEp5-ol-drD_QYlQZs3xE/⦠and if I edit a view mode, I can edit only a title [drive.google.com/file/d/1qgBmQiYMD19-QXCtFwvfxN0mMnZZ3X89/â¦. How to hide some fields? In code somewhere? @4k4
â Oksana
Aug 8 at 14:29
When you add a view mode you can only edit the name, this is correct. After that go to Manage Display of the content type, enable the new view mode in Custom Display Settings at the bottom and then you'll find a new tab at the top to configure the fields for the new view mode.
â 4k4
Aug 8 at 14:35
When you add a view mode you can only edit the name, this is correct. After that go to Manage Display of the content type, enable the new view mode in Custom Display Settings at the bottom and then you'll find a new tab at the top to configure the fields for the new view mode.
â 4k4
Aug 8 at 14:35
I've found the tab where to disable fields with Drag&Drop and where to hide labels. Saved, cleared caches, but no changes on my node page. The view mode is created in Content section, machine name is has
node
at the beginning: $view_mode = 'node.example_img_description_project'
. Is it right?â Oksana
Aug 8 at 15:05
I've found the tab where to disable fields with Drag&Drop and where to hide labels. Saved, cleared caches, but no changes on my node page. The view mode is created in Content section, machine name is has
node
at the beginning: $view_mode = 'node.example_img_description_project'
. Is it right?â Oksana
Aug 8 at 15:05
 |Â
show 4 more comments
up vote
1
down vote
Does this necessarily need to be done in Twig? As you can achieve this without any coding with just field formatters and view modes. It will even cover the publication status.
Apart from that, you don't need to fire a query to get the referenced node IDs. You already have the node IDs as target_id
in the reference field's values.
$nids = ;
if (!$node->field_example_ideas->isEmpty())
$references = $node->field_example_ideas->getValue();
foreach ($references as $reference)
$nids = $reference['target_id'];
// This will give you all node IDs.
ksm($nids);
Approach without any code:
You have content type A
and content type B
. Content type A
has a node reference field, referencing content type B
. Content type B
has an image and now - for simplicity - let's say you want that content type A
only displays the image and nothing else of content type B
.
Go to Structure > Content types >
B
> Manage display tab > and add a new view mode (on the very bottom), or use an existing one, let's sayTeaser
. There you hide all fields except the image field.Go to Structure > Content types >
A
> Manage display tab > and here for your reference field choose "Rendered entity", then click on the small gear icon and select a view mode. Now it should say "Rendered as Teaser".Done :)
Avoid editing templates when it can be done from the UI
There are other alternatives as well, codeword "Pseudo Fields", that prevent using templates to override the UI and therefore make the UI useless in the end. Drop a comment, if the above isn't sufficient.
Yes, UI is probably the best approach for this example (although questions are often simplified and in reality there might be good reasons to do this in code). But I think you need the database query of the code example to get the related nodes and then you need a View to replace this with a UI solution, which you can attach to the node with EVA.
â 4k4
Aug 6 at 21:17
@4k4 â Fired a vanilla D8 to be 100% sure. You really don't need an extra query. Updated snippet slightly.
â leymannx
Aug 6 at 21:47
Thank you @leymannx, but you have misunderstood an important point. You write "Content type A has a node reference field, referencing content type B. Content type B has an image" This would be more easy. But the situation is: "Content type A DOES NOT HAVE a node reference field, referencing content type B. "Content type B has a node reference field, referencing content type A. Content type B has an image". This is needed to simplify the editorial work, so that all references are visible in one place.
â Oksana
Aug 8 at 13:58
@4k4 â Ah I see, now I got it. Reverse entity reference! Well, yeah, then there's maybe no pure UI way to accomplish this (,yet. At least until drupal.org/project/reverse_entity_reference would add a dedicated field type for this).
â leymannx
Aug 8 at 19:34
@Oksana â Now I got it. Thanks for clarifying. Well, yeah, then 4k4's answer probably is the way to go :)
â leymannx
Aug 8 at 19:41
 |Â
show 3 more comments
up vote
1
down vote
Does this necessarily need to be done in Twig? As you can achieve this without any coding with just field formatters and view modes. It will even cover the publication status.
Apart from that, you don't need to fire a query to get the referenced node IDs. You already have the node IDs as target_id
in the reference field's values.
$nids = ;
if (!$node->field_example_ideas->isEmpty())
$references = $node->field_example_ideas->getValue();
foreach ($references as $reference)
$nids = $reference['target_id'];
// This will give you all node IDs.
ksm($nids);
Approach without any code:
You have content type A
and content type B
. Content type A
has a node reference field, referencing content type B
. Content type B
has an image and now - for simplicity - let's say you want that content type A
only displays the image and nothing else of content type B
.
Go to Structure > Content types >
B
> Manage display tab > and add a new view mode (on the very bottom), or use an existing one, let's sayTeaser
. There you hide all fields except the image field.Go to Structure > Content types >
A
> Manage display tab > and here for your reference field choose "Rendered entity", then click on the small gear icon and select a view mode. Now it should say "Rendered as Teaser".Done :)
Avoid editing templates when it can be done from the UI
There are other alternatives as well, codeword "Pseudo Fields", that prevent using templates to override the UI and therefore make the UI useless in the end. Drop a comment, if the above isn't sufficient.
Yes, UI is probably the best approach for this example (although questions are often simplified and in reality there might be good reasons to do this in code). But I think you need the database query of the code example to get the related nodes and then you need a View to replace this with a UI solution, which you can attach to the node with EVA.
â 4k4
Aug 6 at 21:17
@4k4 â Fired a vanilla D8 to be 100% sure. You really don't need an extra query. Updated snippet slightly.
â leymannx
Aug 6 at 21:47
Thank you @leymannx, but you have misunderstood an important point. You write "Content type A has a node reference field, referencing content type B. Content type B has an image" This would be more easy. But the situation is: "Content type A DOES NOT HAVE a node reference field, referencing content type B. "Content type B has a node reference field, referencing content type A. Content type B has an image". This is needed to simplify the editorial work, so that all references are visible in one place.
â Oksana
Aug 8 at 13:58
@4k4 â Ah I see, now I got it. Reverse entity reference! Well, yeah, then there's maybe no pure UI way to accomplish this (,yet. At least until drupal.org/project/reverse_entity_reference would add a dedicated field type for this).
â leymannx
Aug 8 at 19:34
@Oksana â Now I got it. Thanks for clarifying. Well, yeah, then 4k4's answer probably is the way to go :)
â leymannx
Aug 8 at 19:41
 |Â
show 3 more comments
up vote
1
down vote
up vote
1
down vote
Does this necessarily need to be done in Twig? As you can achieve this without any coding with just field formatters and view modes. It will even cover the publication status.
Apart from that, you don't need to fire a query to get the referenced node IDs. You already have the node IDs as target_id
in the reference field's values.
$nids = ;
if (!$node->field_example_ideas->isEmpty())
$references = $node->field_example_ideas->getValue();
foreach ($references as $reference)
$nids = $reference['target_id'];
// This will give you all node IDs.
ksm($nids);
Approach without any code:
You have content type A
and content type B
. Content type A
has a node reference field, referencing content type B
. Content type B
has an image and now - for simplicity - let's say you want that content type A
only displays the image and nothing else of content type B
.
Go to Structure > Content types >
B
> Manage display tab > and add a new view mode (on the very bottom), or use an existing one, let's sayTeaser
. There you hide all fields except the image field.Go to Structure > Content types >
A
> Manage display tab > and here for your reference field choose "Rendered entity", then click on the small gear icon and select a view mode. Now it should say "Rendered as Teaser".Done :)
Avoid editing templates when it can be done from the UI
There are other alternatives as well, codeword "Pseudo Fields", that prevent using templates to override the UI and therefore make the UI useless in the end. Drop a comment, if the above isn't sufficient.
Does this necessarily need to be done in Twig? As you can achieve this without any coding with just field formatters and view modes. It will even cover the publication status.
Apart from that, you don't need to fire a query to get the referenced node IDs. You already have the node IDs as target_id
in the reference field's values.
$nids = ;
if (!$node->field_example_ideas->isEmpty())
$references = $node->field_example_ideas->getValue();
foreach ($references as $reference)
$nids = $reference['target_id'];
// This will give you all node IDs.
ksm($nids);
Approach without any code:
You have content type A
and content type B
. Content type A
has a node reference field, referencing content type B
. Content type B
has an image and now - for simplicity - let's say you want that content type A
only displays the image and nothing else of content type B
.
Go to Structure > Content types >
B
> Manage display tab > and add a new view mode (on the very bottom), or use an existing one, let's sayTeaser
. There you hide all fields except the image field.Go to Structure > Content types >
A
> Manage display tab > and here for your reference field choose "Rendered entity", then click on the small gear icon and select a view mode. Now it should say "Rendered as Teaser".Done :)
Avoid editing templates when it can be done from the UI
There are other alternatives as well, codeword "Pseudo Fields", that prevent using templates to override the UI and therefore make the UI useless in the end. Drop a comment, if the above isn't sufficient.
edited Aug 6 at 22:07
answered Aug 6 at 20:53
leymannx
4,90532254
4,90532254
Yes, UI is probably the best approach for this example (although questions are often simplified and in reality there might be good reasons to do this in code). But I think you need the database query of the code example to get the related nodes and then you need a View to replace this with a UI solution, which you can attach to the node with EVA.
â 4k4
Aug 6 at 21:17
@4k4 â Fired a vanilla D8 to be 100% sure. You really don't need an extra query. Updated snippet slightly.
â leymannx
Aug 6 at 21:47
Thank you @leymannx, but you have misunderstood an important point. You write "Content type A has a node reference field, referencing content type B. Content type B has an image" This would be more easy. But the situation is: "Content type A DOES NOT HAVE a node reference field, referencing content type B. "Content type B has a node reference field, referencing content type A. Content type B has an image". This is needed to simplify the editorial work, so that all references are visible in one place.
â Oksana
Aug 8 at 13:58
@4k4 â Ah I see, now I got it. Reverse entity reference! Well, yeah, then there's maybe no pure UI way to accomplish this (,yet. At least until drupal.org/project/reverse_entity_reference would add a dedicated field type for this).
â leymannx
Aug 8 at 19:34
@Oksana â Now I got it. Thanks for clarifying. Well, yeah, then 4k4's answer probably is the way to go :)
â leymannx
Aug 8 at 19:41
 |Â
show 3 more comments
Yes, UI is probably the best approach for this example (although questions are often simplified and in reality there might be good reasons to do this in code). But I think you need the database query of the code example to get the related nodes and then you need a View to replace this with a UI solution, which you can attach to the node with EVA.
â 4k4
Aug 6 at 21:17
@4k4 â Fired a vanilla D8 to be 100% sure. You really don't need an extra query. Updated snippet slightly.
â leymannx
Aug 6 at 21:47
Thank you @leymannx, but you have misunderstood an important point. You write "Content type A has a node reference field, referencing content type B. Content type B has an image" This would be more easy. But the situation is: "Content type A DOES NOT HAVE a node reference field, referencing content type B. "Content type B has a node reference field, referencing content type A. Content type B has an image". This is needed to simplify the editorial work, so that all references are visible in one place.
â Oksana
Aug 8 at 13:58
@4k4 â Ah I see, now I got it. Reverse entity reference! Well, yeah, then there's maybe no pure UI way to accomplish this (,yet. At least until drupal.org/project/reverse_entity_reference would add a dedicated field type for this).
â leymannx
Aug 8 at 19:34
@Oksana â Now I got it. Thanks for clarifying. Well, yeah, then 4k4's answer probably is the way to go :)
â leymannx
Aug 8 at 19:41
Yes, UI is probably the best approach for this example (although questions are often simplified and in reality there might be good reasons to do this in code). But I think you need the database query of the code example to get the related nodes and then you need a View to replace this with a UI solution, which you can attach to the node with EVA.
â 4k4
Aug 6 at 21:17
Yes, UI is probably the best approach for this example (although questions are often simplified and in reality there might be good reasons to do this in code). But I think you need the database query of the code example to get the related nodes and then you need a View to replace this with a UI solution, which you can attach to the node with EVA.
â 4k4
Aug 6 at 21:17
@4k4 â Fired a vanilla D8 to be 100% sure. You really don't need an extra query. Updated snippet slightly.
â leymannx
Aug 6 at 21:47
@4k4 â Fired a vanilla D8 to be 100% sure. You really don't need an extra query. Updated snippet slightly.
â leymannx
Aug 6 at 21:47
Thank you @leymannx, but you have misunderstood an important point. You write "Content type A has a node reference field, referencing content type B. Content type B has an image" This would be more easy. But the situation is: "Content type A DOES NOT HAVE a node reference field, referencing content type B. "Content type B has a node reference field, referencing content type A. Content type B has an image". This is needed to simplify the editorial work, so that all references are visible in one place.
â Oksana
Aug 8 at 13:58
Thank you @leymannx, but you have misunderstood an important point. You write "Content type A has a node reference field, referencing content type B. Content type B has an image" This would be more easy. But the situation is: "Content type A DOES NOT HAVE a node reference field, referencing content type B. "Content type B has a node reference field, referencing content type A. Content type B has an image". This is needed to simplify the editorial work, so that all references are visible in one place.
â Oksana
Aug 8 at 13:58
@4k4 â Ah I see, now I got it. Reverse entity reference! Well, yeah, then there's maybe no pure UI way to accomplish this (,yet. At least until drupal.org/project/reverse_entity_reference would add a dedicated field type for this).
â leymannx
Aug 8 at 19:34
@4k4 â Ah I see, now I got it. Reverse entity reference! Well, yeah, then there's maybe no pure UI way to accomplish this (,yet. At least until drupal.org/project/reverse_entity_reference would add a dedicated field type for this).
â leymannx
Aug 8 at 19:34
@Oksana â Now I got it. Thanks for clarifying. Well, yeah, then 4k4's answer probably is the way to go :)
â leymannx
Aug 8 at 19:41
@Oksana â Now I got it. Thanks for clarifying. Well, yeah, then 4k4's answer probably is the way to go :)
â leymannx
Aug 8 at 19:41
 |Â
show 3 more comments
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdrupal.stackexchange.com%2fquestions%2f267063%2fdisplay-a-media-entity-image-from-other-referencing-node-in-a-twig-template%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
For better debugging you can temporarily set the levels Kint will go deep to a lower number, see drupal.stackexchange.com/a/224931/15055
â leymannx
Aug 6 at 20:15