Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Customising the CMS /

RSS Feed with images


Reply


6 Posts   2677 Views

Avatar
Benedikt

Community Member, 16 Posts

16 March 2010 at 12:01am

RSS has the possibility to include images in the feed. Cf. http://www.w3schools.com/rss/rss_tag_image.asp

However, the Sapphire class doesn't give the opportunity to add images to the feed (at least I couldn't find it). Is it possible to extend the rssfeed class in order to have images in the feed? How would that be done?

(Maybe I can also submit a feature request since this would be an useful feature.)

Avatar
Willr

Forum Moderator, 5513 Posts

16 March 2010 at 7:18pm

If you wish to submit this as a feature request please open a new enhancement ticket on http://open.silverstripe.org if there isn't one already.

Avatar
yurigoul

Community Member, 202 Posts

16 March 2010 at 8:23pm

You can give an image field instead of content and it will be inserted as such.

However if you also want to include text then it gets hairy.

I got as far as including the RSSFeed-class in my page.php, but after that I got stuck. And I couldnot find the correct location to store my own RSSfeed.ss.

Anyone have any infos on this? (asked htis before, nobody seems to know)

Avatar
Benedikt

Community Member, 16 Posts

21 July 2010 at 4:17am

Edited: 21/07/2010 4:22am

I finally found the time to dig into this.

The generic <image> is only placed inside the <channel> and is rarely used. Therefore I didn't followed this.

The individual entries can't have specific <image>-Tags. Images need to be placed into the <description>.

The core RSSFeed class doesn't need to be changed to solve this.

What I did was creating a function called RSSContent() in the page types containing the entries which are used by the feed.

   function RSSContent() {
    if ($this->Image()->exists())
    {
      $returnvalue ='<div style="float: left; margin-right: 3px;"><img src="'.$this->Image()->SetWidth(150)->AbsoluteURL.'" alt="'.$this->Image()->Title.'" /></div>'.$this->Content;
    }
    else
    { $returnvalue=$this->Content; }
    return $returnvalue;
   }

To be safe a fallback function RSSContent should be placed in the page.php which just returns the usual content.

In the second step the RSS function in the respective holder class needs to be changed:

$rss = new RSSFeed($this->CountryOfTheMonth(), $this->Link(), "Country of the Month", "Each month we present a new country.", "Title", "RSSContent");
    $rss->outputToBrowser();

This places image objects in the RSS feeds which are reproduced in the feed reader or in Facebook (when using RSSGraffiti).

(Thanks to Martijn for the help with rendering image objects inside the PHP code)

Avatar
yurigoul

Community Member, 202 Posts

21 July 2010 at 4:47am

I will try this out as soon as I have the time. Thanks! And if this were on Reddit, you would get my upvotes!

Avatar
JonoM

Community Member, 103 Posts

27 January 2013 at 11:08am

This is great, but the only trouble I had was that my custom description would be escaped twice. To stop this happening I had to change the default casting type in a core file. This was the only way I could work out to stop my custom description being escaped twice - if anyone knows how to fix this without changing a core file I'd love to hear it.

RSSFeed.php > class RSSFeed_Entry

from:

   function Description() {
      return $this->rssField($this->descriptionField, 'Text');
   }

to:

   function Description() {
      return $this->rssField($this->descriptionField, 'HTMLText');
   }