How do I prevent my content's title from appearing in the <description> tag of my RSS feed?

drupal.stackexchange https://drupal.stackexchange.com/questions/289676

  •  21-02-2021
  •  | 
  •  

Question

I have a newly migrated Drupal 8 site and am noticing that the RSS feed displays the Title of a node twice in my feed reader. This is because a node's title is output both in the feed item's <title> and within the item's <description>.

For example:

<item>
  <title>My Clever Title</title>
  <link>http://example.com/node/123</link>
  <description>&lt;span data-quickedit-field-id="node/123/title/und/rss"&gt;My Clever Title&lt;/span&gt;

  &lt;div data-quickedit-field-id="node/123/body/und/rss" class="field field--name-body field--type-text-with-summary field--label-hidden field--item"&gt;
  Lorem ipsum... my content displays here....
  </description>
  ...other tags here...
</item>

So now, a feed reader like Feedly displays my content like this:

My Clever Title

My Clever Title

Lorem ipsum... my content displays here....

Note: In my RSS Manage Display, the content type is set to display the "Default" (not-trimmed) body field.

How do I prevent my RSS feed from inserting the post's title into the the <description> tag?

Was it helpful?

Solution

I haven't tried to reproduce this on my end, but I found a Drupal.org thread with advice that may be relevant:

https://www.drupal.org/project/drupal/issues/2946812#comment-12501714

using 'Show: content' looks like a bad idea to begin with.

I can get some nice clean output:

  • Choose 'Show: Fields' (so not 'Force using fields', just plain fields output)
  • Close the dialog without setting the required fields (just click the X in the top right corner)
  • Add some fields (title, body, a url, node id)
  • Go to Row style options (so click on the 'Settings' link behind 'Show: Fields'
  • Fill out the required fields with the fields you've added
  • Add more fields if you need them to fill out the required settings

If you build the description this way, you should easily be able to omit the title field. Good luck!

OTHER TIPS

Following the link in hotwebmatter's answer, I found the following fix based on this comment: https://www.drupal.org/project/drupal/issues/2946812#comment-13329019

function MYTHEME_preprocess_views_view_row_rss(&$variables) {
  $html = $variables['description'];

  $dom = new \DOMDocument;
  $dom->loadHTML($html);
  $xpath = new \DOMXpath($dom);
  $to_replace = [];
  $replace_with = [];

  // Search for post title based on data-quickedit-field-id attribute
  $found = $xpath->query('//*[contains(@data-quickedit-field-id,"/title/en/rss")]');
  if (!empty($found[0])) {
    $to_replace[] = $found[0]->ownerDocument->saveXML($found[0]);
    $replace_with[] = '';
  }

  $html = str_replace($to_replace, $replace_with , $html);
  $variables['description'] = $html;
}

The title field does not have a class in my RSS output so I changed the original code to look in the quickedit attribute.

Licensed under: CC-BY-SA with attribution
Not affiliated with drupal.stackexchange
scroll top