Reputation: 8655
I'm developing a subclass of DropdownField and I'm trying to couple it with a corresponding DrodownFieldData.ss template without a success.
/simple/templates
, /simple/templates/forms
, /simple/templates/Includes
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);
}
}
Upvotes: 1
Views: 1031
Reputation: 8655
The key was to keep the template in [yourmodule]/templates
, rather than in any theme's location.
Upvotes: 0
Reputation: 61
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.
Upvotes: 0
Reputation: 989
You could try the following:
You'll also find that the fields are rendered using two templates, one of them being suffixed with '_holder', which acts as a wrapper, while the other one renders the field itself, so depending on how you want to customise your field, you might have to create both.
Have a look at the FormField class to get a better understanding on how fields are rendered, as they use a slightly different mechanism than page types
Upvotes: 0