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.

Template Questions /

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

Is it possible to filter ChildrenOf() pages ?


Go to End


5 Posts   1820 Views

Avatar
neogin

Community Member, 13 Posts

5 September 2009 at 8:42am

Hello guys,

I'm a newbie to SS and I'm trying hard to understand, so I'm sorry if my question makes no sense.

So here it is : Is it possible to filter the ChildreOf() of a page, for example to only show children of with a specific $author ?

Thanks
G.

Avatar
Willr

Forum Moderator, 5523 Posts

5 September 2009 at 11:29am

In a template file or in the php? Also is Author a Database Field or a has_one relationship?

In the template its a bit tricky as you cannot quote arguments so you couldn't say <% if Author != Joe Smith %>. That will not work. It will if you are doing it in php as you can compare the strings. If you want to do it from the template file you are going to have to write a function in your custom page type which returns true or false based on the authors name.

Avatar
neogin

Community Member, 13 Posts

5 September 2009 at 9:18pm

Hi,

my fist idea was to do it in the template. I have a staff section with a page for each person with their name in the $Title.
Somewhere else I've got a publication section where they put they papers (it's a scientific website) and each publication is a child of PublicationHolder. The trick is that a same paper can have multiple authors.

And of course I would like each staff page to list all the papers in which the person is listed as author.

So far, I use a trick, each staff page has virtual pages redirecting to the right papers and I use <%control childrem%>. But I would like to make it automatic.

What would be the best way to do it ? What's your opinion ?

P.S. Sorry if I make mistakes english is not my mothertongue

Avatar
Willr

Forum Moderator, 5523 Posts

5 September 2009 at 9:28pm

And of course I would like each staff page to list all the papers in which the person is listed as author

This would be defined as a many_many join. So on your Paper Pagetype you would have a

// PaperPage class...

static $many_many = array(
'Authors' => 'StaffPage'
);

And the corresponding code on the Staff Page Class

// StaffPage class..

static $belongs_many_many = array(
'Papers' => 'PaperPage'
);

Then on the Staff page you can just do <% control Papers %> to get all the papers which the staff member is attached to.

For a more complete example see the 5th tutorial.

Avatar
neogin

Community Member, 13 Posts

5 September 2009 at 10:49pm

Hi,

great ! Now I've got a tab in each paper edit-part in wich I can see all the staff I inputed in the staff part. But I can't select the one I want to set as authors.

Here's my code : Article.php

 class Article extends Page {
   static $db = array(
    'Date1' => 'Date',
    'Author1' => 'Text' 
   );
   

   static $has_one = array(
   'ImageArticle'=>'Image',
   'FileArticle'=>'File'
   );
   
   static $many_many = array( 
'Authors' => 'StaffPage' 
);
      
   function getCMSFields() {
   $fields = parent::getCMSFields();
 
   $fields->addFieldToTab('Root.Content.Main', new CalendarDateField('Date1'), 'Content');
   $fields->addFieldToTab('Root.Content.Main', new ImageField('ImageArticle'),'Content');
   $fields->addFieldToTab('Root.Content.Main', new FileIFrameField('FileArticle'),'Content');
   $fields->addFieldToTab('Root.Content.Main', new TextField('Author1'),'Content');
   
    $tablefield = new HasManyComplexTableField(
         $this,
         'Authors',
         'StaffPage'
              );
      $tablefield->setAddTitle( 'Authors' );
 
      $fields->addFieldToTab( 'Root.Content.Authors', $tablefield );
     
     return $fields;
}
}
 
class Article_Controller extends Page_Controller {
 
}

and I've just add the

static $belongs_many_many = array( 
'Papers' => 'PaperPage' 
);
to the staffPage.php