6

I have an entity reference field in one content referencing an other content.

What I'm trying to achieve is to display that reference in a teaser format programmatically.

I was hoping that I could do something like this :

{{ node.field_entity_ref.view('teaser') }}

But that doesn't work. Do I need to declare a preprocess anyway ? If so what's the best practice ?

7

The best practice would be to configure the reference field in UI as teaser and use it in the node template:

{{ content.field_entity_ref }}

If this is not an option for your case, you would probably need some code in preprocess.

But much easier is to use the module Twig Tweak, which can render nodes without preprocess:

{{ drupal_entity('node', node.field_entity_ref.0.target_id, 'teaser') }}
  • Thanks. I didn't know about Twig Tweak, that sounds like a winner ! Only thing that bother me is that key 0. Do you know how I can iterate on node.field_entity_ref ? – pbonnefoi Jul 21 '16 at 7:51

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.