You are here: Home / What do you need? / Help and documentation / Plone Info / Using portal_transforms

Using portal_transforms

by Darrell Kingsley last modified Mar 13, 2014 01:04 PM
The portal transforms tool helps convert one type of content to another. Here are a few reminders about what to do.

To convert content from one sort to another, especially when converting text to web_intelligent_text or HTML to safe_html, you need the portal_transforms tool.

Example 1 - convert raw HTML to safe_html

    from Products.CMFCore.utils import getToolByName
    portal_transforms = getToolByName(self, 'portal_transforms')
    rawhtml = '<div>Some HTML which may contain nasty code</div>
    datastream = portal_transforms.convertTo('text/x-html-safe', rawhtml, mimetype='text/html')
    finalhtml = datastream.getData()

Example 2 - convert text saved as web_intelligent_text into HTML

The text is saved as web_intelligent_text from a textarea set up to save text as that. It then needs to be converted into HTML as follows:

    from Products.CMFCore.utils import getToolByName
    portal_transforms = getToolByName(context, 'portal_transforms')
    txt = 'stuff from the field with e.g. a link to http://www.beetlebrow.co.uk'
    data = portal_transforms.convertTo('text/x-web-intelligent', txt, mimetype='text/html')
    return data.getData()

Or then there's this alternative, if you know the name of a transform:

html = str(context.portal_transforms.convert('web_intelligent_plain_text_to_html', desc))

I don't know if the str() is required because it's actually a datastream. We should try html.getData() to check.

Useful links