0

Entity

  • Type : block
  • Field : 'Reference to entity : image style' (field_image_style)
  • Manage display : I display the Entity ID

enter image description here

Twig

In my twig template, I get the field_image_style data from content.field_image_style.

Issue

Works great on admin user, but not when browsing the website as an anonymous user.

This is not a image style generation error, or a wrong permission to files folders : you can access to both original image and generated styled image on anonymous.

And this is where it gets weird. It's working when you give the "Administer image styles" permission to anonymous users.

enter image description here

Dump

Just to show you what result I have, here's a dump of content.field_image_style with an admin user :

enter image description here

And with an anonymous user :

enter image description here

  • Your question is unclear about what you are trying to do. This part "This is not a image style generation error, or a wrong permission to files folders : you can access to both original image and generated styled image on anonymous." suggests you want to display a styled image, the rest of the question is about how to display the id of an image style config entity. – 4k4 Nov 23 '17 at 9:48
  • I know how to display an image with a specific image style. In my twig template, I want to get the image style id if it exists, and display my image with this image style. The problem is I have access to image style field only on admin user. – Corentin Le Fur Nov 23 '17 at 9:56
  • But how does the rendered output of the config entity id helps to display an image? – 4k4 Nov 23 '17 at 10:03
  • Like that : i.stack.imgur.com/s9G55.png (I use the Twig Tweak module for the image_style twig filter – Corentin Le Fur Nov 23 '17 at 10:13
  • @4k4 It is a reference to an entity, so it's like it can't display an entity (the image style in this example) if it does not have the permission to see this entity. It might work well for taxonomy terms or nodes, but I can't give access to image style administration for anonymous users "just" for that. Maybe i'm wrong or I'm missing something, but this is how I see it for now. – Corentin Le Fur Nov 23 '17 at 11:09
1

The purpose of a field configured in a view mode is to render the output.

To process field values in code use the entity object. You'll find this in a block template in this variable:

$variables['elements']['content']['#block_content']

So in TWIG you can try to access the field value like this:

{%
  set style = elements.content['#block_content'].field_image_style.target_id
%}
| improve this answer | |
  • Works like a charm, thank you ! I just don't get why you can't use the content variable like in any other field. Maybe I'm wrong but I usually look at the available variables for a template, and I don't see a reference of elements. – Corentin Le Fur Nov 23 '17 at 12:05
  • 1
    It's hard to explain this in one comment. content is the main content of the block. As always in Drupal this means this is a render array. Now coding with a render array is no fun, because it is a moving target. It changes a lot, not only because of access rights, because it only is required to be renderable. Even the position of the fields is not fixed (install a layout module). So if you want to write code it's better to use an API which doesn't change and for this entites are perfect. – 4k4 Nov 23 '17 at 12:55
  • 1
    Now the problem here is, block_content has not a template of its own, which probably would have a documented entity, like node in a node template. The generic block template covers all blocks, not only the ones for custom blocks and so there is no specific variable for this entity. – 4k4 Nov 23 '17 at 13:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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