passing arguments to drupal_view() via a variable.

Created on 20 August 2018, almost 6 years ago
Updated 28 January 2023, over 1 year ago

How do I pass an argument to drupal_view() as a set variable.

{{ drupal_view('view_name', 'default', termid) }} <-- this doesnโ€™t work.
{{ drupal_view('view_name', 'default', '123') }} <-- this works.

termid is set by {% set termid = content.field_program_select|field_value|raw %} which outputs a number. It appears to be a nice clean plain text number in the browser when I {{ termid }}.

But I get this error...

Warning: array_flip(): Can only flip STRING and INTEGER values! in Drupal\Core\Entity\EntityStorageBase->loadMultiple() (line 227 of core/lib/Drupal/Core/Entity/EntityStorageBase.php).

When I use {{ drupal_view('automated_gallery_by_program_term', 'default', termid) }}

Help?

Thanks!

๐Ÿ’ฌ Support request
Status

Closed: works as designed

Version

2.1

Component

Code

Created by

๐Ÿ‡บ๐Ÿ‡ธUnited States alphex Atlanta, GA USA

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States thalemn

    This thread was very helpful for my situation too. I embedded a view via twig tweak and needed to pass a taxonomy id value to the view contextual filter. The field from my content (commerce store) is a taxonomy field to toggle the use of Regular or Special/Holiday hours: store.field_hour. I set that field as a variable to use in the view: {% set hourActive = store.field_hour[0]['#plain_text'] %}. So the embedded view looks like this: {{ drupal_view('hours', 'block_1', hourActive ) }}.

    The part that I was missing was the ['#plain_text'] at the end of the field for the variable.

    The other aspect of this solution was configuring the view contextual filter. After some experimenting, the solution that worked for me was to configure the contextual filter as: When the filter value is NOT available: Display contents of "No results found".

    I also had to create a taxonomy relationship in the view, so the same taxonomy field used in the store (to toggle Regular or Special/Holiday hours), is used in the Hours content type to connect the two via the view contextual filter.

    The result: a user can edit the store, select Regular or Special/Holiday hours, and easily switch between the two. I hope this helps others who have a similar situation.

Production build 0.69.0 2024