Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » Extracting recent children of a parent Page

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

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

Page: 1
Go to End
Author Topic: 1591 Views
  • Naely
    Avatar
    Community Member
    4 Posts

    Extracting recent children of a parent Page Link to this post

    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?
          }
       }

  • Willr
    Avatar
    Forum Moderator
    5490 Posts

    Re: Extracting recent children of a parent Page Link to this post

    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.

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