How to create modular content record

Hey guys, I am trying to create a record with modular content with the document but I really don’t get it document

Here’s my code, can anyone give me some examples?

const ModularModelBlock1 = await client.itemTypes.create({
    name: 'BasicModularModel1',
    apiKey: 'basic_modular_model1',
    modularBlock: true,
  });
  const ModularModelBlock2 = await client.itemTypes.create({
    name: 'BasicModularModel2',
    apiKey: 'basic_modular_model2',
    modularBlock: true,
  });
  const ModularModelBlock = await client.itemTypes.create({
    name: 'BasicModularModel',
    apiKey: 'basic_modular_model',
  });

  const titleField = await client.fields.create(ModularModelBlock1.id, {
    label: 'BasicTitle',
    apiKey: 'basic_title',
    fieldType: 'string',
    validators: {
      required: {},
    },
    appearance: {
      editor: 'single_line',
      parameters: {
        heading: true,
      },
      addons: [],
    },
  });

  const DescriptionField = await client.fields.create(ModularModelBlock2.id, {
    label: 'BasicDescription',
    apiKey: 'basic_description',
    fieldType: 'text',
    validators: {
      required: {},
    },
    appearance: {
      editor: 'markdown',
      addons: [],
      parameters: {
      },
    },
  });

  const field = await client.fields.create(ModularModelBlock.id, {
    label: "Content",
    fieldType: "rich_text",
    apiKey: "content",
    validators: {
      richTextBlocks: {
        itemTypes: [ModularModelBlock1.id, ModularModelBlock2.id],
      },
    },
  });

Hello @jeff.f.chen

I’m not sure i understood what you are trying to do, but here are some notes:
An important thing is, blocks can only be created and used inside a Modular Content field or a Structured Text field inside a record. You can’t create stand alone blocks.

To build a modular block inside one of those fields, you can call the function buildModularBlock, that you can import from datocms-client

This part of the documentation page should make things clearer

Yeah I was trying to add blocks into modular content field.
And I fix the error.

'use strict';
const { buildModularBlock } = require("datocms-client");
module.exports = async (client) => {
  // DatoCMS migration script

  // For more examples, head to our Content Management API docs:
  // https://www.datocms.com/docs/content-management-api

  // Create an Article model:
  // https://www.datocms.com/docs/content-management-api/resources/item-type/create

  const ModularModelBlock1 = await client.itemTypes.create({
    name: 'BasicModularModel1',
    apiKey: 'basic_modular_model1',
    modularBlock: true,
  });
  const ModularModelBlock2 = await client.itemTypes.create({
    name: 'BasicModularModel2',
    apiKey: 'basic_modular_model2',
    modularBlock: true,
  });
  const ModularModelBlock = await client.itemTypes.create({
    name: 'BasicModularModel',
    apiKey: 'basic_modular_model',
  });

  const titleField = await client.fields.create(ModularModelBlock1.id, {
    label: 'BasicTitle',
    apiKey: 'basic_title',
    fieldType: 'string',
    validators: {
      required: {},
    },
    appearance: {
      editor: 'single_line',
      parameters: {
        heading: true,
      },
      addons: [],
    },
  });

  const DescriptionField = await client.fields.create(ModularModelBlock2.id, {
    label: 'BasicDescription',
    apiKey: 'basic_description',
    fieldType: 'text',
    validators: {
      required: {},
    },
    appearance: {
      editor: 'markdown',
      addons: [],
      parameters: {
      },
    },
  });

  const field = await client.fields.create(ModularModelBlock.id, {
    label: "Content",
    fieldType: "rich_text",
    apiKey: "content",
    validators: {
      richTextBlocks: {
        itemTypes: [ModularModelBlock1.id, ModularModelBlock2.id],
      },
    },
  });

  const record = await client.items.create({
    itemType: ModularModelBlock.id, // model ID
    content: [
      buildModularBlock({
        itemType: ModularModelBlock1.id,
        basic_title: "test title",
      }),
      buildModularBlock({
        itemType: ModularModelBlock2.id,
        basic_description: "test description",
      }),
    ]

  });
}

In the record part, I need to add content [] to contain the buildModularBlock function.

1 Like