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.

Data Model Questions /

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

Using DataObject::get() to list last few updated pages that are publicly visible


Go to End


7 Posts   4687 Views

Avatar
HansR

Community Member, 141 Posts

24 May 2009 at 8:48pm

Hi, I have a homepage class that displays the 10 most recently updated pages on the website. The problem is, that this includes pages that are not publicly visible. The code is:

function RecentlyUpdated() {
      $pages = DataObject::get("Page", "", "LastEdited DESC", "", 10);
      $doSet = new DataObjectSet();
      foreach($pages as $key => $data) {
         $title = $data->Title;
         $summary = $data->obj('Content')->FirstParagraph('html');;
         $record = array(
            'Link' => $data->Link(),
            'Title' => $title,
            'Date' => date('Y-m-d', strtotime($data->LastEdited)),
            'Summary' => $summary
         );
         $doSet->push(new ArrayData($record));
      }
      return $doSet;
   }

What should I set the DataObject::get()'s parameters to in order to obtain just the publicly visible pages. Actually, I'd like to filter out and Forum page too. I assume that I only need to change the filter string, but I have no idea what to set it to.

Hans

Avatar
NickJacobs

Community Member, 148 Posts

25 May 2009 at 4:29pm

Hi HansR,

I use this to only include visible pages:

$pages = DataObject::get("Page", "ShowInMenus = 1", "LastEdited DESC", "", 10); 

To filter Forum pages out as well, something like this should work:

$pages = DataObject::get("Page", "ShowInMenus = 1 AND ClassName !=ForumPage", "LastEdited DESC", "", 10); 

Avatar
HansR

Community Member, 141 Posts

25 May 2009 at 9:42pm

Hi NickJacobs,

Thanks for that. I'm still hoping that someone knows how to get the "show to all" pages, because I might want to include a page in the menu. However, your solution should be fine for now.

Hans

Avatar
Ben Gribaudo

Community Member, 181 Posts

27 May 2009 at 12:28am

Edited: 27/05/2009 12:28am

Hello,

Have you looked at how SilverStripe does this? You might want to look at methods Children(), AllChildren(), stageChildren() and liveChildren() in class Hierarchy.

Ben

Avatar
HansR

Community Member, 141 Posts

27 May 2009 at 9:36am

Hi Ben,

The methods that you pointed to aren't the best option in this case; the get() method is the right one to find the ten most recently updated public pages. Plus, all of the pages are "live." The difference between pages is that most are "viewable by all," whereas some will be "viewable by group X." So, I'm looking to filter out the pages that have restrictive security settings, and forum pages).

Hans

Avatar
Willr

Forum Moderator, 5523 Posts

27 May 2009 at 10:39pm

If you want to check permissions then it would be easier to do this in the ORM. Tricky thing is you might have to get more then 10 as you are removing objects as you go. In this one I get all the pages (just for kicks) then adding to a list, making sure I only get 10.

$pages = DataObject::get("Page", "ShowInMenus = 1 AND ClassName !=ForumPage", "LastEdited DESC");
$output = new DataObjectSet();
if($pages) {
 foreach($pages as $page) {
  if($page->canView()) { // check we cant view
     $output->push($page);
     if($output->TotalItems() > 9) break;
   }
}
}
return $output;

Avatar
HansR

Community Member, 141 Posts

30 May 2009 at 1:59pm

@willr

That almost worked. My code ended up being:

      $pages = DataObject::get("Page", "ShowInMenus = 1 AND ClassName != 'Forum'", "LastEdited DESC");
      if($pages) {
          $output = new DataObjectSet();
          foreach($pages as $page) {
            if($page->canView()) { // check we cant view
               $output->push($page);
               if($output->TotalItems() > 9) 
                  break;
            }
          }
      }
      return $output;

The one change is ForumPage changed to 'Forum'. Thanks for your help.

Hans