Upgrade Your Drupal Skills
We trained 1,000+ Drupal Developers over the last decade.
See Advanced Courses NAH, I know EnoughBuild a valid RSS feed for Planet Drupal
Since joining Planet Drupal in 2016, no seriously new blog posts for a long time. Last year, my personal site was rebuilt with Drupal 8 again. And a few months ago, I found my feed was removed from Planet Drupal. Recently, I wanted to join in it again. The first task is to make a valid RSS. This is the background.
Suggestions from How to get your feed to the Planet
If your site uses Drupal, you can create a "Drupal Planet" taxonomy term and use it to tag any content you want to appear on the Planet. Drupal automatically creates an RSS feed for each taxonomy term, and you can submit that term’s feed URL in your Drupal Planet application. Alternatively, you can use the Drupal Planet feature module, which uses Flag and Views modules to create the feed. If you are not using Drupal, you will need to make sure your site can create a feed just for your Planet content.
Using Planet content is inapplicable as it has no Drupal 8 release.
Previously, my feed had the term "Drupal Planet" exactly, and I did submit http://ranqiangjun.com/taxonomy/term/1/feed. This time I want to avoid using the term, instead, creating a content type Planet Drupal and a View Planet Drupal to provide an RSS feed,
Content type: Planet Drupal
Creating content type called Planet Drupal (machine name: planet_drupal) with a body filed. nothing fancy here.
View: Planet Drupal
Add a view Planet Drupal (machine name: planet_drupal) with a page display and a feed display
For the page display, it's free to make it whatever you want it to be, let's focus on the feed display
Feed display
-
TITLE
-
Title:Planet Drupal
-
FORMAT
-
Format:RSS Feed | Settings
-
Show:Fields | Settings
-
FIELDS
-
Content: Title
-
Content: Authored by [hidden]
-
Content: Authored on [hidden]
-
Content: Body [hidden]
-
Content: Link to Content [hidden]
-
Content: UUID [hidden]
-
...
-
FEED SETTINGS
-
Path:/planet.xml
-
Attach to:Page
For the fields added
- Title, remember uncheck Link to the Content
- Authored by, Formatter: label, unchecked Link label to the referenced entity
- Authored on, Date format:
Custom
, Custom date format:r
, here is important. It should be a RFC 2822 formatted date.r
is the proper format character. - Body, Formatter:
Summary or Trimmed
, Trimmed limit:600
characters - Link to Content, Check
Output the URL as text
, Checked or uncheckedUse absolute link (begins with "http://")
are both ok. - UUID, we know UUID is unique, it's used as the
GUID
in feed. by default no formmater for UUID field type, but there is a module called uuid_extra which provides an UUID formatter, so install it, and choose Formatter:UUID
.
Next mapping the fields to feed fields
- Title field -> Content: Title
- Link field -> Content: Link to Content
- Description field -> Content: Body
- Creator field -> Content: Authored by
- Publication date field -> Content: Authored on
- GUID field -> Content: UUID, and uncheck
GUID is permalink
So far so good, let's check if our feed is valid. Go to W3C Feed Validation Service and submit
Bingo, it's valid, but it complains
Let's fix it.
By checking https://dri.es/taxonomy/term/1/feed, which is a valid feed. it contains only one namespace xmlns:dc="http://purl.org/dc/elements/1.1/
, so let's only keep the one. And fix Missing atom:link with rel="self"
needs adding one namespace and inserting a atom:link to your feed in the channel section
Solution: If you haven't already done so, declare the Atom namespace at the top of your feed, thus: Then insert a atom:link to your feed in the channel section. Below is an example to get you started. Be sure to replace the value of the href attribute with the URL of your feed.
So our solution would be alteing the namespaces inside the implementation of template_preprocess_views_view_rss, and adding a new variable called feed_url
as the href
of the atom:link
tag being inserted. let's assume we have a theme called planet
Inside planet.theme file, add the implementation of hook_preprocess_views_view_rss
-
function planet_preprocess_views_view_rss(&$variables) {
-
/** @var \Drupal\views\Entity\View $view */
-
$view = $variables['view'];
-
if ($view->id() === "planet_drupal") {
-
$style = $view->style_plugin;
-
return $key === 'xmlns:dc';
-
}, ARRAY_FILTER_USE_KEY);
-
/** @var \Drupal\views\Plugin\views\display\Feed $display */
-
$display = $view->getDisplay('feed_1');
-
if ($display !== NULL) {
-
$variables['feed_url'] = $display->getUrl()->setAbsolute()->toString();
-
}
-
// Add the missing namespace
-
$style->namespaces['xmlns:atom'] = 'http://www.w3.org/2005/Atom';
-
$variables['namespaces'] = new Attribute($style->namespaces);
-
}
-
}
Copy the views-view-rss.html.twig
from core/modules/views/templates
into the planet theme's templates folder and insert the atom:link
tag inside channel.
-
...
-
<channel>
-
...
I will put all together and publish a module probably on github or as the drupal 8 port of drupal_planet on d.o.
PS
-
Missing atom:link with rel="self"
does not make sense to me, as I am creating a RSS feed not an atom, as I could understand, they are two formats. Anyway, let's just follow what the Validation Service suggests. -
An issue reported to Drupal core Let StringFormatter support UUID filed type explicitly
About Drupal Sun
Drupal Sun is an Evolving Web project. It allows you to:
- Do full-text search on all the articles in Drupal Planet (thanks to Apache Solr)
- Facet based on tags, author, or feed
- Flip through articles quickly (with j/k or arrow keys) to find what you're interested in
- View the entire article text inline, or in the context of the site where it was created
See the blog post at Evolving Web