0

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

2 Answers 2

1

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

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

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

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.