0

I'm attempting to render an array of LibraryItems in a twig template but I'm having trouble figuring out how to retrieve the render array.

Currently my twig template looks like this

{% set paragraphs = node.getParagraphs() %}

{% for paragraph in paragraphs %}
{{ paragraph }}
{% endfor %}

But this gives me the error

Exception: Object of type Drupal\paragraphs_library\Entity\LibraryItem cannot be printed

How would I turn the LibraryItem entity into something printable? I'm willing to do this either on the twig side or in a hook but it would be helpful to know how to do both.

1
  • 1
    Output the paragraph fields as their default field formatter in the node "Manage Display" settings, then theme the paragraphs normally using their Paragraph twig files.
    – Kevin
    Commented Jul 17, 2022 at 22:19

2 Answers 2

2

Since you want to know both, here is a Twig solution:

{{ paragraph|view }}

This is using the view filter of the module Twig Tweak.

In a multilingual site you need to translate the entity:

{{ paragraph|translation|view }}
0

I was able to accomplish this by using the ViewBuilder like so

$paragraphsRender = \Drupal::entityTypeManager()
  ->getViewBuilder('paragraphs_library_item')
  ->viewMultiple($paragraph_ids);

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.