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.

Archive /

Our old forums are still available as a read-only archive.

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

Extracting recent children of a parent Page


Go to End


2 Posts   2171 Views

Avatar
Naely

Community Member, 4 Posts

26 October 2008 at 6:59pm


I'm trying to extract the most recent children of a Page instance called "Recent Success Stories" so that I can show the most recent 3 pages. The following 4 functions were extracted from this tutorial http://doc.silverstripe.com/doku.php?id=rssfeed under "Example of showing the 10 most recently updated
pages."

Could you please take a look and
A) verify that I'm doing this correctly, particularly noting the last comment +
B) explain a little about each function? I generally understand the functionality, but would very much appreciate some verbose explanation.

function recentSS() {
global $recentSS;
return $recentSS;
}

function init() {
RSSFeed::linkToFeed($this->Link() . "rss", "Recent Success Stories");
parent::init();
}

function rss() {
$rss = new RSSFeed($this->LatestUpdates(), $this->Link(), "Recent Success Stories", "Shows a list of the recent success stories.", "Title", "Content", "Author");
$rss->outputToBrowser();
}

function LatestUpdates() {
// 10 is the number of pages
return DataObject::get("Page", "", "LastEdited DESC", "", 10);
DataObject::get('VirtualPage', "ParentID != $this->ID", 'Date DESC', '', 3); // *** Is this correct?
}
}

Avatar
Willr

Forum Moderator, 5523 Posts

27 October 2008 at 12:55pm

 function recentSS() { 
         global $recentSS; 
          return $recentSS; 
      }   

Not sure you need that unless you are using it for something other then this example.

 function init() { 
RSSFeed::linkToFeed($this->Link() . "rss", "Recent Success Stories"); 
         parent::init(); 
}

The function init() is the first thing thats called when you visit the page. It is always called if you are viewing the page. Usually its where you do security / permission checking or including extra files you need. It this example it is creating a '<link>' tag in your HTML to point to your RSS feed.

To break it down further - $this->Link() returns the URL of this page (eg http://www.mysite.com/) then adding 'rss' to it so the final result is http://www.mysite.com/rss - This points to the 'rss' method defined below.

function rss() { 
         $rss = new RSSFeed($this->LatestUpdates(), $this->Link(), "Recent Success Stories", "Shows a list of the recent success stories.", "Title", "Content", "Author"); 
         $rss->outputToBrowser(); 
      }

So basically that init function points to this 'rss' function and this rss function outputs a RSS feed - An RSS feed of LatestUpdates..

function LatestUpdates() { 
         // 10 is the number of pages 
         return DataObject::get("Page", "", "LastEdited DESC", "", 10); 
}

This will get the latest 10 pages you have edited. Note that the 'return' you cant have any other code after that return statement as that breaks out of the function. If you want to get children of a given page you can do it a number of ways, heres one

function LatestUpdates() {
   $parent = DataObject::get_one("Page", "Title = 'Title of the Page');
   return DataObject::get("Page", "ParentID = $parent->ID", LastEdited DESC", "",10);
}

So that basically gets a page with a given title and then using the ID of that parent we use that to filter the search of the Second DataObject call.