ModularBlockField
ModularBlockField
ModularBlockField describes a modular blocks field in a content type or global field schema, with data_type set to blocks. Each block type is a Block with its own schema, and an entry holds any number of blocks in any order.
When to Use This Class: Use this class for a page builder field. For example, in a Sections field an editor adds hero banners and text sections in any order.
Conceptual Role: Acts as the Field subclass that the SDK creates for a field with data_type set to blocks.
Returns: ModularBlockField. A modular blocks field of a schema.
Properties
ModularBlockField inherits every property of Field and adds the property below.
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
| blocks | List<Block>? | No | null | The block types an entry can use. Serialized as blocks. |
Class-Level Notes
Property name.
- The property name blocks is lowercase in the SDK. Write field.blocks, not field.Blocks.
Known issue When you read a schema with client.SerializerOptions or OpenTResponse<T>(), fields nested inside a GroupField or a modular block come back as plain Field objects without their subclass properties, such as format, reference_to, enum, min, and max. Sending that schema back with Update drops those properties from the nested fields. Edit the response as a JsonObject and send it back as JSON, or rebuild each nested field as its typed subclass before you call Update.
Method Index
ModularBlockField has no methods. Set its properties and add it to ContentModelling.Schema.
Class-Level Snippet
The snippet below creates a content type with a modular blocks field that offers a hero block and a text block.
using System;
using System.Collections.Generic;
using Contentstack.Management.Core;
using Contentstack.Management.Core.Exceptions;
using Contentstack.Management.Core.Models;
using Contentstack.Management.Core.Models.Fields;
ContentstackClient client = new ContentstackClient("<AUTHTOKEN>");
ContentModelling model = new ContentModelling
{
Title = "Blog Post",
Uid = "blog_post",
Schema = new List<Field>
{
new TextboxField { DisplayName = "Title", Uid = "title", DataType = "text", Mandatory = true, Unique = true },
new ModularBlockField
{
DisplayName = "Sections",
Uid = "sections",
DataType = "blocks",
Multiple = true,
blocks = new List<Block>
{
new Block
{
Title = "Hero",
Uid = "hero",
Schema = new List<Field> { new TextboxField { DisplayName = "Heading", Uid = "heading", DataType = "text" } }
},
new Block
{
Title = "Text",
Uid = "text",
Schema = new List<Field>
{
new TextboxField
{
DisplayName = "Body",
Uid = "body",
DataType = "text",
FieldMetadata = new FieldMetadata { AllowRichText = true, Multiline = true }
}
}
}
}
}
}
};
try
{
ContentstackResponse response = client.Stack("<API_KEY>").ContentType().Create(model);
Console.WriteLine(response.OpenJsonObjectResponse()["notice"]);
}
catch (ContentstackErrorException e)
{
// Thrown when the API rejects the request
Console.WriteLine($"Request failed: {(int)e.StatusCode} {e.ErrorCode} {e.ErrorMessage}");
}