How to apply imagen styles

Created on 12 September 2023, 10 months ago
Updated 19 October 2023, 8 months ago

Problem/Motivation

This is something that just happened to me. I have an image field and I call it with a token in the ace editor and everything works very well, but how do I apply image styles to that field?

πŸ“Œ Task
Status

Closed: works as designed

Version

4.0

Component

Code

Created by

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

Comments & Activities

  • Issue created by @Nelo_Drup
  • πŸ‡ΊπŸ‡ΈUnited States bobbysaul

    Hello,

    I guess it would depend on how you are rendering the image. I would recommend the Twig Tweak module and use one of the filters provided with that module, drupal_image β†’ or image_style filter β†’ .

    Something like one of the following should work:

    <img src="{{ node.field_media.entity.field_media_image|file_uri|image_style('thumbnail') }}" />
    
    {{ drupal_image(node.field_media.entity.field_media_image|file_uri, 'thumbnail') }}
    
  • I do it more or less this way:
    [paragraph:field_tipparr_tipbloq_reshom_img]
    And yes it works for me but how could I do the same with twig because I only get errors or where did I add the styles?

  • πŸ‡ΊπŸ‡ΈUnited States bobbysaul

    Using the token [paragraph:field_tipparr_tipbloq_reshom_img:[image style machine name here]] will return the url to the image with the image style specified on it. For example if your image style is "medium" something like this would render the image:

    <img src="[paragraph:field_tipparr_tipbloq_reshom_img:medium]" alt="[paragraph:field_tipparr_tipbloq_reshom_img:alt]" />
    

    You can see your image styles at /admin/config/media/image-styles assuming the image style module is installed.

    If you want to do the same thing but with twig instead of tokens you can do something like this:

    <img src="{{ paragraph.field_tipparr_tipbloq_reshom_img|file_uri|image_style('medium') }}" alt="{{ paragraph.field_tipparr_tipbloq_reshom_img.alt }}" />
    
  • πŸ‡ΊπŸ‡ΈUnited States bobbysaul

    The same can also be done with the Twig Tweak view filter β†’ as such:

    {{ paragraph.field_image | view({settings: {image_style: 'medium'}}) }}
    
  • Status changed to Postponed: needs info 9 months ago
  • πŸ‡ΊπŸ‡ΈUnited States bobbysaul

    Hello @Nelo_Drup. Did the above suggestions work for you? Is there any work that needs to be done for this issue?

  • @bobbysaul Yes, thank you very much, it worked perfectly.

  • Status changed to Closed: works as designed 8 months ago
  • πŸ‡ΊπŸ‡ΈUnited States bobbysaul

    Marking this issue as closed.

Production build 0.69.0 2024