Jump to:

5513 Posts in 1732 Topics by 1218 members

Customising the CMS

SilverStripe Forums » Customising the CMS » Select value in dropdownfield by default based on parent

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

Page: 1
Go to End
Author Topic: 1370 Views
  • klikhier
    Avatar
    Community Member
    150 Posts

    Select value in dropdownfield by default based on parent Link to this post

    What I want to do is to select a certain value in a dropdownfield by default based on the value of the parent (that has the exact same dropdownfield). Why can't I get this field to display the parent value by default in the CMS?

    public static $db = array(
    'ResponsibleOffice' => 'Int'
    };

    (...)

    /**
    * Overload so that the default value is same as parent.
    * Based on same function in BlogEntry.php
    */
    public function populateDefaults() {
    parent::populateDefaults();
    $this->setField('ResponsibleOffice', $parent->ResponsibleOffice);
    }

    (...)

    public function getCMSFields() {
    $fields = parent::getCMSFields();

    $options = array('Value1', 'Value2', 'Value3', 'Value4');
    $fields->addFieldToTab("Root.Content.Main", new DropdownField("ResponsibleOffice", "Responsible office", $options), 'Content');

    return $fields;
    }   

  • klikhier
    Avatar
    Community Member
    150 Posts

    Re: Select value in dropdownfield by default based on parent Link to this post

    Found populatedefaults recipe here. Following this example I now have:

       public function populateDefaults() {
          parent::populateDefaults();
          $parent_object = $this->getParent();
          $this->ResponsibleOffice = $parent_object->getField('ResponsibleOffice');
       }

    Now, when adding this page as a child to another page, I get a 'Javascript parse error' in the CMS. What am I doing wrong?

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