A content type for one entry only

Hello, everyone

This is my first time setting up a space for my project. I have Home Page and Page Footer content types which each only has exactly one active entry at any given time.

Is this an antipattern in Contentful?

Hi @trirawibowo,

As a general rule, I would recommend against building your content models to reflect the layout of your website. Think of content models as the foundation that structures data semantically, and the layout as the tip of the iceberg that is merely one of the many possible ways to display the same data.

Having said that, so called “singletons” i.e. content models that can have only one instance, is indeed a feature requested by many and I can think of several valid use cases for it. However, in your case it might not be the best solution. For example…do Page and HomePage really have any difference when it comes to field structure? How about instead adding a field to Page model that marks it as the home page.

Hi,

Thank you for your response. My initial understanding was not to make a content model for specifically a single page but after some tinkering, I came to realize that such “singleton”, although I found it contradicting database normalization, was actually quite fitting for Contentful as a CMS in my opinion.

In my case, I don’t have a generic layout that is applicable for every page. Some of them are based on the same layout but the home page has its own. Therefore, making a generic Page content model is not what I need as far as I understand.

I have been thinking on Contentful’s lack of “singleton” content types for things like global site settings and similar. It does seem like the App framework could potentially be a solution here. A custom App could have a Footer Content field on the configuration page that could be used in your consumer application. The downside here is that the only way to consume the app installation parameters is with the Contentful Management API/SDK, I don’t think there’s a way to get them through the Delivery API.

I figured out a way to do singletons in Contentful.

Add a text field with a name like “Authors Ignore Me” and or “Unique Singleton ID” and, if you don’t have anything better for a title field, set it to be a title field.

Make it required, unique, having to match a specific pattern, and accepting only specified values. Set the display to radio to make it pretty obvious.

It’s up to you what value you put in the accepted pattern and the accepted value, but make sure it’s the same piece of text. I like to make it a meaningful title for when the object shows up in the content list, like “Site-Wide Settings.”

It’s a bit hacky, but it should keep anyone from creating a duplicate.

Now if only we could get non-standalone objects in Contentful. I want things like the socials and the opening hours of a business in the site-wide settings, and they’re simply not needed outside of those … I don’t want them in the “All” content list overwhelming authors. :frowning: One day, hopefully!

1 Like