Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
The image is not displaying if debug mode is true because there is wrapper of html comments like following. So we do not need the wrapper for specific fields.
We need to
Change the code from
style="background:url(<!-- THEME DEBUG -->
<!-- THEME HOOK: 'views_view_field' -->
<!-- BEGIN OUTPUT from 'core/modules/views/templates/views-view-field.html.twig' -->
<!-- THEME DEBUG -->
<!-- THEME HOOK: 'image_url_formatter' -->
<!-- BEGIN OUTPUT from 'modules/contrib/image_url_formatter/templates/image-url-formatter.html.twig' -->
http://local.oit-drupal8-new.com/sites/default/files/2016-01/id_banner_img.jpg
<!-- END OUTPUT from 'modules/contrib/image_url_formatter/templates/image-url-formatter.html.twig' -->
<!-- END OUTPUT from 'core/modules/views/templates/views-view-field.html.twig' -->)repeat"
to
style="background:url(http://local.oit-drupal8-new.com/sites/default/files/2016-01/id_banner_img.jpg)repeat"
Comment | File | Size | Author |
---|---|---|---|
#56 | 2672656-56.patch | 1.49 KB | nortmas |
#40 | 2672656-32.patch | 1.49 KB | nortmas |
#35 | twig_comment_stripper.zip | 2.66 KB | code-brighton |
#31 | 2672656-31.patch | 1.49 KB | yogeshmpawar |
#23 | Remove-HTML-Comments-From-String-2672656-23.patch | 1.53 KB | osopolar |
Comments
Comment #2
mukesh.dev CreditAttribution: mukesh.dev commentedComment #3
mukesh.dev CreditAttribution: mukesh.dev commentedComment #4
mukesh.dev CreditAttribution: mukesh.dev commentedI have created a twig filter named 'remove_html_comments' to solve this issue.
In Twig, I used this as following.
Comment #5
mukesh.dev CreditAttribution: mukesh.dev commentedComment #7
zerolab CreditAttribution: zerolab at Torchbox for Mencap commentedComment #8
frogdog_tech CreditAttribution: frogdog_tech commentedwould love to see this in core
Comment #9
Nikhilesh Gupta CreditAttribution: Nikhilesh Gupta as a volunteer and at Melity commentedComment #10
zerolab CreditAttribution: zerolab at Torchbox for Mencap commentedUnnecessary spaces
Needs data type. See https://www.drupal.org/coding-standards/docs#param
Missing return description
Comment #12
pminfInstead of introducing a new twig filter we should improve the existing filter strip_tags to remove comments. Because thats what developers are expecting – analog to PHP's strip_tags function:
Comment #13
ckaotikThe default Twig striptags filter seems to be based on the php strip_tags equivalent. Thus we can provide a list of allowed tags and can check for empty using something like this:
my_variable|render|striptags('<img><drupal-render-placeholder>')|trim|length > 0
:) Not the easiest solution, but so far it does what I need.Comment #14
mukesh.dev CreditAttribution: mukesh.dev commentedComment #15
zerolab CreditAttribution: zerolab at Torchbox for Mencap commentedTriple php start tag.
Looks like a tab.
Comment #16
mukesh.dev CreditAttribution: mukesh.dev commentedComment #17
landsman CreditAttribution: landsman commentedHello mukesh.dev!
Your patch has good idea but do same as default filter: "|render|striptags".
My show case:
{% set article_type = content.field_article_type|render|striptags %}
Returns: via: {{ dpm(article_type) }}
And your patch return same result.
We need remove these new lines "\n" ...
Comment #18
landsman CreditAttribution: landsman as a volunteer and commented----- Bad patch :( moderators please remove this comment -----
Comment #19
landsman CreditAttribution: landsman as a volunteer and commentedI was added it to our module: DrupalTwigFood
Comment #21
ay13 CreditAttribution: ay13 commentedAdding the
trim
filter will remove all the excess whitespace and new lines.{% set article_type = content.field_article_type|render|striptags|trim %}
Comment #23
osopolarThe str_replace removes to many line breaks, so that the $output is just one line. What a bout adding
\s*
to the end of the regular expression inpreg_replace
instead of using str_regreplace?#10 and #15 seems to be fixed too.
Comment #25
bdanin CreditAttribution: bdanin commented#21, |render|striptags|trim worked very well for me, many thanks!
Comment #26
AdamPS CreditAttribution: AdamPS at AlbanyWeb commentedComment #27
maxplus CreditAttribution: maxplus commentedThanks,
working with |render|striptags|trim also worked for me to render a field inside a paragraph used as css class.
Without the |render|striptags|trim the complete theme debug code cluttered the rendered field and broke my html during development...
Comment #29
robbdavis CreditAttribution: robbdavis commented#21 worked for me also; although, I did not need the "| render |" filter. Code to get pure url looks like this:
Gotta say that this was a super annoying issue to deal with; I hope the fix gets added to core soon.
Comment #30
yogeshmpawarComment #31
yogeshmpawarRe-rolled the patch #23 against 8.6.x branch.
Comment #32
lpeabody CreditAttribution: lpeabody commentedOne thing to note with striptags is that it loves to html encode strings. So if you end up outputting a URL with ampersands in a query string it's going to break the query string portion. This is where striptags breaks down for me. In particular this happens when I'm outputting an image with a cache busting token + a focal point token. So I need an alternative solution to striptags in this instance.
Comment #33
lpeabody CreditAttribution: lpeabody commentedWhat if this was solved by a sort of whitelist/blacklist where you could specify entity type, bundle, view mode, and field name to not output debug commentary on? Thoughts?
Comment #35
code-brighton CreditAttribution: code-brighton commentedFor anyone like me wanting this functionality and not sure whether to go for this patch until things have been settled on. I created a little module using the method in the patch (thanks folk)
1. Install module
2. Add something like this in your twig
e.g.
Comment #36
pminfDon't know when or how this was fixed but using Drupal 8.7.0-dev I can get rid of all html tags including comments by just using
striptags
. I did not applied any of the patches posted here. At the time this issue was createdstriptags
was not removing html comments.Normal output without
striptags
:Plain output with
striptags
:Plain and trimmed output with
striptags
andtrim
:Foo: [42]
Comment #37
pminfComment #38
mlncn CreditAttribution: mlncn as a volunteer and at Agaric for Drutopia, Portside commentedThere should be a way to override debug comments on a per-field basis without stripping tags but, yeah, that never directly came up in this issue so i'm just leaving this comment as moral support for anyone who wants to open a new issue about that!
Comment #40
nortmas CreditAttribution: nortmas commentedAlso, remove line breaks.
Comment #41
OnkelTem CreditAttribution: OnkelTem as a volunteer commentedMy few cents.
This issue is just crazy. Really.
What you guys do seems as a sort of nonsense. Instead of picking the source values of the fields, you first render them and then trying to get rid of commens, eh?
You'd laugh but I came here because I have similar issue. But with views. With damned views which just don't provide source values for the fields in 'views-view-fields' templates. If you're not coupling with Views like me atm, try to borrow source values of the fields, not from their rendered result.
Comment #42
chharvey CreditAttribution: chharvey commentedThis worked for me:
See the docs for the
|striptags
and|trim
filters.Comment #43
pminf@OnkelTem (#41) Of course using the source value is a lot cleaner but sometimes it does not output the desired format. Think about reference fields to images for example: By using the source value you would get the entity id instead of the url and you would need to build up the correct path to the image style file with twig functions by yourself (although it has already been done "by core").
Comment #44
hawkeye.twolf@mlncn, re:
Isn't that exactly what this issue is about? I believe this issue should be re-opened, as stripping HTML comments without stripping the rest of the rendered tags is important.
BTW, for those who do use
striptags
, make sure you whitelistdrupal-render-placeholder
, in addition to any other tags your content might contain. E.g.,Comment #45
robert_t_taylor CreditAttribution: robert_t_taylor commented#21 simplified an issue I was having trying to escape a variable to run it through a custom filter. Thanks!
Comment #46
orlando.thoenySince this issue was created in 2016, and it seems like it might take a while (or it will never happen) for this to land in core.
I used the patch and created a module out of it: https://www.drupal.org/project/twig_remove_html_comments
Comment #47
Alen Simonyan CreditAttribution: Alen Simonyan as a volunteer commentedFor example in views template you can use -
{{ fields.field_name.content | striptags | trim }}
, no patch needed I think:In this case
| striptags
cuts all tags including HTML comments.For Images you can use -
{{ fields.field_author_image.content | render | striptags('<img>') | trim | raw }}
:In this case
| striptags
cuts all tags except<img>
tagComment #48
Martijn de WitReferring to meta issue. #953034: [meta] Themes improperly check renderable arrays when determining visibility.
Didn't know there was a "similar" discussion.
Comment #49
hawkeye.twolf@Alen Simonyan, note that striptags does not suffice as we need a generic way to remove HTML comments without also removing other tags, even when we don't know those tags will be.
Comment #50
bdanin CreditAttribution: bdanin commentedThe drupal/twig_remove_html_comments from #46 doesn't work well because it doesn't work for arrays. I've found that using
entity.field_name.value
tends to work quite well. For example in a node template, I usenode.field_name.value
or in a paragraph templateparagraph.field_name.value
.Comment #51
orlando.thoeny@bdanin please create an issue in the module issue queue if you need that functionality.
However I only intended it to be a temporary solution (preferably). Until something for this issue is worked out for Core.
Comment #52
hawkeye.twolf@bdanin, printing node data directly in field templates is not advised, both for security and because it cuts off functionality provided by field formatters, etc. Use render arrays provided by the drupal render API wherever possible.
The solution is to call
|render
on your render array before passing it to|remove_html_comments
. E.g.,Comment #53
bdanin CreditAttribution: bdanin commented@hawkeye.twolf, thanks for this info! @orlando.thoeny, I've created an issue in the module queue.
Comment #54
draganFSD CreditAttribution: draganFSD commentedInstead of
you can put just empty field...
{{ article_type|replace({'\r\n': '<br>'}) }}
Comment #55
cswan CreditAttribution: cswan commented#21, |render|striptags|trim worked for me. Thank you!
Comment #56
nortmas CreditAttribution: nortmas commentedNew patch for the core 9.4.5.
Comment #57
camslice CreditAttribution: camslice commentedIMHO opinion this filter is needed, however it should target Twig specifically and properly process HTML entities.
Can we please re-open this issue?
In its current form, the result of
|remove_html_comments
is the same as|striptags|trim
.For example, take a field value of "Twelve O'Clock".
Both
|striptags|trim
and|remove_html_comments
output the same result:I suggest 2 changes to make this filter worthwhile in its own right:
|strip_twig_debug
or maybe|remove_twig_debug
If processing HTML entities feels too invasive, we could always pass an optional argument allowing people to prevent HTML entity processing.
Currently, the only viable option is the twig_htmlspecialchars_decode module, resulting in quite a long and clumsy filter chain ':[]
Comment #58
Martijn de Wit@camslice did you try the https://www.drupal.org/project/twig_field_value module?
Comment #59
scott_euser CreditAttribution: scott_euser as a volunteer and at Soapbox Communications Ltd commentedYou can also use https://www.drupal.org/project/twig_remove_html_comments which removes the html comments without removing the actual markup inside the nested template. I have suggested an update to the module homepage as it is outdated referencing this issue #3421810: Module homepage description is in need of an update with incorrect information.
Comment #60
quietone CreditAttribution: quietone at PreviousNext commented@scott_euser asked that the status of this issue be change to 'closed won't fix'.
I have searched the issue and there is no commit here. I skimmed through the comments and do not see anything, like a documentation change, to suggest that this has been fixed. #36 closed this as outdated and the next comment changed the status to 'Fixed'. That was the mistake. If I knew the issue where this was fixed there is a possibility that this could be closed as a duplicate and credit transferred. But, there is no reference to another issue so 'closed outdated' is the status to use.
Also, noting that there are comments suggesting the use of contrib projects, #57, #58, #59
I am restoring the status from #36
Edit: s/know/knew/
Comment #61
scott_euser CreditAttribution: scott_euser as a volunteer and at Soapbox Communications Ltd commentedThanks @quietone. In case people come across this issue because of debug comments added within formatted text, I separately raised #3421843: filter_autop should ignore twig.config debug html comments with a merge request proposing a solution.