I have a site design the i need to implement. in this design I have a grid and in the grid I can put any content (node/paragraph) that i choose. the image of the content changes size and ratio in relation to its location in the grid. I created display modes for the nodes and i use twig tweak to render it in the right display mode for example:

{{ drupal_entity('node', content['#node'].id , 'box_grid_1x2') }}

for the paragraphs I add my image style directly in the paragraph template:

{{ content.field_image.0 | merge({'#image_style': 'box_grid_1x2'}) }}

because its a little more dynamic than this and my 'box_grid_1x2' is actually a variable that can be set by the user I want to verify that my image styles of view modes do exist before printing them and if they dont I want to print a fallback display\image style.

how would you do it? Thanks for your help.

1 Answer 1


You can check image style existence in a preprocess hooks like follows.

$variables['image_style_exists'] = (bool) ImageStyle::load($style_name);

Though it's better to validate user input instead. Note that configuration entities like image styles or views can be selected through entity reference field which may perform such a validation for you.

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.