Jump to:

3460 Posts in 1064 Topics by 739 members

Data Model Questions

SilverStripe Forums » Data Model Questions » [SOLVED] How to create non-static default values or preselect DropDownFields?

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

Page: 1
Go to End
Author Topic: 1857 Views
  • bkausbk
    Avatar
    Community Member
    11 Posts

    [SOLVED] How to create non-static default values or preselect DropDownFields? Link to this post

    Is there a way to create non static default values. Usually all default values are defined in $default array like:

    static $defaults = array(
       'Live' => true
    );

    I want to assign the ID of the member who created a news article page. For this purpose I created a relation like:

    public static $has_one = array(
       'Author' => 'Member'
    );

    This will create a new AuthorID in my NewsArticle related database table.
    Each time a registered user creates a new NewsArticle, a DropDownField on CMS page should be displayed with preselected own Member (Member::currentUserID()) name. The user/news administrator should be able to change the member who created the specified news article.

    The problem is, how can I pre select that member in the created DropDownList? What I tried so far is following:

    [...]
    $members = DataObject::get('Member');
    if ($members) {
       $membersmap = $members->map_multiple("ID", array("FirstName", "Surname"));
    } else {
       $membersmap = null;
    }
    $authorfield = new DropdownField('AuthorID', 'Author', $membersmap, Member::currentUserID());
    $fields->addFieldToTab('Root.Content.Main', $authorfield, 'Content');

    As I found out, the 4th argument in DropDownField ctor is the default value. But if a new news article page is created this 4th argument will be ignored, is always reset to 0. What can I do here to pre select a specified entry in DropDownField?

    Even $authorfield->value = Member::currentUserID() didn't work. Is there maybe a bug in SilverStripe?
    I'm using SilverStripe 2.3.4.

    Thank's for help,
    bkausbk

    Attached Files
  • bkausbk
    Avatar
    Community Member
    11 Posts

    Re: [SOLVED] How to create non-static default values or preselect DropDownFields? Link to this post

    Does anybody has an idea here? Or how would you solve my problem in another way?

  • CodeGuerrilla
    Avatar
    Community Member
    105 Posts

    Re: [SOLVED] How to create non-static default values or preselect DropDownFields? Link to this post

    * deleted *

  • ajshort
    Avatar
    Community Member
    244 Posts

    Re: [SOLVED] How to create non-static default values or preselect DropDownFields? Link to this post

    The way to do this is to overload populateDefaults() in your SiteTree sub-class - this is called when a new object is created. Just make sure to call the parent method as well. Something like this should do the job:

    public function populateDefaults() {
       $this->AuthorID = Member::currentUserID();
       parent::populateDefaults();
    }

  • bkausbk
    Avatar
    Community Member
    11 Posts

    Re: [SOLVED] How to create non-static default values or preselect DropDownFields? Link to this post

    Thank you ajshort, this was exactly what I was looking for.

    But what is the 4th argument in DropDownField for? As far as I found out by viewing the source is that the 4th argument is the default value for that DropDownField.

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