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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Customising the CMS /

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

How to RSS feed content from other pages [tutorial]


Go to End


2299 Views

Avatar
Jakxnz

Community Member, 36 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 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");