Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

All other Modules /

Discuss all other Modules here.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

restfulserver module: subclassing HTMLText to add toXML() not working


Go to End


1014 Views

Avatar
sirocco

Community Member, 14 Posts

6 August 2014 at 9:02pm

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!