Jump to:

3393 Posts in 976 Topics by 852 members

Template Questions

SilverStripe Forums » Template Questions » Is it possible to filter ChildrenOf() pages ?

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

Page: 1
Go to End
Author Topic: 954 Views
  • neogin
    Avatar
    Community Member
    13 Posts

    Is it possible to filter ChildrenOf() pages ? Link to this post

    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.

  • Willr
    Avatar
    Forum Moderator
    5462 Posts

    Re: Is it possible to filter ChildrenOf() pages ? Link to this post

    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.

  • neogin
    Avatar
    Community Member
    13 Posts

    Re: Is it possible to filter ChildrenOf() pages ? Link to this post

    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

  • Willr
    Avatar
    Forum Moderator
    5462 Posts

    Re: Is it possible to filter ChildrenOf() pages ? Link to this post

    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.

  • neogin
    Avatar
    Community Member
    13 Posts

    Re: Is it possible to filter ChildrenOf() pages ? Link to this post

    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

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