contentful-management

Adding a media field when creating a new entry


I've searched around and looked at all the SDK docs and on Contentful's API docs I am having difficulty understanding how to add a media field with a link to an asset when creating a new entry. I can successfully create the other fields, but a media field should be an object but I am not sure exactly how to format that so Contentful will accept it.

const videoAsset = yield client.getAsset(assetID)

fields = {
  title: {
    "en-US": 'Title' //works
  },
  description: {
    "en-US": 'Description' //works
  },
  video: {
    "en-US": //contenful api wants an object, what does this object look like?
               //i have a published asset in videoAsset returned by client.getAsset()
  },
  team: {
    "en-US": 'Community' //works 
  },
}

const entryCreated = yield space.createEntry(action.payload.contentType, {
    fields: fields
})

When I I say "works" I mean that I can successfully create an entry that appears in Contentful space.


Solution

  • I got it!

    This person wasn't doing exactly the same thing but the answer in terms of formatting was here:

    https://github.com/contentful/contentful-management.js/issues/57

    Basically the field should look like this:

    const videoAsset = yield client.getAsset(assetID)
    
    fields = {
      title: {
        "en-US": 'Title' //works
      },
      description: {
        "en-US": 'Description' //works
      },
      video: { 
        "en-US": {
           sys: {
             type: "Link", linkType: "Asset", id: assetID
           }
         }
      }, //this formatting works!
      team: {
        "en-US": 'Community' //works 
      },
    }
    
    const entryCreated = yield space.createEntry(action.payload.contentType, {
        fields: fields
    })