Converting a specific content type to another type and update link of the references

Hello

We have a content type called “Category” which has a field called “body” which is a reference field that supports multiple references of type “Teaser”. Now we have an updated content type of “Teaser” called “TeaserV2”. We want to convert all the "Teaser"s to “TeaserV2” maintaining the links and order. How that can be done with migration?

I was thinking to use transformEntries but since the links / references do not contained the whole document I am not sure how to do that.

Can anyone please help?

Thanks

Hi @aveltmp_bctgn

To migrate, what I assume is a non-localised Teaser to TeaserV2, both being one-to-many reference fields, maintaining both the links and the order, you could try something like this:

module.exports = function (migration, context) {
  migration.transformEntries({
    contentType: 'category',
    from: ['teaser'],
    to: ['teaserv2'],
    transformEntryForLocale: function (fromFields, currentLocale) {
      let newTeaserV2 = []

      let referencedItems = fromFields['teaser']
      if (referencedItems !== undefined) {
        let arrayOfReference = referencedItems['en-GB']
        let arrayOfReferenceLength = arrayOfReference?.length ?? 0

        for (let i = 0; i < arrayOfReferenceLength; i++) {
          if (arrayOfReference[i]?.sys?.id !== undefined) {
            newTeaserV2.push({
              sys: {
                type: 'Link',
                linkType: 'Entry',
                id: arrayOfReference[i].sys.id
              }
            })
          }
        }
      }

      return {
        teaserv2: newTeaserV2
      }
    }
  })
}

Hi @Alma
Thank you for the reply. It seems to me I was not able to explain the issue correctly.


I’ve added a diagram to explain what I’m trying to do. “Category” content type has a field called “body” which is a reference field that supports several content types including “Teaser”, “Recommendation”, etc. We enable the “body” to also support “TeaserV2” and now we want to migrate all “Teaser” to “TeaserV2” while maintaining the references of the original “Teaser” in “body” and possibly all other places where the “Teaser” is being referenced.

Thanks