I have a node that has a reference field to the taxonomy vocabulary. In the Manage display of the reference field, the Rendered entity format is selected, and the view mode (field_image field display) I need to override the template and make something like this:
<div style="background-color: {{content.field_hex.0.value}};">
{{content.field_image}}
</div>
The variable may not be content but something else. It depends on the template.
If a taxonomy term has an added image, we display it; if not, we display the background-color where we pass the field_hex field of the term, which is of type Text (plain).
Which template needs to be overridden to get the required markup? field.html.twig? Or taxonomy-term.html.twig? And how to get the value of the field_image and field_hex fields in the template?
Inside the overridden field.html.twig {{dump(item.content)}}
shows:
array:6 [
"#taxonomy_term" => Drupal\taxonomy\Entity\Term {
#18802
}
"#view_mode" => "product_colors"
"#cache" => array:5 [
]
"#theme" => "taxonomy_term"
"#weight" => 0
"#pre_render" => array:1 []
]
Inside the overridden taxonomy-term.html.twig {{ dump(term) }}
shows:
Drupal\taxonomy\Entity\Term {
#entityTypeId: "taxonomy_term"
// other keys
#values: array:23 []
#fields: array:3 []
// other keys
}
Screenshot of the current markup from the page where the nodes with the reference field are displayed: