D8, how to make node template aware of entity reference parent I have a multivalue entity reference field, referencing basic content. 
The content (node) view is controlled via display modes (display modes is probably not relevant to the question)

Right now the entity reference field template is 'boilerplate' and just outputs the content in a for loop. 

{% for item in items %}    
    {{ item.content }}
{% endfor %} 


My display node template, node--[content type]--[display mode].tpl.twig
has custom markup, and granular output of field values. 

However somewhere 'deep' in this template I need to alter the markup in accordance with the position this node has in the parent referencing entity. 

i.e. 


entity reference field:  


node 1  
node 2
node 3 &lt;-- pass this list
position value somehow into the child node template, so i can change
the markup inside   
node 4



I've been stepping through preprocessor variables in node_preprocess and field_preprocess, but I'm not really clear if the child node is 'aware' of its parent field, or more importantly the hierarchy of the parent field, and its position in it. 

Any help or pointers much appreciated!
well, this seems to be working. 

function THEME_preprocess_node(&amp;$variables) {
$nid  = $variables['node']-&gt;id();

if ($variables['elements']['#view_mode'] === 'VIEW_MODE') {
// Get the node ID.
    $variables['nid'] = $nid;
    $parent   = $variables['elements']['#node']-&gt;_referringItem-&gt;getEntity();
    $items = $parent-&gt;get('FIELD_NAME')-&gt;getValue();

    foreach ($items as $key =&gt; $item) {
        if ($item['target_id'] == $nid) {
            $variables['tree_position'] = $key;
        }
    }
  }
}


Not sure this is the best way. So any suggestions still welcome.