How to render assets (images) in rich text coming from Contentful

Viewed 252

I've set up Contentful as the headless CMS of my blog. Everything works, except rendering assets (in my case images specifically). My blog is built using Symfony and uses Twig for templating.

This is how (a part of the) content is currently rendered:

enter image description here

I'm using Contentful's default rich text renderer for Twig, as described here. Unfortunately, it doesn't say anything about assets. But given that there's an EmbeddedImage class available, I would expect support for images.

What step am I missing?

1 Answers

You need to define a custom renderer for the assets in the same way as described for a custom heading here.

The reason why assets don't work "out of the box" is that the renderer can't know what type of asset is being returned. For example, is it a .jpg, .mp4, .wav file etc. You may have a variety of different assets in your Rich Text and will need to render the different asset files accordingly to your HTML.

The full list of node types you can define custom renderers for is here.

Related