Faloude Faloude - 2 months ago 25
PHP Question

SilverStripe change field label from parent class

Within

SiteConfig
there is a
TextField
Site title. I'm trying to change the label of this textfield
$titleField
through a SiteConfig extension in class
SiteConfigExtension extends DataExtension
.

Here is the code from siteconfig/code/model/ where it's created:

$fields = new FieldList(
new TabSet("Root",
$tabMain = new Tab('Main',
$titleField = new TextField("Title", _t('SiteConfig.SITETITLE', "Site title")),
....


Q: What's the easiest way to replace the SiteTitle label without having to remove the field in the SiteConfig extension and re-add it again with the desired label?

Answer

Updating the title from SiteConfigExtension uses updateCMSFields...

class SiteConfigExtension extends DataExtension {

    public function updateCMSFields(FieldList $fields) {
        if ($titleField = $fields->dataFieldByName('Title'))
            $titleField->setTitle('my title');
    }

}
Comments