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.

Data Model Questions

Extend DataObject via subclass (I think this is what I want?)


Reply

3 Posts   445 Views

Avatar
Peavers

30 April 2014 at 8:58am Community Member, 8 Posts

How do I extend/customize a function in a dataobject via subclassing?

I have created a custom class:

class CustomEditableFormField extends EditableFormField
{
public function getFieldConfiguration()
{
//Do custom stuff
}
}


I then added:

Injector:
EditableFormField:
class: CustomEditableFormField

In my config.yml but the subclass is still being ignored. Do I need to include all the functions of EditableFormField like an interface, or is there something bigger I'm missing?

Note: Same question on SO https://stackoverflow.com/questions/23352571/silverstripe-extend-dataobject-via-subclass

Avatar
Tim Snadden

19 May 2014 at 7:58am Community Member, 32 Posts

You are probably looking for Dataextension. http://doc.silverstripe.org/framework/en/reference/dataextension

Avatar
Peavers

19 May 2014 at 8:27am Community Member, 8 Posts

It was my understanding that in order to use DataExtension, the calling method needs to have a hook. The class I'm trying to extend has no such hooks. I could add one, but then I'm still modifying the core of that module which defeats the entire purpose.

Attempted anyhow

class CustomEditableFormField extends DataExtension
{

public function getFieldConfiguration()
{
[...]

$customField= new TextField(
$this->owner->getSettingName('customField'),
_t('customField', 'Custom field'),
$this->owner->getSetting('customField')
);

$fields = FieldList::create(
$ec,
$right,
$customField
);

[...]
}

}

EditableFormField:
extensions:
- CustomEditableFormField

Returns no errors, but doesn't add the customfield as expected.

Note the code to add the customfield works fine if I add it to the core module.

So that leaves me to think the injection point isn't correct/added but not sure how to solve that?