Question

How can default value be set for textareafield in CustomSiteConfig? This doesnt work:

class CustomSiteConfig extends DataObjectDecorator {

    function extraStatics() {
        return array(
            'db' => array(
                'MyText' => 'Text'),
            'defaults' => array( 
                'MyText' => 'Bla Bla'),
            'has_one' => array(
                'Logo' => 'Image')
        );

    }

public function updateCMSFields(FieldSet $fields) {

        $fields->addFieldToTab("Root.Main", new TextareaField("MyText", "My Text"));
        $fields->addFieldToTab("Root.Main", new ImageField('Logo', 'Logo'));

    }
Was it helpful?

Solution

If the statics do not work, probable this untested (since 2.4 is so far away) snippet could help:

public function populateDefaults() {
    parent::populateDefaults();
    $this->owner->MyText = 'Bla Bla';
}

OTHER TIPS

This code works, but maybe not how you expect.

The defaults array allows you to specify simple static values to be the default value for when a record is created. If a record is already created then the values from the defaults array will not be used, even when adding a new variable to that database table.

If you were adding a database variable to a Page, rather than the SiteConfig, and specified it's default value in the defaults array, all existing pages would not get that default value but any new page created after that point would.

Because the SiteConfig record has already been created MyText does not get the default value.

If you create a new site with your code and run dev/build you would find your SiteConfig MyText variable will have the default value of "bla bla".

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top