Getting the values and url of an entity reference field using entity query then store it inside a variable
Getting the values and url


Sounds like you want to link to the referenced node, so I will assume that you want to do that.


No need to use entity query at all.


You can do it like this, assuming your content type is Article:

function mytheme_preprocess_node(&amp;$variables) {

  $node = $variables['node']; // gets current node object

  if ($node-&gt;getType() == 'article') { // checks if content type is article

    $reference_links = array(); // creates empty array
    foreach ($node-&gt;field_academic_experience_ref as $item) { // loop through ref field
      if ($item-&gt;entity) { // gets referenced node object
        $reference_links[] = $item-&gt;entity-&gt;toLink(); // gets ref node link, adds to array
      }
    }

    $variables['academic_menu'] = $reference_links; // creates variable

  }

}


Then in twig

{% for reference_link in academic_menu %}
   {{ reference_link }}
{% endfor %}