Domanda

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

  • I flushed the cache repeatedly.
  • I deleted the cache folder on my localhost (XAMPP)
  • I moved the template around to various locations of my 'simple' theme: /simple/templates, /simple/templates/forms, /simple/templates/Includes
  • As you can see, the template name does not have an underscore character which has been reported to cause problems

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);

    }
}
È stato utile?

Soluzione

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.

Altri suggerimenti

You could try the following:

  • rename your template DropdownFieldData_holder.ss
  • move DropdownFieldData_holder.ss into mysite/templates/DropdownFieldData_holder.ss
  • use the setFieldHolderTemplate method on your field, ie $this->setFieldHolderTemplate('DropdownFieldData_holder');

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

The key was to keep the template in [yourmodule]/templates, rather than in any theme's location.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top