5

In Drupal 8, I'm using Twig Tweak to embed a view. I have a contextual filter set to one of the node's fields, field_project_id. If I use a literal for the third argument, it works. If I try to reference the value of the current node's project ID, no luck.

Works:

{{ drupal_view('my_view', 'block_1', '2011-40054-000') }}  

Doesn't work:

{{ drupal_view('my_view', 'block_1', content.field_project_id) }},  
{{ drupal_view('my_view', 'block_1', content.field_project_id.value) }},  
{{ drupal_view('my_view', 'block_1', content.field_project_id.0) }},  
{{ drupal_view('my_view', 'block_1', content.field_project_id.0.value) }},  

etc.

What's the proper way to do this?

4
  • What type is field_project_id?
    – ya.teck
    Commented Jul 4, 2018 at 17:37
  • Is this a node template?
    – 4uk4
    Commented Jul 4, 2018 at 19:16
  • field_project_id is Text (plain) and yes, this is a node template.
    – timwilson
    Commented Jul 4, 2018 at 20:28
  • 1
    Then use the node object node.field_project_id.value. {{ content }} is for rendering and node is for coding, see drupal.stackexchange.com/questions/198124/…
    – 4uk4
    Commented Jul 5, 2018 at 6:40

2 Answers 2

0

First of all check drupal_view implementation but I think that the arguments parameter (contextual filters) is an array, though the function wraps scalars into an array you should pass an array (robustness). Said that everything looks fine in your template perhaps you should do {{ vardumper(content) }} to see what actually is getting to your template variable. In order to use the vardumper function you need to install the VarDumper module.

4
  • Where should I look for drupal_view implementation? I've scoured all the Twig Tweak info I can find.
    – timwilson
    Commented Jul 4, 2018 at 22:56
  • It's in the TwigExtension class but it just an alias of views_embed_view function.
    – d70rr3s
    Commented Jul 4, 2018 at 23:03
  • content.field_project_id is, I believe, a render array for the field. A dump of that gets array(1) { [0] => array(3) { '#type' => string(15) "inline_template" '#template' => string(17) "{{ value|nl2br }}" '#context' => array(1) { 'value' => string(14) "2011-40054-000" } } } If I put the literal into a var and dump that, it's just string(14) "2011-40054-000". The string works, the array doesn't. Seems that the function wants a string and doesn't know how to extract it from a render array, and I don't either. Can you tell I'm kinda new to this stuff?
    – timwilson
    Commented Jul 4, 2018 at 23:29
  • A view argument cannot be a render array for obvious reasons. If is a render array you can fetch the value but thats kind of quick dirty hack. The problem here is that you dont need to call for the view here, instead you should do it in a preprocess hook. But first where is your content var coming from? As it is an inline-template, typically this done by you or a contrib module as the core dont use that render element type quite often.
    – d70rr3s
    Commented Jul 5, 2018 at 7:12
0

You're treating {{ content }} as if it were an entity leveraging Entity API, but it's not - it's a render array.

It sounds like you're doing this in a node template, so you do actually have the entity as {{ node }} which is accessible using Entity API conventions. So this should probably work:

{{ drupal_view('my_view', 'block_1', node.field_project_id.value) }}

Your Answer

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

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