Skip navigation

Extend the content editing features with custom elements

5 min read
Download PDF

You can extend Kontent.ai by implementing extra features you need for your project, such as a 3rd-party video service. These extensions are called custom elements. Custom elements help you expand the built-in functionality of Kontent.ai and take the authoring experience to next level.

Table of contents

    Key points

    • A custom element is a small HTML app that acts as an extension of Kontent.ai's native capabilities.
    • To use your extension in a Kontent.ai project, add a custom element into a content type and specify the URL of your extension.
    • You need to securely host your custom elements yourself.

    What is a custom element?

    Custom elements are small HTML apps that exist in a sandboxed <iframe> in Kontent.ai and interact with Kontent.ai through the Custom Elements API. They enable you to extend the default content type elements to include the functionality you need.

    Have a look at a few examples of extending Kontent.ai capabilities using custom elements:

    • Enable selecting assets from a 3rd-party digital asset management system (DAM), such as Bynder.
    • Add an AI-powered content personalization system like Recombee.
    • Let your content creators write the way they're used to by implementing a custom WYSIWYG text editor.

    To see all the custom element samples, check out our gallery of custom elements. There, you can choose whatever suits your needs and add it to Kontent.ai.

    We're using the Markdown editor for example purposes in this article. You can apply the same principles to add any custom element from the gallery. 

    If you don't find in the gallery what you need, you can always create your own custom element.

    Host your custom element securely

    Custom elements are HTML applications loaded in an <iframe>. They need to be hosted so the browser can fetch the file. You need to host your custom element yourself or use a service like Netlify.

    • If you decide to host your custom element on your own servers, you need to
      • setup the appropriate domain certificate for it;
      • use the X-frame-options (or Content-Security-Policy) header to specify an allowed origin, in this case, https://app.kontent.ai. For more details, see the MDN Web Docs.
    • To test your custom element locally, we suggest to use a secure tunnel to your localhost. You can use a service like ngrok that connects to your local application and provides a secure public URL for access from Kontent.ai.

    Your custom element is separated from the rest of Kontent.ai through the sandbox attribute on the <iframe>. The following sandbox flags are enabled:

    • allow-forms – allows form submission
    • allow-scripts – allows JavaScript to run inside the <iframe>
    • allow-popups – allows popups
    • allow-same-origin – allows the document to maintain its origin
    • allow-modals – allows modal dialogs

    Add the custom element to your project

    To have your custom element appear in content items, you need to add it to the content type of the items.

    1. In Content model, open a content type for editing.
    2. Click Custom element on the right to add a new custom element.
    3. Name the element.
    4. In Hosted code URL (HTTPS), paste the URL where you host your custom element.
    5. (Optional) In Parameters {JSON}, enter configuration parameters if your custom element has any.
    6. Click Save changes.

    If you set the custom element as required, Kontent.ai checks if the element contains any value. Any other value than null is considered valid and won't prevent an item from getting published.

    JSON parameters

    The JSON parameters enable you to configure your element and reuse it in various contexts. In some cases, you may want different settings for different projects, content types, or even elements. 

    For instance, in the case of the Markdown editor, you could configure yours to accept JSON parameters for activating and deactivating spellcheck and tools like inserting images. Then, you would enable spellcheck or images only in places where it makes sense.

    You could also change the custom editor's HTML so that all of the tools in it can be activated and deactivated via JSON parameters. That way, each custom element could have a different toolset.

    Never store your API keys or any other sensitive data in the JSON parameters field. It's not secure! Use server-side proxy to store sensitive data.

    See the final result

    After you're done setting up the content type, you can use the custom element inside content items. If you implement the Markdown editor, it looks like this:

    When you fetch this content item via the Delivery API, you'll get your markdown in the value of the custom element, similar to the following.

    • JSON
    { ... "markdown_editor": { "type": "custom", "name": "Markdown editor", "value": "# What is Kontent.ai and how it helps you\n\nThere are a lot of terms that put [Kontent.ai](https://kontent.ai/) into the right box—modulra content platform, headless CMS, omnichannel content hub, [content infrastructure](https://kontent.ai/learn/tutorials/references/terminology/#a-content-infrastructure), API-first CMS. Let's look into **how Kontent helps your business**.\n\n## The one and only place for content\n\nWith Kontent.ai, you get a _single place to manage all your content_. Especially in **large organizations**, content tends to be everywhere, created in different systems that aren't connected, which is a nightmare for everyone. \n\n![An image](https://assets-us-01.kc-usercontent.com/8d20758c-d74c-4f59-ae04-ee928c0816b7/6e38c039-ad00-47f3-a584-26252827655a/daylight.jpg)" } ... }
    { ... "markdown_editor": { "type": "custom", "name": "Markdown editor", "value": "# What is Kontent.ai and how it helps you\n\nThere are a lot of terms that put [Kontent.ai](https://kontent.ai/) into the right box—modulra content platform, headless CMS, omnichannel content hub, [content infrastructure](https://kontent.ai/learn/tutorials/references/terminology/#a-content-infrastructure), API-first CMS. Let's look into **how Kontent helps your business**.\n\n## The one and only place for content\n\nWith Kontent.ai, you get a _single place to manage all your content_. Especially in **large organizations**, content tends to be everywhere, created in different systems that aren't connected, which is a nightmare for everyone. \n\n![An image](https://assets-us-01.kc-usercontent.com/8d20758c-d74c-4f59-ae04-ee928c0816b7/6e38c039-ad00-47f3-a584-26252827655a/daylight.jpg)" } ... }

    What's next?