1

I'm on Drupal 8 and I have a view with multiple argument (list of node ID) but I can't embed this on my node.twig.html.

That's my code:

{% set myArray = [] %}
{% for item in content.field_articles_lies['#items'] %}
    {% set myArray = myArray|merge([item.entity.nid.value]) %}
{% endfor %}

{{ drupal_view('articles_lies','default', myArray()) }}

if I do that: {{ drupal_view('articles_lies','default', myArray.0) }} it's ok but obviously only for one.

How can I pass an argument with multiple values to a view rendered via drupal_view() in a template?

0
1

You can't pass an array. You have to implode the array to a string first. And use '+' as glue for OR and ',' as glue for AND. Like '1+2+3' or '1,2,3'. And maybe you also have to enable Allow multiple values in your view (in the very bottom of the contextual filter screen).

I have try that and it works but I'm not sure it's very clear:

{% set arguments = '' %}
{% set i = 0 %}
{% for item in content.field_articles_lies['#items'] %}
    {% if i == 0 %}
        {% set arguments = item.entity.nid.value %}
    {% else %}
        {% set arguments = arguments ~ "+" ~ item.entity.nid.value %}
    {% endif %}
    {% set i = i + 1 %}
{% endfor %}

{{ drupal_view('articles_lies', 'default', arguments) }}

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.