I'm currently in the middle of trying to figure out a way to pass the value from a variable to be used as parameter/argument for my drupal view.

I have a contextual filter that currently configured on my drupal view to output only nodes based on the taxonomy term/ID. Instead of explicitly manually setting the parameters below.

{{drupal_view('view_one', 'block_1', 212, 210)}}

Is there a way I could use a variable to pass the parameters to my drupal view

 % for tags in node.field_filter_tags %}

    {% set tagid = tags.entity.id %}

{% endfor %}

{{drupal_view('view_one', 'block_1', tagid)}}

I tried it but it seems to show duplicates.


the first you install module Twig Tweak

In file yourtheme.theme

 * Implements hook_preprocess_HOOK() 
function yourtheme_preprocess_node(&$variables) {
  foreach($variables['node']->field_tags->getValue() as $term){
    $terms[] = $term['target_id'];
  $variables['node_terms'] = implode(',',$terms);

In file node.html.twig

{{ drupal_view('custom_view','article_block',node_terms) }}

Config Views CONTEXTUAL FILTERS in your views enter image description here

| improve this answer | |
  • Thanks for this, the only issue I'm currently experiencing that it seems to be rendering duplicates. I have already enable Distinct in the Query Settings – clestcruz Aug 20 '18 at 10:11

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.