I have the problem, that the theme / template suggestion comments destroy the html when they are outputted as values.

{% set img_uri = "fields.img.value"%}

{{ dpm(img_uri)
style="backround:url({{ createMyImagePathCustomTwigFunction(img_uri) }});"

Results in:

<!-- THEME DEBUG -->
<!-- THEME HOOK: 'views_view_field' -->
<!-- BEGIN OUTPUT from 'core/themes/stable/templates/views/views-view-field.html.twig' -->
<!-- END OUTPUT from 'core/themes/stable/templates/views/views-view-field.html.twig' -->

<!-- END OUTPUT from 'core/themes/stable/templates/views/views-view-fields.html.twig' -->

..as wrong uri.

I know that I could dive deeper into the rendered array to find other versions.

However: To change production templates in general only for enabling a developing environment seems wrong to me.

Better would be a hook that would remove field tpl suggestions in general as I don't need them.

I know I can write handler functions... that is what I do in the moment.

But cleaner would be to turn the suggestions off for views fields. How this can be done?

  • How did you turn them on? Apart from that, they should be disabled at all in production environments.
    – leymannx
    Aug 30, 2018 at 10:06
  • Sure but they are wrong in dev. As you read in my question I would have to create handler functions which are just used in dev. In Production theme suggestions are anyway not used. My question is not how to turn them off in general - of course I know that. ;) I need to turn them off for views fields only - so that I don't have to create a twig "filter comments out"- function that wraps all values as they might have or not have comments in them.
    – mogio
    Aug 30, 2018 at 10:10
  • In this moment I create overhead in production.That is something I want to avoid.
    – mogio
    Aug 30, 2018 at 10:13
  • 1
    Just have seen this: drupal.org/project/drupal/issues/2672656 Seems that this is the way to go... hell no :)
    – mogio
    Aug 30, 2018 at 10:35
  • 1
    Just make the url a string as he said. Far easier.
    – Kevin
    Aug 30, 2018 at 12:50

4 Answers 4


You try this code :

{% set img_uri = "fields.img.value"%}

{{ dpm(img_uri)
style="backround:url({{ createMyImagePathCustomTwigFunction(img_uri) |render|striptags}});"

use |render|striptags


As it seems, that there is no solid alternative that works for field values I have written a Twig-Filter which returns the value via this php function:

public function removeHtmlComments($string) {

  $output = preg_replace(array('/<!--(.|\s)*?-->\s*/', '/\t+/', '/\n+/', '/[\r\n]/', '/[\n\r]/'), '', $string);

  return $output;

For me what worked well in the Twig was to use : field | render | striptags | trim

  • striptags : remove the suggestions (or any other html tag)
  • trim : remove the white-spaces left after striptags

Try the following.

{{ file_url(fields.img.content|striptags|trim) }}

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.