1

Inside my paragraph template I have variables which contain category id and view machine name. So when I print them like this:

  {{ content.field_category }}
  {{ content.field_view }}

I get correct values.

Then I'm using twig tweak to embed view and if I embed it with hard-coded values, like this:

  {{ drupal_view('view_machine_name', 'embed_1', 33) }}

it also works. I hard-coded the same values my variables display from above.

Now I want to use my field values instead of hard-coded values:

{{ drupal_view(content.field_view, 'embed_1', content.field_category) }}

I get error like:

Warning: array_flip(): Can only flip STRING and INTEGER values! in Drupal\Core\Entity\EntityStorageBase->loadMultiple() (line 266 of core/lib/Drupal/Core/Entity/EntityStorageBase.php). Warning: Illegal offset type in isset or empty in Drupal\Core\Entity\EntityStorageBase->load() (line 251 of core/lib/Drupal/Core/Entity/EntityStorageBase.php). 

when I try to replace view parameter and:

InvalidArgumentException: Placeholders must have a trailing [] if they are to be expanded with an array of values. in Drupal\Core\Database\Connection->expandArguments() (line 738 of core/lib/Drupal/Core/Database/Connection.php).

When I try to replace category parameter.

I guess that content.field_category is not an int value and content.field_view is not an string, as this twig filter expects but I have no idea how to convert them to those primitive types?

Tried intval - it's not working

Tried number_format - also not working

I guess I would have to somehow get view machine name and category id directly from those objects but I don't know how to achieve that.

Tried content.field_category.id() and content.field_view.id()

Nothing works.

7
  • 2
    What template exactly? Enable Twig Debugging, install the Devel sub-module Kint and then put {{ kint() }} in your template for having all available variables pretty-printed on screen and inspect them. It probably needs to be {{ content.field_view.value }} or {{ content.field_view.0 }} (same for the cat field) for printing your field values.
    – leymannx
    Commented Apr 14, 2020 at 11:41
  • Possible duplicate of drupal.stackexchange.com/q/228388/15055
    – leymannx
    Commented Apr 14, 2020 at 11:44
  • 1
    You can't pass the render arrays, you need to pass the raw values.
    – Kevin
    Commented Apr 14, 2020 at 12:30
  • 1
    {{ drupal_view(content.field_view, 'embed_1', content.field_category.0) }}
    – No Sssweat
    Commented Apr 14, 2020 at 17:50
  • For a paragraph template see drupal.stackexchange.com/a/263285/47547
    – 4uk4
    Commented Apr 15, 2020 at 7:07

1 Answer 1

0

Try to avoid using {{ content }} this way. The position you have found in the render array in your last comment can change anytime when reconfiguring the paragraph type or when installing/updating code. Render arrays are not APIs and they are only required to have valid renderable child elements at the lowest level, but not a fixed structure.

The Entity API on the other hand, which you would use to access {{ paragraph }} in a paragraph template, does not change, at least not without a transition period while you can still use a deprecated API.

Example:

// field value like string or number
{{ paragraph.field_example.value }}

// target id of a reference field
{{ paragraph.field_reference.target_id }}

See also https://drupal.stackexchange.com/a/263285/47547.

1
  • Great! This works like a charm. I don't know is it me or things like this are hard to find... :/
    – MilanG
    Commented Apr 16, 2020 at 6:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.