I have node--article--teaser.html.twig which calls image-formatter.html.twig to printout the field_image. Everything works fine except that I don't want the field_image itself to be a linked. i.e image-formatter.html.twig should not find a url to only print an img tag.

I have tried the following in node--article--teaser.html.twig:

{#{ content }#}
  {% set content.field_image[0]['#url'] = null %}{% endset %}
  {{ content.field_image }}
    {{ label }}

The second line causing the following error:

Twig_Error_Syntax: Unexpected token "punctuation" of value "." ("end of statement block" expected) in "themes/saidbakr/templates/node--article--teaser.html.twig" at line 78. in Twig_TokenStream->expect() (line 87 of vendor\twig\twig\lib\Twig\TokenStream.php).

While {{ content.field_image[0]['#url'] }} prints the url without any problems!

The image-formatter.html.twig code is as the following:

{% if url %}
  <a href="{{ url }}">{{ image }}</a>
{% else %}
  {{ image }}
{% endif %}

There is no any suggestion from twig debug about any alternative template for image-formatter.html.twig

Also using Theme Hook image_formatter as saidbakr_preprocess_image_formatter will not be an option because it is not possible to know the view context, i.e teaser or full, or content types to restrict this interaction for articles type. I think that the most simple way is from Twig, but how?!


There should be no reason to change any code for this, as you are using the default image formatter. You should be able to remove the link by going to /admin/structure/types/manage/article/display/teaser and change your field's formatter settings using the cogwheel-button and then select "None" for "Link image to".

Should the teaser view mode configuration page not be available, got to /admin/structure/types/manage/article/display and activate its checkbox on the far bottom of the page, then save and try again.

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.