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 /

How to RSS feed content from other pages [tutorial]


Reply


1194 Views

Avatar
jdi

Community Member, 34 Posts

2 September 2010 at 11:31am

Edited: 02/09/2010 11:40am

I don't think that there is enough accessible documentation on this:

How to RSS feed content from other pages.

In this example I am feeding the latest articles of my site from the RSS feed found in my Homege_Controller using the article page setup found in the Silverstripe tutorials.

This is very simple.

What knowledge are we using?

1. A understanding of handling DataObjects using the [url=http://doc.silverstripe.org/datamodel#querying_data]Querying Methods[/url] of the Data Model.

2. The [url=http://doc.silverstripe.org/rssfeed]RSSFeed Class[/url] and how to follow it's [url=http://api.silverstripe.org/2.4/sapphire/integration/RSSFeed.html]API Documentation[/url].

I am using a custom homepage 'mysite/code/Homepage.php' and will be adding this code snippet into the Homepage_Controller class found in Homepage.php.

Step 1:

Using the code found in the [url=http://doc.silverstripe.org/rssfeed]RSSFeed Class[/url] documentation I have added a RSS feed to my Homepage_Controller:

   
   function init() {
      RSSFeed::linkToFeed($this->Link() . "rss", "RSS feed of this blog");
      parent::init();
   }

   function rss() {
      $rss = new RSSFeed($this->Children(), $this->Link(), "My feed", "This is an example feed.", "Title", "Content", "Author");
      $rss->outputToBrowser();
   }

Step 2:

We need to retrieve a custom DataObjectSet of pages which we would like to list in our Homepage RSS feed, we use the 'DataObject::get' operator to retrieve our set, and I am specifically targeting pages using the "ArticlePage" class/template.

I've added this code to my Homepage_Controller:

   $LatestArticles = DataObject::get(
      $callerClass = "ArticlePage"
   );

e.g...

   
   function init() {
      RSSFeed::linkToFeed($this->Link() . "rss", "RSS feed of this blog");
      parent::init();
   }

   

   function rss() {
      $LatestArticles = DataObject::get(
         $callerClass = "ArticlePage"
      );

      $rss = new RSSFeed($this->Children(), $this->Link(), "My feed", "This is an example feed.", "Title", "Content", "Author");
      $rss->outputToBrowser();
   }

Step 3:

We need to pass the DataObjectSet to the RSSFeed class to get those pages listing in our RSS feed. To do this we simply modify the first arguement ($entries) of the RSSFeed class to contain our $LatestArticles DataObjectSet:

   
   function init() {
      RSSFeed::linkToFeed($this->Link() . "rss", "RSS feed of this blog");
      parent::init();
   }

   function rss() {
      $LatestArticles = DataObject::get(
         $callerClass = "ArticlePage"
      );

      $rss = new RSSFeed($LatestArticles, $this->Link(), "My feed", "This is an example feed.", "Title", "Content", "Author");
      $rss->outputToBrowser();
   }

And that's it. Simple.

If you review the [url=http://api.silverstripe.org/2.4/sapphire/integration/RSSFeed.html]API Documentation[/url] you can find the information on what arguments are available for use:

constructor __construct ( $entries, $link, $title, [$description = null], [$titleField = "Title"], [$descriptionField = "Content"], [$authorField = null], [$lastModified = null], [$etag = null] )

I've modified my version of ArticlePage.php since doing the tutorial and added an "Introduction" to each page. So instead of my feed posting the entire "Content" of each page I've modified my use of the RSSFeed class to post my Introductions by updating the [$descriptionField = "Content"] argument:

      $rss = new RSSFeed($LatestArticles, $this->Link(), "My feed", "This is an example feed.", "Title", "Introduction", "Author");