0

I have privacy policy document which it is the same across many different websites. I store the document in a Document content type with a Rich Text field called "body". I would like to change the website name and urls programmatically without duplicating the entries.

I tried using embedded entries. For example, having another Content Type called WebSite and there having two fields, title and url. But there is no way to access each specific field in Rich Text so when I parse the payload I don't know when to use the title and when to use the url.

I could have different content types for the website title and the website name but feels like an overkill.

What is the best way to achieve this?

chchrist
  • 18,854
  • 11
  • 48
  • 82

1 Answers1

0

You could solve this a couple of ways.

Shortcodes like you would commonly see in WordPress can be used to great effect here. For example placing [[website_name]] or [[website_url]] in your text, and parsing it during render would be a really simple way of achieving it.

Alternatively, if you do go down the Embedded entry route...

If you're fetching the content via GraphQL, you can get this using the following syntax, you'd need to look up the entry when parsing RichText, but it gives you access to the fields:

content {
    json
    links {
      entries {
        inline {
          sys {
            id
          }
          ... on SiteContentMention { // Have a fragment for each content type you can embed
            title
            url
          }
          __typename
        }
      }
    }
  }

You'll see the data you want in the links object for that field matched by sys.id

If you're fetching content via the regular content delivery API. You'll see the data you want in the includes top level field, again matched by sys.id. You may need to increase the include param's value. Documented here: https://www.contentful.com/developers/docs/references/content-delivery-api/#/reference/links/retrieval-of-linked-items

Steve Vaughan
  • 2,163
  • 13
  • 18
  • Yes, this could work. The only problem I have is that it is a little bit brittle. Since I posted the question I read these articles on microcopies which is what I'm saying I want to avoid but on the other hand maybe is less error prone? https://www.contentful.com/r/knowledgebase/dynamic-microcopy/ https://levelup.gitconnected.com/managing-microcopy-with-react-and-contentful-acae948141ea https://www.contentful.com/blog/2019/05/31/interactive-content-react-rich-text-editor/ – chchrist Jan 13 '20 at 12:28
  • I think it comes down to how you manage the code that is rendering the RichText content. If the shortcode approach feels brittle because there are lots of sites that render the RichText content, and you'd have to maintain each of them with the code required to render the website links, then the approach in that article could work better for you. If it's a shared codebase, then I'd argue that the shortcode approach is fine, if the code is entered incorrectly in Contentful, then it's easy to update. – Steve Vaughan Jan 13 '20 at 19:01