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

enter image description here

        // Entity Query for Academic Experience Entity Reference Field
        $academicExperience = $query->get('node')
                                    ->condition('status', 1, '=')

        foreach ($academicExperience as $key => $academic_menu) {    

             $academicNode = _nodeLoad($academic_menu);

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

                {% for academic in academic_menu %}

                    <span>{{ academic['menu'] }}</span>

                {% endfor %}

1 Answer 1


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(&$variables) {

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

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

    $reference_links = array(); // creates empty array
    foreach ($node->field_academic_experience_ref as $item) { // loop through ref field
      if ($item->entity) { // gets referenced node object
        $reference_links[] = $item->entity->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 %}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.