Getting the values and url of an entity reference field using entity query then store it inside a variable I'm trying to figure out how I can output the values and url of the entity reference field values and display them inside the twig. Here is a sample of the code that I have constructed. I can't seem to find out what else is missing



        // Entity Query for Academic Experience Entity Reference Field
        $academicExperience = $query-&gt;get('node')
                                    -&gt;condition('status', 1, '=')
                                    -&gt;condition('type','programs')        
                                    -&gt;sort('created')
                                    -&gt;execute();

        foreach ($academicExperience as $key =&gt; $academic_menu) {    

             $academicNode = _nodeLoad($academic_menu);

             //Entity Reference Field
             $variables['academic_menu'][$key]['menu'] = $academicNode-&gt;get('field_academic_experience_ref')-&gt;value;
        }        




                {% for academic in academic_menu %}

                    &lt;span&gt;{{ academic['menu'] }}&lt;/span&gt;

                {% endfor %}
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 %}