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
---------------

  1. create a new drupal project
  2. turn on twig debug
  3. 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.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

tjwelde created an issue. See original summary.

cilefen’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, rss_transform-0.patch, failed testing.

cilefen’s picture

The patch was created outside of Drupal's base directory.

tjwelde’s picture

But the proposed solution would be fine?

joelpittet’s picture

Version: 8.1.8 » 8.2.x-dev
Issue tags: -RSS, -rss feeds, -RSS.XML, -Views RSS

I'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.

mike_pol’s picture

I 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:

  1. create a new drupal project
  2. activate the aggregator module
  3. turn on twig debug
  4. visit the default feed page (/aggregator/rss)

The result would be:
<?xml version="1.0"?>

Apply the patch, the output becomes:

<!-- 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 xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0" xml:base="http://drupal-8-2-x.dd:8083/aggregator/rss">
  <channel>
    <title>Aggregator RSS feed</title>
    <link>http://drupal-8-2-x.dd:8083/aggregator/rss</link>
    <description/>
    <language>en</language>
    
    
  </channel>
</rss>
<!-- END OUTPUT from 'core/themes/classy/templates/views/views-view-rss.html.twig' -->

What would be the best approach to solve the issue though?

joelpittet’s picture

I 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.

mike_pol’s picture

I 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

tjwelde’s picture

tjwelde’s picture

Issue summary: View changes
tjwelde’s picture

When 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.

tjwelde’s picture

Issue summary: View changes
ckaotik’s picture

How about simply ensuring the <?xml?> tag is output first? Something like this might do the trick:

// Move starting comments after xml doc tag.
$rss_markup = preg_replace("/^(\s*(?:<!--.*-->\s*)+)(<\?xml.*?\>)/", "$2$1", $rss_markup, 1);

Resulting output:

<?xml version="1.0" encoding="utf-8"?>
<!-- THEME DEBUG -->
<!-- THEME HOOK: 'views_view_rss' -->
<!-- BEGIN OUTPUT from 'core/themes/classy/templates/views/views-view-rss.html.twig' -->
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0" xml:base="http://drupal-8-2-x.dd:8083/aggregator/rss">
  <channel>
    <title>Aggregator RSS feed</title>
    <link>http://drupal-8-2-x.dd:8083/aggregator/rss</link>
    <description/>
    <language>en</language>
    
    
  </channel>
</rss>
<!-- END OUTPUT from 'core/themes/classy/templates/views/views-view-rss.html.twig' -->

tjwelde’s picture

Yes, that would work, but also wouldn't represent the truth.
It begins the output with <?xml ... not with <rss ...

ckaotik’s picture

But 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.

lias’s picture

So 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

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

FeyP’s picture

AdamPS’s picture

Title: RSS output fails, when using Twig debug » RSS output fails when using Twig debug
Status: Closed (duplicate) » Needs work

I 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.

  • Expect that many test/dev sites have twig debug enabled by default.
  • When developing a new feed, people will use the dev site.
  • If the RSS feed doesn't work with twig debug it is initially very confusing until you find this issue.
  • Once the cause is known it is frustrating to keep turning twig debug on/off.
  • Control over twig debug is not available in the GUI, so GUI admin has to call the dev.
  • Difficult to debug RSS feed without twig debug.
AdamPS’s picture

Here is another scenario.

  • Create an RSS feed view
  • Set "Show: fields"
  • In the field settings set "Link field" = "Content: Path"
  • Fatal error because views tries to parse a comment as a URL

InvalidArgumentException: The user-entered string '

/notices/what-great-event

' must begin with a '/', '?', or '#'. in Drupal\Core\Url::fromUserInput() (line 204 of core/lib/Drupal/Core/Url.php).

Twig debug also confuses any views that have tests if fields are empty. Here are some ideas that could help

  • If twig debug is enabled, whenever views runs further processing on something likely to be a compiled twig template, call a function to strip comments??
  • Make it easier to enable/disable twig debug selectively for example it could be a setting on a view??
AdamPS’s picture

Sorry 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:

  • It's difficult to test the RSS feed because it is rejected by many external tools. In particular a developer cannot test integration with third-party services such as IFTTT.
  • The transformRootRelativeUrlsToAbsolute step was skipped so the URLs may be wrong.

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.

FeyP’s picture

Thanks 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?

AdamPS’s picture

Title: RSS output fails when using Twig debug » RSS output not valid when using Twig debug
Issue summary: View changes
Status: Needs work » Active
Issue tags: -Needs issue summary update
AdamPS’s picture

AdamPS’s picture

I 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

Anonymous’s picture

Status: Active » Needs review
FileSize
1.08 KB

Working 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.

AdamPS’s picture

Status: Needs review » Needs work

@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.

AdamPS’s picture

If 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.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

ivnish’s picture

Same problem

Version: 8.6.x-dev » 8.8.x-dev

Drupal 8.6.x will not receive any further development aside from security fixes. Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

VladimirAus’s picture

This solution only affects one template, but not child ones.

Before fix

<!-- 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="https://website.local/" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Website titile</title>
    <link>https://website.local/</link>
    <description></description>
    <language>en</language>
    
    

<!-- THEME DEBUG -->
<!-- THEME HOOK: 'ds_entity_view' -->
<!-- BEGIN OUTPUT from 'modules/contrib/ds/templates/ds-entity-view.html.twig' -->


<!-- THEME DEBUG -->
<!-- THEME HOOK: 'recall' -->
<!-- FILE NAME SUGGESTIONS:
   * recall--node--20337.html.twig
   * recall--node-rss.html.twig
   * recall--node.html.twig
   x recall.html.twig
   x recall.html.twig
-->
<!-- BEGIN OUTPUT from 'sites/www.productsafety.gov.au/modules/custom_content/templates/recall.html.twig' -->


<article class="full-view node-full-view">
            <div class="node-field__section node-field__flex">
...

After fix

<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xml:base="https://website.local/" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Website titile</title>
    <link>https://website.local/</link>
    <description></description>
    <language>en</language>
    
    

<!-- THEME DEBUG -->
<!-- THEME HOOK: 'ds_entity_view' -->
<!-- BEGIN OUTPUT from 'modules/contrib/ds/templates/ds-entity-view.html.twig' -->


<!-- THEME DEBUG -->
<!-- THEME HOOK: 'recall' -->
<!-- FILE NAME SUGGESTIONS:
   * recall--node--20337.html.twig
   * recall--node-rss.html.twig
   * recall--node.html.twig
   x recall.html.twig
   x recall.html.twig
-->
<!-- BEGIN OUTPUT from 'sites/www.productsafety.gov.au/modules/custom_content/templates/recall.html.twig' -->


<article class="full-view node-full-view">
            <div class="node-field__section node-field__flex">
...
Shefik’s picture

The patch should not be applicable only for views, but also for any custom controller with template output.

Shefik’s picture

The patch should not be applicable only for views, but also for any custom controller with template output.

Version: 8.9.x-dev » 9.2.x-dev

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.15 was released on June 1st, 2022 and is the final full bugfix release for the Drupal 9.3.x series. Drupal 9.3.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.4.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

quietone’s picture

This 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.

ravi.shankar’s picture

Added reroll of patch #28 for Drupal 9.4.x. still needs work for #42.

VladimirAus’s picture

Status: Needs work » Reviewed & tested by the community

Looks good. Applies to 9.4.

VladimirAus’s picture

Status: Reviewed & tested by the community » Needs work

Still can see children as per #36

<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:foaf="http://xmlns.com/foaf/0.1/" xmlns:og="http://ogp.me/ns#" xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#" xmlns:schema="http://schema.org/" xmlns:sioc="http://rdfs.org/sioc/ns#" xmlns:sioct="http://rdfs.org/sioc/types#" xmlns:skos="http://www.w3.org/2004/02/skos/core#" xmlns:xsd="http://www.w3.org/2001/XMLSchema#" version="2.0" xml:base="http://tomato-elephant-studio.local/">
  <channel>
    <title>Tomato Elephant Studio</title>
    <link>http://tomato-elephant-studio.local/</link>
    <description/>
    <language>en</language>
    
    <item>
  <title>Drupal mentoring (May 2022)</title>
  <link>http://tomato-elephant-studio.local/events/2022-05-27</link>
  <description>

&lt;!-- THEME DEBUG --&gt;
&lt;!-- THEME HOOK: 'ds_entity_view' --&gt;
&lt;!-- BEGIN OUTPUT from 'modules/contrib/ds/templates/ds-entity-view.html.twig' --&gt;


&lt;!-- THEME DEBUG --&gt;
&lt;!-- THEME HOOK: 'ds_1col' --&gt;
&lt;!-- FILE NAME SUGGESTIONS:
   * ds-1col--node--125.html.twig
   * ds-1col--node-tes-event-rss.html.twig
   * ds-1col--node-tes-event.html.twig
   * ds-1col--node-rss.html.twig
   * ds-1col--node.html.twig
   x ds-1col.html.twig
   x ds-1col.html.twig
--&gt;
&lt;!-- BEGIN OUTPUT from 'modules/contrib/ds/templates/ds-1col.html.twig' --&gt;
&lt;div data-quickedit-entity-id="node/125" class="contextual-region node node--type-tes-event node--view-mode-rss ds-1col clearfix"&gt;

  &lt;div data-contextual-id="node:node=125:changed=1652884725&amp;ds_bundle=tes_event&amp;ds_view_mode=default&amp;langcode=en" data-contextual-token="E3u5oapBVaUmZv540jIGnB6-wnkd6c0TKjFMjncfEQY"&gt;&lt;/div&gt;

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.9 was released on December 7, 2022 and is the final full bugfix release for the Drupal 9.4.x series. Drupal 9.4.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.5.x-dev branch from now on, and new development or disruptive changes should be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

renatog’s picture

FileSize
21.64 KB
19.54 KB

I also continue seeing.

Before patch:

<!-- THEME DEBUG -->
<!-- THEME HOOK: 'views_view_rss' -->
<!-- BEGIN OUTPUT from 'core/modules/views/templates/views-view-rss.html.twig' -->
<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xml:base="http://drupal10.lndo.site/" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Blog Posts</title>
    <link>http://drupal10.lndo.site/</link>
    <description></description>
    <language>en</language>
    
    

<!-- THEME DEBUG -->
<!-- THEME HOOK: 'views_view_row_rss' -->
<!-- BEGIN OUTPUT from 'core/modules/views/templates/views-view-row-rss.html.twig' -->
<item>
  <title>Second Blog</title>
  <link>http://drupal10.lndo.site/blog-2</link>
  <description>

&lt;!-- THEME DEBUG --&gt;
&lt;!-- THEME HOOK: &#039;field&#039; --&gt;
&lt;!-- FILE NAME SUGGESTIONS:
   * field--node--title--blog.html.twig
   x field--node--title.html.twig
   * field--node--blog.html.twig
   * field--title.html.twig
   * field--string.html.twig
   * field.html.twig
--&gt;
&lt;!-- BEGIN OUTPUT from &#039;core/modules/node/templates/field--node--title.html.twig&#039; --&gt;

&lt;span&gt;Second Blog&lt;/span&gt;

&lt;!-- END OUTPUT from &#039;core/modules/node/templates/field--node--title.html.twig&#039; --&gt;

After patch:

<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0" xml:base="http://drupal10.lndo.site/">
  <channel>
    <title>Blog Posts</title>
    <link>http://drupal10.lndo.site/</link>
    <description/>
    <language>en</language>
    
    <item>
  <title>Second Blog</title>
  <link>http://drupal10.lndo.site/blog-2</link>
  <description>

&lt;!-- THEME DEBUG --&gt;
&lt;!-- THEME HOOK: 'field' --&gt;
&lt;!-- FILE NAME SUGGESTIONS:
   * field--node--title--blog.html.twig
   x field--node--title.html.twig
   * field--node--blog.html.twig
   * field--title.html.twig
   * field--string.html.twig
   * field.html.twig
--&gt;
&lt;!-- BEGIN OUTPUT from 'core/modules/node/templates/field--node--title.html.twig' --&gt;

&lt;span&gt;Second Blog&lt;/span&gt;

&lt;!-- END OUTPUT from 'core/modules/node/templates/field--node--title.html.twig' --&gt;



&lt;!-- THEME DEBUG --&gt;
&lt;!-- THEME HOOK: 'field' --&gt;
&lt;!-- FILE NAME SUGGESTIONS:
   * field--node--uid--blog.html.twig
   x field--node--uid.html.twig
   * field--node--blog.html.twig
   * field--uid.html.twig
   * field--entity-reference.html.twig
   * field.html.twig
--&gt;
&lt;!-- BEGIN OUTPUT from 'core/modules/node/templates/field--node--uid.html.twig' --&gt;

&lt;span&gt;

Version: 9.5.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.