0

I've got a view with a contextual filter for an entity reference field. The contextual filter fetches the current node id from URL as filter criteria. The individual rows of the view are rendered as content in teaser view mode (not using fields), so basically it uses node--nodetype--teaser.html.twig to render the results. The view is attached to nodes using the EVA module

I now need the value of a field from the host/contextual node in node--nodetype--teaser.html.twig (the template used by the views row). I know how to query entities and fields, but I don't know which hook to use and how to get the node ID of the contextual filter.

How can I get the contextual filter ID and which hook should I use to pass the contextual filter down to the node teaser template?

3

Implement template_preprocess_node in your .theme file and get the page's node with the following code.

/**
 * Implements template_preprocess_node().
 */
my_theme_preprocess_node(&$vars) {
  // This will fire for any node type with the teaser view mode
  if ($vars['view_mode'] == 'teaser')
    $node = \Drupal::routeMatch()->getParameter('node');
    if ($node instanceof \Drupal\node\NodeInterface) {
      // You have the full node object and can access fields as needed.
      // You can even check the $node type in the above if statement to only add
      // this value to the teaser for nodes when you're on the type with your EVA
      // Make sure to add them to the $vars array to see them in your template.
    }
  }
}

See this post re: how to access fields from a node object.

Note: This doesn't do exactly what you're asking, but will work if you're viewing the full page of the node with the EVA.

  • Oh god, it is so simple ... why didn't I came up with this myself? Thanks for the quick answer, this will work for my use case. I'll leave it open though to see if someone knows the "real" answer (no harm meant, you helped me a lot and your answer will work for me, it is just the right answer to the wrong question ;-) ) – Hudri Apr 3 '18 at 19:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.