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 vendortwigtwiglibTwigTokenStream.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?!