Orchardcore content definition handlers

From CrestApps/CrestApps.AgentSkills · View interactive version

Orchardcore content definition handlers is an AI agent skill from the GitHub repository CrestApps/CrestApps.AgentSkills. Skill for intercepting Orchard Core content definitions as they are built with IContentDefinitionHandler, and for creating system-defined content types, parts, and fields that users cannot remove or modify through the admin UI or recipes. Covers the ContentTypeBuilding, ContentPartBuilding, ContentT

How to use this skill

Skill file

Orchard Core Content Definition Handlers & System-Defined Definitions

Intercept content definitions as they are built

You are an Orchard Core expert. IContentDefinitionHandler lets a module intercept and modify content type, part, and field definitions while they are being built, before they are cached. The primary use is injecting a part into a content type programmatically and marking types, parts, or fields as system-defined so users cannot remove or modify them through the admin UI or recipes. This complements — it does not replace — the IContentDefinitionManager migration APIs (see the orchardcore-content-types skill) used for ordinary, user-manageable definitions.

Guidelines

Inject a system-defined part into a content type

This handler attaches an SeoPart to every content type whose stereotype is Page, and marks the part system-defined so users cannot detach it. It mirrors the built-in DashboardPartContentTypeDefinitionHandler.

using System.Text.Json.Nodes;
using OrchardCore.ContentManagement.Metadata.Records;
using OrchardCore.ContentManagement.Metadata.Settings;
using OrchardCore.ContentTypes.Events;

namespace MyModule;

public sealed class SeoPartContentTypeDefinitionHandler : IContentDefinitionHandler
{
    // Attach SeoPart to any type with the `Page` stereotype.
    public void ContentTypeBuilding(ContentTypeBuildingContext context)
    {
        if (context?.Record?.Settings is null ||
            !context.Record.Settings.TryGetPropertyValue(nameof(ContentTypeSettings), out var node))
        {
            return;
        }

        var settings = node.ToObject<ContentTypeSettings>();

        if (!string.Equals(settings.Stereotype, "Page", StringComparison.OrdinalIgnoreCase))
        {
            return;
        }

        // Do not add it twice.
        if (context.Record.ContentTypePartDefinitionRecords
            .Any(x => x.Name.EqualsOrdinalIgnoreCase(nameof(SeoPart))))
        {
            return;
        }

        context.Record.ContentTypePartDefinitionRecords.Add(new ContentTypePartDefinitionRecord
        {
            Name = nameof(SeoPart),
            PartName = nameof(SeoPart),
            Settings = new JsonObject
            {
                [nameof(ContentSettings)] = JObject.FromObject(new ContentSettings
                {
                    IsSystemDefined = true,
                }),
            },
        });
    }

    // Mark the type-part system-defined so it cannot be removed from the type.
    public void ContentTypePartBuilding(ContentTypePartBuildingContext context)
    {
        if (context?.Record?.Settings is null ||
            !context.Record.PartName.EqualsOrdinalIgnoreCase(nameof(SeoPart)))
        {
            return;
        }

        var settings = context.Record.Settings[nameof(ContentSettings)]?.ToObject<ContentSettings>()
            ?? new ContentSettings();

        settings.IsSystemDefined = true;

        context.Record.Settings[nameof(ContentSettings)] = JObject.FromObject(settings);
    }

    // Create the part definition on the fly if the module does not define it via migration.
    public void ContentPartBuilding(ContentPartBuildingContext context)
    {
        if (context.Record is not null || context.PartName != nameof(SeoPart))
        {
            return;
        }

        context.Record = new ContentPartDefinitionRecord
        {
            Name = context.PartName,
            Settings = new JsonObject
            {
                [nameof(ContentPartSettings)] = JObject.FromObject(new ContentPartSettings
                {
                    Attachable = false,
                    Reusable = false,
                }),
                [nameof(ContentSettings)] = JObject.FromObject(new ContentSettings
                {
                    IsSystemDefined = true,
                }),
            },
        };
    }

    public void ContentPartFieldBuilding(ContentPartFieldBuildingContext context)
    {
    }
}

Register the handler in Startup:

using Microsoft.Extensions.DependencyInjection;
using OrchardCore.ContentTypes.Events;
using OrchardCore.Modules;

namespace MyModule;

public sealed class Startup : StartupBase
{
    public override void ConfigureServices(IServiceCollection services)
    {
        services.AddScoped<IContentDefinitionHandler, SeoPartContentTypeDefinitionHandler>();
    }
}

Setting Attachable = false on the injected part keeps it out of the "add part" list, so the part exists only where the handler places it.

Read the system-defined flag

ContentSettings lives in OrchardCore.ContentManagement.Metadata.Settings; the GetSettings<T>() accessor is on the definition models in OrchardCore.ContentManagement.Metadata.Models.

using OrchardCore.ContentManagement.Metadata.Models;
using OrchardCore.ContentManagement.Metadata.Settings;

var isSystem = typeDefinition.GetSettings<ContentSettings>().IsSystemDefined;

The same call works on ContentTypeDefinition, ContentPartDefinition, ContentTypePartDefinition, and ContentPartFieldDefinition.

What "system-defined" enforces

Troubleshooting

See references/content-definition-handler-examples.md for a system-defined field example.

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.