Score:0

Passing parameters to view blocks in Twig

pl flag

Running Drupal 8.9.18

I have a string that I want to pass to a Drupal block for use in a contextual filter. In a twig template I have a view that creates a simple list of the terms to use. I extract the string from the result array, map the required string to a variable that I pass as a parameter to another view block:

{% set view-output = drupal_block('views_block:view1-block_1') %}

{% for item in view-output.content['#view'].result %}

  {% set qParam = item.taxonomy_term_field_data_name | render | striptags %}
  {% if drupal_block('views_block:view2-block_1', qParam )is not empty %}
    {{ drupal_block('views_block:view-name-block_1', qParam) }}
  {% endif %}

{% endfor %}

The expression item.taxonomy_term_field_data_name | render | striptags gives me the values I expect from the view output.

I get the following error:

The website encountered an unexpected error. Please try again later. TypeError: Argument 2 passed to Drupal\twig_tweak\TwigExtension::drupalBlock() must be of the type array, string given,

Question: how can I pass qParam as an array (it's a single value). I've tried using {% set qParam = qParam|merge(..) %} inside the for loop but the (..) bit is a mystery to me.

Or is there a better way of nesting queries that I don't know?

Note that I am not conversant with coding in PHP and want to stick with using Twig alone.

Thanks in anticipation for any help

Score:0
pl flag

thanks for your help. I'll try that. I've actually found a workaround using a contextual filter to identify a specific path component rather than using a query parameter. This does what I want but I'd be interested to solve the original problem

Score:0
gr flag

following the error-message you probably just use an array with a single value as second argument, e.g

{{ drupal_block('xx', {qParam: qParam} ) }}
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.