-
Notifications
You must be signed in to change notification settings - Fork 150
/
15-derive-entry-n-to-1.js
45 lines (40 loc) · 1.74 KB
/
15-derive-entry-n-to-1.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// In this example, we want to turn the dog's owner field into its own entry
// and link it back to the dog. To do this, we create an "owner"
// content type and a link field on the "dog" content type.
// The link field is a singular Entry link field. (See example 20 if you want to create an Array link field.)
// In the identity function, we define the criterion for when a new owner should
// be created: If the name joined by a hyphen is the same, then the same owner entry is
// linked.
// In the deriveLinkedEntries function, we define what values should go into the new
// owner entries. We don't create any values for the locale 'en-US' on the derived entries.
module.exports = function (migration) {
const owner = migration.createContentType('owner').name('Owner').description('An owner of a dog');
owner.createField('firstName').type('Symbol').name('First Name');
owner.createField('lastName').type('Symbol').name('Last Name');
owner.displayField('firstName');
const dog = migration.editContentType('dog');
dog.createField('ownerRef').type('Link').linkType('Entry').name('The Owner');
migration.deriveLinkedEntries({
contentType: 'dog',
derivedContentType: 'owner',
from: ['owner'],
toReferenceField: 'ownerRef',
derivedFields: ['firstName', 'lastName'],
identityKey: async (fromFields) => {
return fromFields.owner['en-US'].toLowerCase().replace(' ', '-');
},
shouldPublish: true,
deriveEntryForLocale: async (inputFields, locale) => {
if (locale !== 'en-US') {
return;
}
const [firstName, lastName] = inputFields.owner[locale].split(' ');
return {
firstName,
lastName
};
}
});
dog.deleteField('owner');
dog.changeFieldId('ownerRef', 'owner');
};