Block

View as Markdown

Block

Block describes one block type of a ModularBlockField. Each block has a title, a UID, and its own schema of fields.

When to Use This Class: Use this class to define the block types that a modular blocks field offers, for example a Hero block and a Text block.

Conceptual Role: Acts as the data model of one item in the blocks list of a modular blocks field.

Returns: Block. One block type of a modular blocks field.

Properties

NameTypeRequiredDefaultDescription
Titlestring?NonullName of the block in the entry editor. Serialized as title.
Uidstring?NonullUnique ID of the block within the field. Serialized as uid.
AutoEditboolNofalseAuto-edit setting of the block. Serialized as autoEdit.
BlockTypeboolNofalseBlock type setting of the block. Serialized as blockType.
SchemaList<Field>?NonullThe fields inside the block. Serialized as schema.

Class-Level Notes

Serialization.

  • The SDK always sends autoEdit and blockType, with false when you leave them unset. Their JSON keys use camel case.
  • Add any Field subclass to Schema. The SDK sends each nested field with the properties of its subclass.

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

Block has no methods. Set its properties and add the object to ModularBlockField.blocks.

Class-Level Snippet

The snippet below creates a content type with a modular blocks field that offers one quote 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 = "Quote",
                    Uid = "quote",
                    Schema = new List<Field>
                    {
                        new TextboxField { DisplayName = "Quote Text", Uid = "quote_text", DataType = "text" },
                        new TextboxField { DisplayName = "Author", Uid = "author", DataType = "text" }
                    }
                }
            }
        }
    }
};

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}");
}