Jump to:

5513 Posts in 1732 Topics by 1218 members

Customising the CMS

SilverStripe Forums » Customising the CMS » How to RSS feed content from other pages [tutorial]

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

Page: 1
Go to End
Author Topic: 1122 Views
  • jdi
    Avatar
    Community Member
    34 Posts

    How to RSS feed content from other pages [tutorial] Link to this post

    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 Querying Methods of the Data Model.

    2. The RSSFeed Class and how to follow it's API Documentation.

    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 RSSFeed Class 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 API Documentation 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");

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