phptemplatessilverstripe

Can't get my custom template to work with my custom subclass


I'm developing a subclass of DropdownField and I'm trying to couple it with a corresponding DrodownFieldData.ss template without a success.

I'm calling it as in:

return $this->customise($properties)->renderWith('DropdownFieldData');

Do you have any other ideas that I could give a try?


This is the code. It's basically a copy of DropdownField, skimmed down to the Field method.

<?php
class DropdownFieldData extends DropdownField {

    public function Field($properties = array()) {
        $source = $this->getSource();
        $options = array();
        if($source) {
            // SQLMap needs this to add an empty value to the options
            if(is_object($source) && $this->emptyString) {
                $options[] = new ArrayData(array(
                    'Value' => '',
                    'Title' => $this->emptyString,
                ));
            }

            foreach($source as $value => $title) {
                $selected = false;
                if($value === '' && ($this->value === '' || $this->value === null)) {
                    $selected = true;
                } else {
                    // check against value, fallback to a type check comparison when !value
                    if($value) {
                        $selected = ($value == $this->value);
                    } else {
                        $selected = ($value === $this->value) || (((string) $value) === ((string) $this->value));
                    }

                    $this->isSelected = $selected;
                }

                $disabled = false;
                if(in_array($value, $this->disabledItems) && $title != $this->emptyString ){
                    $disabled = 'disabled';
                }

                $options[] = new ArrayData(array(
                    'Title' => $title,
                    'Value' => $value,
                    'Selected' => $selected,
                    'Disabled' => $disabled,
                ));
            }
        }

        $properties = array_merge($properties, array('Options' => new ArrayList($options)));
        return $this->customise($properties)->renderWith('DropdownFieldData');

//      return parent::Field($properties);

    }
}

Solution

  • I had a similar problem that was the result of timing (there was a template loaded later that replaced my own customisation, but only when using the default form template). The fix was making sure the subclassed form field had its own version of the FormField Holder method. EG:

    public function FieldHolder($properties = array()) {
        $obj = $properties ? $this->customise($properties) : $this;
        return $obj->renderWith($this->getTemplates());
    }
    

    The template should be in templates/forms/CustomField.ss. I don't think it should matter if this is in your theme folder, in mysite, or in a module folder.