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 /

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

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


Go to End
Reply


3 Posts   685 Views

Avatar
Peavers

Community Member, 15 Posts

30 April 2014 at 8:58am

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

Community Member, 32 Posts

19 May 2014 at 7:58am

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

Avatar
Peavers

Community Member, 15 Posts

19 May 2014 at 8:27am

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?