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.

Customising the CMS

Select value in dropdownfield by default based on parent


Reply

2 Posts   1392 Views

Avatar
klikhier

21 May 2009 at 2:53am (Last edited: 21 May 2009 2:54am), Community Member, 150 Posts

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;
}   

Avatar
klikhier

21 May 2009 at 10:14pm Community Member, 150 Posts

Found populatedefaults recipe [url=http://doc.silverstripe.org/doku.php?id=recipes:populatedefaults]here[/url]. 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?