Jump to:

5540 Posts in 1738 Topics by 1224 members

Customising the CMS

SilverStripe Forums » Customising the CMS » RSS Feed with images

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 2519 Views
  • Benedikt
    Avatar
    Community Member
    16 Posts

    RSS Feed with images Link to this post

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

  • Willr
    Avatar
    Forum Moderator
    5502 Posts

    Re: RSS Feed with images Link to this post

    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.

  • yurigoul
    Avatar
    Community Member
    202 Posts

    Re: RSS Feed with images Link to this post

    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)

  • Benedikt
    Avatar
    Community Member
    16 Posts

    Re: RSS Feed with images Link to this post

    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)

  • yurigoul
    Avatar
    Community Member
    202 Posts

    Re: RSS Feed with images Link to this post

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

  • JonoM
    Avatar
    Community Member
    103 Posts

    Re: RSS Feed with images Link to this post

    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');
       }

    2519 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.