Jump to:

5450 Posts in 1672 Topics by 1197 members

Customising the CMS

SilverStripe Forums » Customising the CMS » [Solved] Populating a DropDownField with Children

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

Page: 1
Go to End
Author Topic: 510 Views
  • Mr. Neave
    Avatar
    Community Member
    8 Posts

    [Solved] Populating a DropDownField with Children Link to this post

    For a blog/magazine style site with Posts organized into Categories.

    Categories may only contain Posts as child pages.

    Each Category would have one featured Post at any one time, selectable in the CMS.

    So, in the Category class I have this:

    static $allowed_children = array('Post');
    static $has_one = array(
    'FeaturedPost' => 'Post'
    );

    In the admin for a Category, I want to populate a dropdown with all of the Category's child Posts, and save the dropdown selection into FeaturedPost. Right now in getCMSFields with Category, I have this:

    public function getCMSFields()
    {
    $fields = parent::getCMSFields();
          
    $children = $this->Children();
    $featured_field = new DropDownField('FeaturedPost', 'FeaturedPost', $children);
          
    $fields->addFieldToTab('Root.Content.Main', $featured_field);   
    return $fields;
    }

    The DropDownField populates with the right number of Posts, but it doesn't save into the FeaturedPost relationship, or display the titles of the Posts.

    Any help? Thanks in advance.

  • Willr
    Avatar
    Forum Moderator
    5462 Posts

    Re: [Solved] Populating a DropDownField with Children Link to this post

    With the has_one you need to append ID to the field name:

    $featured_field = new DropDownField('FeaturedPostID', 'FeaturedPost', $children);

    Though $children will be a list of pages, so perhaps you want to ensure you only get Post types in $children.

    $children = DataObject::get("Post", "ParentID = '". $this->ID ."'");

  • Mr. Neave
    Avatar
    Community Member
    8 Posts

    Re: [Solved] Populating a DropDownField with Children Link to this post

    Thanks for the reply Will.

    How do I also get the Post titles to be listed in the DropDownField? Right now it displays:
    Post
    Post
    Post
    etc...

    And it doesn’t seem to save a particular Post as the FeaturedPost when you publish the page. How do you bind the DropDownField to a particular $has_one relationship?

  • Willr
    Avatar
    Forum Moderator
    5462 Posts

    Re: [Solved] Populating a DropDownField with Children Link to this post

    I thought it would have done this by default but you may need to call map() (http://API.silverstripe.org/2.3/sapphire/model/DataObjectSet.html#methodmap)

    $children = DataObject::get("Post", "ParentID = '". $this->ID ."'");
    if($children) $children = $children->map("ID", "Title");

    $featured_field = new DropDownField('FeaturedPostID', 'FeaturedPost', $children);

    How do you bind the DropDownField to a particular $has_one relationship?

    Well the first argument to DropDownFIeld 'FeaturedPostID' should map to your $has_one = array('FeaturedPost'). Have you done a dev/build?flush=1 then reloaded the admin?

  • Mr. Neave
    Avatar
    Community Member
    8 Posts

    Re: [Solved] Populating a DropDownField with Children Link to this post

    That works perfectly. Will, you are a legend. Thank you.

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