App Framework tutorial: Building a custom reference app
Tags: contentful, webdev
Published: 2021-06-23 14:40:52

I'm cross-posting this article for my [Colleague David Fateh](https://www.contentful.com/blog/author/david-fateh/). Check out the original post over on the [Contentful Blog](https://www.contentful.com/blog/2021/04/22/app-framework-tutorial-custom-reference-app/).

[Shy](https://www.contentful.com/blog/author/shy-ruparel/) and I set out on a [livestream](https://youtu.be/UFMNO5ZXce0) to customize the Contentful reference field by using the [App Framework](https://www.contentful.com/developers/docs/extensibility/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:

![Screenshot of what defining fields in an entry in Contentful looks like](https://images.ctfassets.net/fo9twyrwpveg/1gSlKw9sxIalrwWJOknHFe/6291b23a7754ea967307a6be46ee1fc4/Screen_Shot_2021-03-24_at_21.32.37.png?fm=webp&q=1)

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.

![Gif showing an app that can show more data from the referenced entry.](https://images.ctfassets.net/fo9twyrwpveg/66Bvj5bxfe0HyJTPrgHHiT/ecc8c28289d512e16a773ffcd8529040/reference-app-ui.gif)

Getting started
---------------

To start, we used the [create-contentful-app](https://www.contentful.com/developers/docs/extensibility/app-framework/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](https://f36.contentful.com/) (our design library) and our [open source field editors](https://www.contentful.com/developers/docs/extensibility/app-framework/field-editors/) for easy access.

We get all this by running the commands below:

```sh
npx @contentful/create-contentful-app init reference-field-app
cd reference-field-app
npm start
```

At this point our app is running on `localhost:3000` but won't be accessible until we create the [AppDefinition](https://www.contentful.com/developers/docs/extensibility/app-framework/app-definition/) and select the locations where we want it to show up. Let's do this next.

### Creating the AppDefinition

An [AppDefinition](https://www.contentful.com/developers/docs/extensibility/app-framework/app-definition/) 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.

![Screenshot showing how the process of creating the App Definition works](https://images.ctfassets.net/fo9twyrwpveg/2xIyF8y2qRX0sUopPz2GHe/c3d727a9f1cafc654d1a1151cbc48388/Screen_Shot_2021-03-10_at_10.08.56.png?fm=webp&q=1)

### 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.

![Screenshot showing how to replace a built-in reference app with a custom app](https://images.ctfassets.net/fo9twyrwpveg/48ir5RY90j0ffgAvPY87WF/de2e097f7ce26a733f32bb018d7dd14b/Screen_Shot_2021-04-12_at_13.06.38.png?fm=webp&q=1)

### 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](https://www.npmjs.com/package/@contentful/rich-text-react-renderer) for display use. We are also going to use the handy [MultipleEntryReferenceEditor](https://contentful-field-editors.netlify.app/reference-multiple) which is part of our [open source editor package](https://contentful-field-editors.netlify.app/). 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.


```js
import React, { useEffect } from 'react';
import {
    Card,
    Typography,
    Heading,
    CardActions,
    DropdownList,
    DropdownListItem,
} from '@contentful/forma-36-react-components';
import { MultipleEntryReferenceEditor } from '@contentful/field-editor-reference';
import { documentToReactComponents } from '@contentful/rich-text-react-renderer';
import { FieldExtensionSDK } from '@contentful/app-sdk';
```

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.

```js
const Field = (props: FieldProps) => {
    useEffect(() => {
        props.sdk.window.startAutoResizer();
    });

    return (
        <MultipleEntryReferenceEditor
            renderCustomCard={customRenderer}
            viewType="link"
            sdk={props.sdk}
            isInitiallyDisabled
            hasCardEditActions
            parameters={{
                instance: {
                    showCreateEntityAction: true,
                    showLinkEntityAction: true,
                },
            }}
        />
    );
};
```

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


```js
 const customRenderer = (props: any) => {
    if (props.contentType.sys.id !== 'blogPost') {
        return false;
    }
    const title = props.entity.fields?.title?.[props.localeCode] || 'Untitled';

    return (
        <Card style={{ flexGrow: 1 }} padding="none">
            <div style={{ display: 'flex' }}>
                <div>{props.cardDragHandle}</div>
                <div style={{ flexGrow: 1, padding: '1em' }}>
                    <Typography style={{ marginBottom: '20px' }}>
                        <Heading style={{ borderBottom: '1px solid gray' }}>
                            {title}
                        </Heading>
                        {props.entity.fields.body &&
                            documentToReactComponents(
                                props.entity.fields.body[props.localeCode]
                            )}
                    </Typography>
                </div>
                <div style={{ padding: '1em' }}>
                    <CardActions>
                        <DropdownList>
                            <DropdownListItem onClick={props.onEdit}>
                                Edit
                            </DropdownListItem>
                            <DropdownListItem onClick={props.onRemove}>
                                Remove
                            </DropdownListItem>
                        </DropdownList>
                    </CardActions>
                </div>
            </div>
        </Card>
    );
};
```

Now let's put it all together:


```js
import React, { useEffect } from 'react';
import {
    Card,
    Typography,
    Heading,
    CardActions,
    DropdownList,
    DropdownListItem,
} from '@contentful/forma-36-react-components';
import { MultipleEntryReferenceEditor } from '@contentful/field-editor-reference';
import { documentToReactComponents } from '@contentful/rich-text-react-renderer';
import { FieldExtensionSDK } from '@contentful/app-sdk';

interface FieldProps {
    sdk: FieldExtensionSDK;
}

const customRenderer = (props: any) => {
    if (props.contentType.sys.id !== 'blogPost') {
        return false;
    }
    const title = props.entity.fields?.title?.[props.localeCode] || 'Untitled';

    return (
        <Card style={{ flexGrow: 1 }} padding="none">
            <div style={{ display: 'flex' }}>
                <div>{props.cardDragHandle}</div>
                <div style={{ flexGrow: 1, padding: '1em' }}>
                    <Typography style={{ marginBottom: '20px' }}>
                        <Heading style={{ borderBottom: '1px solid gray' }}>
                            {title}
                        </Heading>
                        {props.entity.fields.body &&
                            documentToReactComponents(
                                props.entity.fields.body[props.localeCode]
                            )}
                    </Typography>
                </div>
                <div style={{ padding: '1em' }}>
                    <CardActions>
                        <DropdownList>
                            <DropdownListItem onClick={props.onEdit}>
                                Edit
                            </DropdownListItem>
                            <DropdownListItem onClick={props.onRemove}>
                                Remove
                            </DropdownListItem>
                        </DropdownList>
                    </CardActions>
                </div>
            </div>
        </Card>
    );
};

const Field = (props: FieldProps) => {
    useEffect(() => {
        props.sdk.window.startAutoResizer();
    });

    return (
        <MultipleEntryReferenceEditor
            renderCustomCard={customRenderer}
            viewType="link"
            sdk={props.sdk}
            isInitiallyDisabled
            hasCardEditActions
            parameters={{
                instance: {
                    showCreateEntityAction: true,
                    showLinkEntityAction: true,
                },
            }}
        />
    );
};

export default Field;
```

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](https://github.com/davidfateh/reference-field-app).

### 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](https://www.contentful.com/slack/) 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](https://www.twitch.tv/contentfuldevs) and [YouTube](https://www.youtube.com/channel/UCrsEcxPCJlz5yZ_FKGo6D9g) where we code live and work through problems on the fly!

If you haven't signed up for a free Contentful account yet, register for a Community edition!