0
votes

I want to extend the DataObject (EditableDateField) of a module that I use (UserDefinedForms). There I want to overwrite a certain method (getFormField). I was trying to extend with DataExtension. But it does not work.

Here the code...

config.php:

EditableDateField::add_extension("CustomEditableDateField");

CustomEditableDateField:

class CustomEditableDateField extends DataExtension {
    public function getFormField() {
        //test is function called
        echo 'test';
        exit();
    }
} 

Also I tried to use Object::useCustomClass in the config to replace the whole EditableDateField with my CustomClass, but also no success.

What is the best way to do that?

Many thanx, Florian

1
by overriding getFormField what are you trying to do? Might be another function that you can use and has a extend callback? - colymba
i want to do this and force always the same dateformat in a multilingual website: public function getFormField() { $defaultValue = ($this->getSetting('DefaultToToday')) ? date('Y-m-d') : $this->Default; $field = new DateField( $this->Name, $this->Title, $defaultValue); $field->setConfig('showcalendar', true); $field->setConfig('dateformat', 'dd.MM.yyyy'); return $field; } - spierala
not a 100% sure but then just to set the dateformat couldn't you use the onBeforeRender callback on your extension? - colymba
thx. the basic problem is that the function in the extension is not called. if I read the documentation of DataExtension again, I wonder if it is actually possible to just overwrite an existing method. - spierala
I don't think you can overwrite anything you want, DataExtension have special hooks called by extend() on the original class, on FormFields onBeforeRender is the only hook being call on extensions. If you want to overwrite any function you'll have to create a custom class that extends EditableDateField. - colymba

1 Answers

1
votes

To do what you are wanting to achieve, you don't need to extend EditableDateField.

What you are looking for is EditableDateField_FormField which extends DateField. DateField does the heavy lifting for generating the HTML and doing the validation etc. One thing that DateField does have is a static variable called default_config which looks like this:

static $default_config = array(
    'showcalendar' => false,
    'jslocale' => null,
    'dmyfields' => false,
    'dmyseparator' => '&nbsp;<span class="separator">/</span>&nbsp;',
    'dmyplaceholders' => true,
    'dateformat' => null,
    'datavalueformat' => 'yyyy-MM-dd',
    'min' => null,
    'max' => null,
);

Using the configuration system in Silverstripe, you can change the default dateformat to your dd.MM.yyyy format by using:

EditableDateField_FormField:
  default_config:
    'dateformat': 'dd.MM.yyyy'

You will want to use EditableDateField_FormField like my example above rather than change the default_config of DateField itself otherwise you may encounter issues in the CMS.

Technical Description

This works due to the constructor of DateField setting the instance config to the value of the default_config.

Because the value of dateformat is normally NULL in default_config, an if-statement passes which causes dateformat to be set the result from i18n::get_date_format().