Id Est Drupal Development

Fixed: Drupal RSS Not Validating

An essential part of any news web site is the RSS feed. RSS allows you to push your content out into the world as soon as you hit the “publish” (or “save” in Drupal) button.

With a standard installation of Drupal you can customise an RSS feed for your site in a few minutes and create more targeted RSS feeds for sections of the site.

Drupal uses the powerful Views module, in core since Drupal 8, to create feeds and lists, including RSS feeds. In Views, you can include thumbnail images, selected fields from each item, or full stories, depending on your end-user.

For my purposes, I just wanted a feed of the most recent news items and reviews published to the site for those who source our RSS, be it news aggregators or individuals who like to browse their news through feed readers.

When I clicked on the EdinburghGuide.com RSS page everything looked fine. But checking the Drupal site reports I saw – along with all the usual WordPress sniffer traffic – that there was an RSS link throwing “Page Not Found” errors.

The errors were actually for “/rss” rather than the correct “rss.xml” page but it sent me checking the feed.

As I say, the RSS looked ok, but plugging in the URL for the RSS at W3C Feed Validation Service returned a:

“Sorry

This feed does not validate.

line 6, column 0: XML parsing error: :6:0: XML or text declaration not at start of entity”

Typically, white space before the declaration in the RSS source code can cause this kind of validation error.

I hadn’t changed much from the default setting for the main RSS view in Drupal, and when a cursory Google search didn’t turn up instances of other Drupal users experiencing the problem, it suggested to me that this was something particular to my set-up.

I went back to my RSS feed and squinted again at the code.

There it was staring me in the face. I’d left Twig debug engine on.

Twig is the underlying theming engine used by Drupal. The debug engine is helpful at the design stage, but as the link above says it can break views, and shouldn’t be used on a production site. I obviously forgot to turn it off.

The solution was to toggle the Twig debug to off in my site services.yml file (sites/default/services.yml). It’s a simple matter of changing the boolean operator from “true” to “false” on the relevant line.

twig.config:
# Twig debugging:
# (clipped)
# For more information about debugging Twig templates, see
# https://www.drupal.org/node/1906392.
#
# Not recommended in production environments
# @default false
debug: true

So I changed the last line to:

debug: false

Then I flushed the site cache using Drush.

drush cr

Retrying W3C’s feed validator returned the desired result.

Finally, to fix the “/rss” errors I added a redirect from that URL to the feed URL “rss.xml”, using Drupal’s redirect module.