Custom reference field apps in the Contentful App Framework

Published on April 22, 2021

App Framework tutorial: Custom reference app

Subscribe for updates

Build better digital experiences with Contentful updates direct to your inbox.

Shy and I set out on a livestream to customize the Contentful reference field by using the App Framework to build a custom React app that would provide us with some new and novel functionality. While our progress during the live stream was good, I took the liberty of tightening up the styling and making sure additional functionality was possible. In this post, I’m going to show you how I created a custom reference field app which provides custom functionality.

Building on the App Framework

Building an app on the App Framework as a developer requires an understanding of how editors — the people who use the Contentful web app — may want to manipulate and view entry data. We know that everyone has different use cases and so the customization allowed by the App Framework can be useful when trying to create an experience that will help solve these unique cases. 

When you define fields in an entry, you are defining how data should be displayed. In the case of the reference field, data such as the title of the entry is displayed by default like in the screenshot below:

Defining fields in an entry in Contentful

We can enhance this experience by creating a visually similar custom app using the App Framework that can show more data from the referenced entry such as the body of the post, not just the title.

reference-app-ui

Getting started

To start, we used the create-contentful-app CLI tool. The tool is available for free to developers, and they can use it to quickly get an app up and running. The create-contentful-app CLI tool creates a React app project with Forma 36 (our design library) and our open source field editors for easy access.

We get all this by running the commands below:

At this point our app is running on localhost:3000 but won’t be accessible until we create the AppDefinition and select the locations where we want it to show up. Let’s do this next.

Creating the AppDefinition

An AppDefinition is the entity that represents an app in Contentful. You can think of it as a sort of blueprint for how your app will interact inside the Contentful experience.

You must have an admin or developer account in your Contentful organization. Many developers find it easiest to create a free Contentful organization for a developer environment. You can also develop an app in your primary organization if you prefer to develop in a space or environment which isn’t production facing.

To create the AppDefinition, head to your organization settings and click on Apps in the top menu bar. Once on the AppDefinition page, click the button to create a new app.

First, we are going to name our app: Custom Reference Field. Next, we will make its app URL http://localhost:3000. This is where our local app is currently running. Last, we are going to select the field location and pick the reference field (many) and click the confirm button to save this AppDefinition.

Creating App Definition in Contentful

Replacing a built-in reference field with our custom app

Let’s now head over to a space where we want to see our app show up. In the top menu of our space or environment, click Apps then Manage apps. From here we can find our newly created Custom Reference Field app and install it into our space.

Since we are building an app that will take over a reference field, we must also have a content type that makes use of a reference field. For our purposes, we are going to use a blog post list content type as an example.

In our content model, we are going to adjust the field of our blog post list to use our app by editing the settings of the reference field and selecting appearance. Let’s choose our newly created Custom Reference Field app.

Replacing a built-in reference field with our custom app

Coding the app for custom functionality

Once the app is assigned to a field, we can head over to our entries section and find an entry to see how the app is displayed. In our example, we have a reference field which links to different blog posts. Each blog post has a rich text field which we’d like to use to display some more custom information — something that will give us a bit more functionality than the default reference field experience.

Let’s create some custom functionality. First, we are going to replace our Hello World component with components from Forma 36, as well as utilities from React and a Contentful rich text renderer for display use. We are also going to use the handy MultipleEntryReferenceEditor which is part of our open source editor package. We are using the rich text renderer in this situation because our referenced content model of blog posts uses a rich text field for its body. Depending on the content type, you are referencing in your custom reference field app, displaying this data through different custom or open source components is possible.

Next, let’s replace the Field component with custom code using the MultipleEntryReferenceEditor. You’ll notice that while this markup is not very complicated, we will have to implement a customRenderer for our MultipleEntryReferenceEditor to show properly.

Let’s create the custom renderer function which will render some more React components:

Now let’s put it all together:

We now have a working component that not only shows the title of the blog post but also the first line of the blog post. We have successfully transformed the default experience of the reference field to show data that is more conducive for our use-case. If you’re interested in seeing the full code, check out the repo here.

Wrapping up

For our purposes, changing the fields inside of an entry can be a very powerful tool. There are many cases for why you may want to either slightly modify existing functionality or totally recreate your own. For the cases where you’d like to simply modify the web app, we provide the default fields as a React component in our open source editor library. For cases where you’d like to create your own experience, Forma 36 can be an invaluable tool for achieving a very fluid look and feel of your UI without having to spend time messing with layout yourself.

That said, it is always interesting to see the different ways developers have come together and built their own components for the UI/UX they envision for their users. Many developers make use of our Slack Community where help and ideas are easily shared. I’m active on the channel and am always happy to help explore ideas or guide other developers through the app creation process. 

If you’d like to join our community, you can take advantage of some of the cool things we are doing and discussing over there. If you are interested in seeing more video tutorials, check out our weekly streams on Twitch and YouTube where we code live and work through problems on the fly!

Subscribe for updates

Build better digital experiences with Contentful updates direct to your inbox.

Related articles

Migrating your website to Contentful? This article will line out the steps you need to take to ensure your migration process is as smooth as possible.
Guides

Migrating to Contentful from Wordpress is far easier than it may seem

July 10, 2024

Guides

The understated innovation of static site generators

July 17, 2018

An enterprise CMS is a content management system that meets the needs of enterprise-class companies. Read on and decide if your organization needs one.
Guides

What is an enterprise CMS?

January 19, 2024

Contentful Logo 2.5 Dark

Ready to start building?

Put everything you learned into action. Create and publish your content with Contentful — no credit card required.

Get started