Michael McFadden: Plone Outputfilters and TransformChain (how to display things differently)
Talk by Michael McFadden at the online Plone Conference 2021.
I work for Radio Free Asia.
Let's jump right into the problem. There should be some kind of 'hook' to modify 'stuff' before we send it to 'the browser'. For example: TinyMCE images, safe-html transforms, Mosaic blocks. Intercept the response, do something with it, and send it on.
The solutions in Plone: plone.outputfilters and plone.transformchain.
plone.outputfilters: tools for modifying field values when you get() them. Demo: add an image and then use this in a news item or document in TinyMCE. Save it, and you see the caption of the image. Where does this come from?
- story.text.raw has just an image.
- story.text.output suddenly has a caption.
plone/outputfilter/browser/configure.zcml has a browser page plone.outputfilters_captioned_image. If you don't like it, you can easily override it with jbot, overrides.zcml or a browser layer.
For Radio Free Asia, we want AMP. This is a special webpage for Google to quickly show news items, and Google caches it. We need an updated caption for this, but only when there is an AMP request.
First try: special browser view, let it add an interface on the request, and let the snippet return something else.
Problem: the output is cached, so you have the original non-AMP answer in a Zope cache.
Plone Outputfilters hooks into the Products.PortalTransforms machinery. It registers its own mimetype and transform for safe html transform.
Solution: create a new mimetype text/x-html-safe-for-amp and transform policy. Manually call the transformer in the browser page. We now have two versions of the caption snippet in the cache.
On to plone.transformchain. It provides methods to modify the response from a page before it is returned to the browser. It is used by plone.app.theming, plone.app.blocks, plone.protect, plone.app.caching, etc. They are called in a specific order.
At RFA, we use this to convert between language alphabets. Uygur Arabic is transformed into Latin or Cyrillic.
We have some example private code, you can send me a mail to get access.