When twig debug is turned on the theme hook suggestions are inserted in the templates.
However the insertion of the comments breaks the XML standard.
Implications
---------------
The feed does display with comments in the Views preview pane. However the situation is not ideal as the RSS feed is rejected by many external tools. In particular a developer cannot test integration with third-party services such as IFTTT.
Furthermore the transformRootRelativeUrlsToAbsolute step gets skipped so the feed may contain relative URLs.
Steps to reproduce
---------------
- create a new drupal project
- turn on twig debug
- visit the default frontpage feed page (/rss.xml)
Example current feed contents
--------
<!-- THEME DEBUG -->
<!-- THEME HOOK: 'views_view_rss' -->
<!-- BEGIN OUTPUT from 'core/themes/classy/templates/views/views-view-rss.html.twig' -->
<?xml version="1.0" encoding="utf-8" ?>
<rss version="2.0" xml:base="http://drupal8rsstwigerror.dd:8083/rss.xml" xmlns:dc="http://purl.org/dc/elements/1.1/">
<channel>
<title>Drupal 8</title>
<link>http://drupal8rsstwigerror.dd:8083/rss.xml</link>
<description></description>
<language>en</language>
</channel>
</rss>
<!-- END OUTPUT from 'core/themes/classy/templates/views/views-view-rss.html.twig' -->
Proposal
------
It is the initial comments before the xml tag that cause the problem. The feed should work if these were moved after the xml tag. The new code should only run if twig debug is enabled.
Comment | File | Size | Author |
---|---|---|---|
#43 | reroll_diff_28-43.txt | 997 bytes | ravi.shankar |
#43 | 2796453-43.patch | 1.08 KB | ravi.shankar |
#28 | 2796453_rss_output_not_valid_twig_debug.patch | 1.08 KB | Anonymous (not verified) |
Comments
Comment #2
cilefen CreditAttribution: cilefen commentedComment #4
cilefen CreditAttribution: cilefen commentedThe patch was created outside of Drupal's base directory.
Comment #5
tjwelde CreditAttribution: tjwelde at Thunder commentedBut the proposed solution would be fine?
Comment #6
joelpittetI'm not totally sure about the proposed patch yet but it is a clever trick to fix it. What is the error that we would see with this. Is it only when Twig debug is on?
Could you add the error and note about needing twig debug to the issue summary?
I think we need more information on how this bug affects people to know how to review it.
Comment #7
mike_pol CreditAttribution: mike_pol commentedI think the description can be updated to illustrate the problem:
When twig debug is turned on the theme hook suggestions are inserted in the templates. However the insertion of the comments breaks the XML standard. To replicate:
The result would be:
<?xml version="1.0"?>
Apply the patch, the output becomes:
What would be the best approach to solve the issue though?
Comment #8
joelpittetI was thinking html comments are valid but I'm guessing to be clear you are saying they are not valid on the first line?
What problems will this validity cause in a real world scenario?
Twig debug is only a dev/debug so wouldn't it be fine to leave this but note some of its shortcomings somewhere instead of introducing extra logic to circumvent the default output in debug mode?
Maybe still missing something, thanks for the reply.
Comment #9
mike_pol CreditAttribution: mike_pol commentedI agree with you 100%, I don't think that it is the responsibility of RssResponseRelativeUrlFilter.php to deal with the comments. I was just adding to the description. I think -- breaks XML comments https://www.w3.org/TR/REC-xml/#sec-comments
Thanks
Mike
Comment #10
tjwelde CreditAttribution: tjwelde at Thunder commentedComment #11
tjwelde CreditAttribution: tjwelde at Thunder commentedComment #12
tjwelde CreditAttribution: tjwelde at Thunder commentedWhen you are working on a custom rss feed, it gets in the way of debugging the used templates.
Twig debug would be unusable in that scenario.
Nevertheless, I think it would be ok, if the first comment is missing, if it would be documented.
Comment #13
tjwelde CreditAttribution: tjwelde at Thunder commentedComment #14
ckaotikHow about simply ensuring the
<?xml?>
tag is output first? Something like this might do the trick:Resulting output:
Comment #15
tjwelde CreditAttribution: tjwelde at Thunder commentedYes, that would work, but also wouldn't represent the truth.
It begins the output with
<?xml ...
not with<rss ...
Comment #16
ckaotikBut isn't the
xml
tag the one that must be output first? That's what the error message states,XML declaration allowed only at the start of the document
.The whole feed basically is "just an xml", and the
rss
tag is the first node within that file.Comment #17
lias CreditAttribution: lias commentedSo this is a not fix?
I'm just starting to really dig into 8x themes and initially thought this was an error in my rss view and I wouldn't be surprised if other folks have the same impression.
@ckaotik - how would you implement your suggestion #14? It's a really annoying message I'd like to eliminate. Thx
Comment #20
FeyP CreditAttribution: FeyP as a volunteer and at werk21 commentedThis looks like a duplicate of #2801097: Converting feed to absolute URLs fails on invalid XML, results in empty output.
Comment #21
AdamPS CreditAttribution: AdamPS at AlbanyWeb commentedI don't think this is a duplicate.
As far as I understand it, #2801097: Converting feed to absolute URLs fails on invalid XML, results in empty output has improved the error handling when there is invalid XML. It just happens that the example case given in the issue summary was twig debug.
This issue is about the fact that turning on twig debug should not lead to invalid XML.
Comment #22
AdamPS CreditAttribution: AdamPS at AlbanyWeb commentedHere is another scenario.
Twig debug also confuses any views that have tests if fields are empty. Here are some ideas that could help
Comment #23
AdamPS CreditAttribution: AdamPS at AlbanyWeb commentedSorry I should be clearer. After #2801097: Converting feed to absolute URLs fails on invalid XML, results in empty output, good news is that the rss feed is visible in the view preview with twig debug. The original patch by @tjwelde is no longer necessary as similar code was committed in the fixed issue.
However the situation is not ideal:
The approach described in #14 would solve the above problems and seems like it would be useful.
#22 describes a related but separate bug which I could convert to a separate issue. In #22 I also mentioned a problem about views and empty fields which is another separate issue. I mentioned these to illustrate that the problem is potentially wider than just this issue. Hence another possible solution would be to provide an easier way for developers to turn twig debug on and off, perhaps selectively by URL/per view.
Comment #24
FeyP CreditAttribution: FeyP as a volunteer and at werk21 commentedThanks AdamPS. In that case, I think we should update the issue summary to better reflect the actual intentions of this issue, especially since the original warning mentioned in the summary has been fixed. Given that you already provided great input in the comments above, would you volunteer to do so?
Comment #25
AdamPS CreditAttribution: AdamPS at AlbanyWeb commentedComment #26
AdamPS CreditAttribution: AdamPS at AlbanyWeb commentedComment #27
AdamPS CreditAttribution: AdamPS at AlbanyWeb commentedI have raised a separate issue for the scenario in #22 #2914734: RSS feed view of fields corrupted with twig debug enabled
There are some other related issues listed in #2914733: [META] twig debug can break parts of site
Comment #28
Anonymous (not verified) CreditAttribution: Anonymous commentedWorking on an RSS feed, surprised this is broken...
Here's a patch that could probably use some improvements. Right now it assumes way too much about the output, somehow we should allow other things to alter the twig engine file, but I don't know enough about the theme engines to figure that out. Really it looks like all of this is procedural right now so I'm not sure how it would be possible without a big change.
Comment #29
AdamPS CreditAttribution: AdamPS at AlbanyWeb commented@vilepickle Excellent that you have started thinking about this. It's a great idea, but like you say it needs a little work.
How about this instead
if ($twig_service->isDebug() && !$variables['twig_no_debug_prefix']) {
Then add code in views RSS feed (and elsewhere with the twig problem) to set that flag in the variables.
Comment #30
AdamPS CreditAttribution: AdamPS at AlbanyWeb commentedIf you look on #2914733: [META] twig debug can break parts of site there are various related problems, and probably the trickiest is #2908634: "Hide if empty" not working if twig debug enabled. Whatever solution we come up with should ideally be extensible to the other issues to. For several of the related problems, ideally we could have the debug comments present, but also have a way to get the plain field whenever it is going to be processed further.
Comment #33
ivnish CreditAttribution: ivnish commentedSame problem
Comment #36
VladimirAusThis solution only affects one template, but not child ones.
Before fix
After fix
Comment #37
Shefik CreditAttribution: Shefik commentedThe patch should not be applicable only for views, but also for any custom controller with template output.
Comment #38
Shefik CreditAttribution: Shefik commentedThe patch should not be applicable only for views, but also for any custom controller with template output.
Comment #42
quietone CreditAttribution: quietone at PreviousNext commentedThis needs an issue summary update that clearly explains what the next step is for this issue. I think this is suitable as a first time issue.
Write an issue summary for an existing issue explain what is needed in an issue summary.
Comment #43
ravi.shankar CreditAttribution: ravi.shankar at OpenSense Labs commentedAdded reroll of patch #28 for Drupal 9.4.x. still needs work for #42.
Comment #44
VladimirAusLooks good. Applies to
9.4
.Comment #45
VladimirAusStill can see children as per #36
Comment #47
renatogI also continue seeing.
Before patch:
After patch: