Skip to content

Instantly share code, notes, and snippets.

@Jimmydalecleveland
Last active August 1, 2019 15:06
Show Gist options
  • Save Jimmydalecleveland/79e9bfa67ce663b5986ab10328894f6d to your computer and use it in GitHub Desktop.
Save Jimmydalecleveland/79e9bfa67ce663b5986ab10328894f6d to your computer and use it in GitHub Desktop.
Contentful Migration of button to CTA.
// 1) Runs first, Initial creation of 'button'
const button = migration
.createContentType('button')
.name('Button')
.description(
'Global button type including things such as site links and call now buttons'
)
.displayField('identifier')
button
.createField('identifier')
.name('Identifier')
.type('Symbol')
.validations([{ unique: true }])
.required(true)
button
.createField('type')
.name('Type')
.type('Symbol')
.validations([{ in: ['Default', 'Phone Number'] }])
.required(true)
button
.createField('text')
.name('Text')
.type('Symbol')
.required(true)
button
.createField('destination')
.name('Destination')
.type('Symbol')
.validations([
{
regexp: {
pattern: '^(w+:{0,1}w*@)?(S+)(:[0-9]+)?(/|/([w#!:.?+=&%@!-/]))?$',
},
},
])
// 3) Runs last
const uuid = require('uuid/v4')
module.exports = migration => {
migration.transformEntriesToType({
sourceContentType: 'button',
targetContentType: 'cta',
from: ['identifier', 'type', 'text', 'destination'],
shouldPublish: true,
updateReferences: true,
removeOldEntries: true,
identityKey: (() => uuid()),
transformEntryForLocale: (fromFields, currentLocale) => ({
identifier: fromFields.identifier[currentLocale],
type: fromFields.type[currentLocale] === 'Phone Number' ? 'Phone' : 'Link',
buttonText: fromFields.text[currentLocale],
destinationURL: fromFields.destination && fromFields.destination[currentLocale],
})
})
}
// 2) Runs second
module.exports = migration => {
const cta = migration
.createContentType('cta')
.name('CTA')
.description('A call to action')
.displayField('identifier')
cta
.createField('identifier')
.name('Identifier')
.type('Symbol')
.validations([{ unique: true }])
.required(true)
cta
.createField('type')
.name('Type')
.type('Symbol')
.validations([{ in: ['Link', 'Phone', 'Zip', 'Newsletter'] }])
.required(true)
cta
.createField('buttonText')
.name('Button Text')
.type('Symbol')
.required(true)
cta
.createField('labelText')
.name('Label Text')
.type('Symbol')
cta
.createField('placeholderText')
.name('Placeholder Text')
.type('Symbol')
cta
.createField('destinationURL')
.name('Destination URL')
.type('Symbol')
.validations([
{
message: 'Must be a valid URL format',
regexp: {
pattern: `((http(s)?:\\/\\/.)?(www\\.)?[-a-zA-Z0-9@:%._\\+~#=]{2,256}\\.[a-z]{2,6}\\b([-a-zA-Z0-9@:%_\\+.~#?&//=]*))|(^\\/[-a-zA-Z0-9@:%._\\+~#=]{0,256})`,
},
},
])
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment