2

I'm trying to create a twig layout for a Person content type that is be used within Paragraphs. My twig file is paragraph--person-profile--default.html.twig.

I can get the Person name with {{ content.field_person[0]['#node'].title.0.value }}

I cannot get the image. I've tried the following:

{{ content.field_person[0]['#node'].field_image.0.value }}

and {{ content.field_person[0]['#node'].field_image.0 }}

The following error is reported.

Exception: Object of type Drupal\image\Plugin\Field\FieldType\ImageItem cannot be printed. in Drupal\Core\Template\TwigExtension->escapeFilter()

The end game is to have a taxonomy term and a person content type within a paragraph type. The taxonomy term will be used to to style the person. The same person may be styled differently on different pages.

2 Answers 2

1

To get the url from an image field you have to go one level deeper, .entity gets the file entity where the URI of the image is stored:

{{ file_url(content.field_person[0]['#node'].field_image.0.entity.uri.value) }}

But digging through multiple levels of nested entities is not good practice, because this produces code hard to debug and maintain. You should look for a better place to theme the image, for example node--person.html.twig, where you can print the image field, formatted how it is configured in UI:

{{ content.field_image }}
1
  • Thank you. Yes, I had gotten lost in the trees and couldn't see the forest. I ended up being about to get the styling field I wanted from the paragraph. I then had to go to the person template to do the other styling. <div class="person_profile" class="{{ content.field_display_type[0]['#title'] }}"> {{ content.field_person.0 }} </div>
    – bart
    Feb 14, 2018 at 13:17
0

Hi bart please refer the below link, it will be helpfull for getting the full URL of the image.

After that you can use Image Tag for rendering the image

Rendering Image Field

Your Answer

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

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