Hi
I have a DataObect class, KBArticle, which is accessible via the restfulserver module (https://github.com/silverstripe/silverstripe-restfulserver).
One of the properties of KBArticle was Content and of type HTMLText (http://api.silverstripe.org/3.1/class-HTMLText.html), but when accessing the object via the api the value of this property, being HTML, should properly be wrapped in [CDATA[]], so I created a subclass of HTMLText called CustomHTMLText, which added the method toXML() so that the XMLDataFormatter would invoke this, and the CustomHTMLText::toXML method could do the wrapping in CDATA.
But when I took a look at the XMLDataFormatter source (https://github.com/silverstripe/silverstripe-framework/blob/3.1/api/XMLDataFormatter.php#L60) it looked as though this should be happening anyway.
Regardless, I implemented CustomHTMLText with toXML, and changed the type of the Content property of KBArticle to be CustomHTMLText, but the toXML method is not invoked.
Furthermore, looking more at the XMLDataFormatter source, it seems strange that it is checking that $fieldValue->hasMethod('toXML') instead of $fieldType (or even $fieldName?)...
Anyway, my questions are:
- any reason why CustomHTMLText:toXML not getting invoked?
- can anyone clarify why the pre-coded CDATA wrapping in XMLDataFormatter is not getting invoked for HTMLText properies in the first place?
class KBArticle extends DataObject {
static $db = array(
'Title' => 'Varchar(300)',
'Content' => 'CustomHTMLText',
);
...
class CustomHTMLText extends HTMLText {
public function toXML() {
return sprintf('<![CDATA[%s]]>', str_replace(']]>', ']]]]><![CDATA[>', $this->value));
}
}
Thanks in advance!