0

I have a custom block type with a link field and an image field. I want to combine those fields to wrap the image in the link.

I created a Display Suite Twig field. What do I use to show the image?

Ideally the image as rendered in Manage display, but as long as I can show the image with a certain image style, I'd be very happy.

This is what I have already tried.

{{ file_url(node.field_career_highlight_image.entity.uri.value) }}
{{ file_url(content.field_career_highlight_image['#items'].entity.uri.value) }}
{{ file_url(block_content.field_career_highlight_image.0['#item'].entity.uri.value) }}
{{ file_url(node.field_career_highlight_image['#items'].entity.uri.value) }}
{{ block_content.field_career_highlight_image.alt }} - shows alt, just experimenting, so wondering why this didn't work for the image
{{ content.field_career_highlight_image }}
{{ block_content.field_career_highlight_image }} - white screen
{{ block_content.field_career_highlight_image.0 }}
{{ block_content.field_career_highlight_image.0.value }}
{{ block_content.field_career_highlight_image[0] }}
{{ block_content['#block_content'].field_career_highlight_image.value }}
{{ content['#block_content'].field_career_highlight_image.value }}
{{ content['#block_content'].field_career_highlight_image }}
0

1 Answer 1

0
{% set image_uri = block_content.field_feature_row_image['#items'].entity.uri.value %}

<img src="{{ image_uri|image_style('thumbnail') }}"/>

OR with a .0

{% set image_uri = block_content.field_feature_row_image.0['#items'].entity.uri.value %}

<img src="{{ image_uri|image_style('thumbnail') }}"/>

If not, try

{{ file_url(block_content.field_career_highlight_image.entity.fileuri) }}

If still no dice, then give this a shot. In /sites/default/settings.php add:

// Override default twig method whitelist.
$settings['twig_sandbox_whitelisted_methods'] = [
  // Defaults:
  'id',
  'label',
  'bundle',
  'get',
  '__toString',
  'toString',
  // Additions:
  'url',
];

and then in your field twig put

{{ block_content.field_career_highlight_image.url }}
1
  • This one worked: {{ file_url(block_content.field_career_highlight_image.entity.fileuri) }}. It got the original file uploaded and I understand why. Unfortunately, the nature of the platform doesn't give me access to settings.php, which is a bummer. My ultimate goal is to be able to access the version of a certain image style, but I wasn't able to figure that with your solutions. I'm going to mark your answer as accepted because you did answer my core question.
    – Sarah C
    Commented Aug 26, 2020 at 20:10

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.