Orchardcore crestapps content fields

From CrestApps/CrestApps.AgentSkills · View interactive version

Orchardcore crestapps content fields is an AI agent skill from the GitHub repository CrestApps/CrestApps.AgentSkills. Skill for the CrestApps Content Fields module in Orchard Core, which adds PhoneField, AddressField and Conditional Fields. Covers PhoneField storage with E.164 validation and PhoneFieldSettings, AddressField with country-aware editing, AddressFieldSettings and AddressFieldIndex, Conditional Fields w

How to use this skill

Skill file

CrestApps Content Fields

Add CrestApps content fields

You are an Orchard Core expert. Generate focused code, migrations and recipes for the CrestApps content fields: PhoneField (international phone numbers stored in E.164 with their country), AddressField (a postal address laid out and validated for its country) and Conditional Fields (show any content field only when other answers say so).

Guidelines

Feature overview

| Feature ID | Purpose |

|---|---|

| CrestApps.OrchardCore.ContentFields | PhoneField, its display and settings drivers, the international-telephone-editor script and style |

| CrestApps.OrchardCore.ContentFields.Address | AddressField, its drivers, AddressFieldIndex and Liquid member access |

| CrestApps.OrchardCore.ContentFields.ConditionalFields | ConditionalFieldSettings on every field, server-side hiding, the crestapps-conditional-fields and crestapps-cascading-picker scripts, value readers |

| CrestApps.OrchardCore.Recipes | Adds PhoneFieldSchemaDefinition and AddressFieldSchemaDefinition when enabled with the field features |

{
  "steps": [
    {
      "name": "Feature",
      "enable": [
        "CrestApps.OrchardCore.ContentFields",
        "CrestApps.OrchardCore.ContentFields.Address",
        "CrestApps.OrchardCore.Recipes"
      ],
      "disable": []
    }
  ]
}

PhoneField

| Property | Meaning |

|---|---|

| PhoneNumber | Normalized E.164 phone number |

| CountryCode | ISO 3166-1 alpha-2 region used by the editor |

| NationalNumber | Local number without the calling code |

| PhoneFieldSettings | Default | Meaning |

|---|---|---|

| Hint | null | Editor help text |

| Required | false | Rejects an empty number |

| InitialCountryMode | Globe | Globe (no preselection), CurrentCulture (region of the request culture, none for neutral cultures such as en), or Specific |

| SpecificCountryCode | null | ISO code preselected in Specific mode; the settings driver clears it for other modes |

using CrestApps.OrchardCore.ContentFields.Settings;
using OrchardCore.ContentManagement.Metadata;
using OrchardCore.ContentManagement.Metadata.Settings;
using OrchardCore.Data.Migration;

namespace MyModule.Migrations;

public sealed class ContactMigrations : DataMigration
{
    private readonly IContentDefinitionManager _contentDefinitionManager;

    public ContactMigrations(IContentDefinitionManager contentDefinitionManager)
    {
        _contentDefinitionManager = contentDefinitionManager;
    }

    public async Task<int> CreateAsync()
    {
        await _contentDefinitionManager.AlterPartDefinitionAsync("ContactPart", part => part
            .WithField("Phone", field => field
                .OfType("PhoneField")
                .WithDisplayName("Phone number")
                .WithPosition("1")
                .WithSettings(new PhoneFieldSettings
                {
                    Hint = "Enter an international phone number.",
                    Required = true,
                    InitialCountryMode = InitialCountryMode.Specific,
                    SpecificCountryCode = "US",
                })));

        return 1;
    }
}

Register it with services.AddDataMigration<ContactMigrations>(); in the owning module's startup.

Editor behavior

PhoneFieldDisplayDriver uses the stored CountryCode on edit; for older records without one, it resolves the region from PhoneNumber and derives a national number only when none is stored. A required field rejects an empty number, and an invalid number adds a model-state error. The editor uses the intl-tel-input library from CrestApps.OrchardCore.Resources (see orchardcore-crestapps-resources).

AddressField

AddressField.Address holds the shared Address model of the Addresses module (CrestApps.OrchardCore.Addresses.Models), or null when nothing was entered: Name, Company, AddressLine1, AddressLine2, DependentLocality, City, Region (ISO 3166-2 subdivision code without the country prefix, when the country lists regions), County, District, PostalCode, SortingCode, Country (ISO 3166-1 alpha-2) and Phone. A type can have several, such as Home and Work.

| AddressFieldSettings | Default | Meaning |

|---|---|---|

| Hint | null | Help text above the address |

| Required | false | An address must be entered; an optional blank one is stored as null |

| ShowName, ShowCompany, ShowPhone | true, true, false | Which extra lines the editor asks for |

| Countries | null | ISO codes offered; empty offers every country ICountryService lists |

| DefaultCountry | null | Country a new address starts in |

The editor is the Addresses module's AddressEditor shape bound under the field prefix and validated by IAddressEditorService, so required parts, postal code patterns, region lists and postal-code-in-region checks follow the chosen country. The AddressField display shape prints through IAddressFormatter. AddressField and Address are registered for Liquid member access. See the orchardcore-addresses skill for the address services.

using CrestApps.OrchardCore.ContentFields.Settings;
using OrchardCore.ContentManagement.Metadata;
using OrchardCore.ContentManagement.Metadata.Settings;
using OrchardCore.Data.Migration;

namespace MyModule.Migrations;

public sealed class VenueMigrations : DataMigration
{
    private readonly IContentDefinitionManager _contentDefinitionManager;

    public VenueMigrations(IContentDefinitionManager contentDefinitionManager)
    {
        _contentDefinitionManager = contentDefinitionManager;
    }

    public async Task<int> CreateAsync()
    {
        await _contentDefinitionManager.AlterPartDefinitionAsync("Venue", part => part
            .WithField("Location", field => field
                .OfType("AddressField")
                .WithDisplayName("Location")
                .WithSettings(new AddressFieldSettings
                {
                    Required = true,
                    ShowCompany = false,
                    Countries = ["US", "CA"],
                    DefaultCountry = "CA",
                })));

        return 1;
    }
}

Query content by place

AddressFieldIndex (namespace CrestApps.OrchardCore.ContentFields.Indexes, a ContentFieldIndex) has one row per address field with Country, Region, City and PostalCode. Its provider (AddressFieldIndexProvider : ContentFieldIndexProvider) maps published and latest versions, so it is never single-record per document.

using CrestApps.OrchardCore.ContentFields.Indexes;
using OrchardCore.ContentManagement;
using YesSql;

namespace MyModule.Services;

public sealed class VenueQueries
{
    private readonly ISession _session;

    public VenueQueries(ISession session)
    {
        _session = session;
    }

    public Task<IEnumerable<ContentItem>> GetOntarioVenuesAsync()
        => _session
            .Query<ContentItem, AddressFieldIndex>(index =>
                index.ContentType == "Venue" &&
                index.Country == "CA" &&
                index.Region == "ON" &&
                index.Published)
            .ListAsync();
}

Conditional Fields

Every field's settings gain Show this field when, stored as ConditionalFieldSettings.ShowWhen (a ConditionRule from CrestApps.OrchardCore.Conditions: Match All or Any, and Conditions of Source, Operator, Values). A source is another field as {TypePartName}.{FieldName} or a module-provided value name (such as the Tickets TicketCategory). Operators: Is, IsNot, IsOneOf, IsNoneOf, IsEmpty, IsNotEmpty, Contains, GreaterThan, LessThan, IsOrUnder.

using CrestApps.OrchardCore.Conditions;
using CrestApps.OrchardCore.ContentFields.Settings;
using OrchardCore.ContentManagement.Metadata;
using OrchardCore.ContentManagement.Metadata.Settings;
using OrchardCore.Data.Migration;

namespace MyModule.Migrations;

public sealed class DeviceRequestMigrations : DataMigration
{
    private readonly IContentDefinitionManager _contentDefinitionManager;

    public DeviceRequestMigrations(IContentDefinitionManager contentDefinitionManager)
    {
        _contentDefinitionManager = contentDefinitionManager;
    }

    public async Task<int> CreateAsync()
    {
        await _contentDefinitionManager.AlterPartDefinitionAsync("DeviceRequest", part => part
            .WithField("IsLaptop", field => field
                .OfType("BooleanField")
                .WithDisplayName("Is it a laptop?"))
            .WithField("LaptopModel", field => field
                .OfType("TextField")
                .WithDisplayName("Laptop model")
                .WithSettings(new ConditionalFieldSettings
                {
                    ShowWhen = new ConditionRule
                    {
                        Match = ConditionMatch.All,
                        Conditions =
                        [
                            new Condition { Source = "DeviceRequest.IsLaptop", Operator = ConditionOperator.Is, Values = ["true"] },
                        ],
                    },
                })));

        return 1;
    }
}

Read conditional fields for the server pipeline, the built-in readers, a custom reader, the recipe shape and the cascading picker.

Recipes

With CrestApps Recipes enabled, both fields have a content-definition settings schema and a content payload schema.

{
  "steps": [
    {
      "name": "ContentDefinition",
      "ContentParts": [
        {
          "Name": "ContactPart",
          "ContentPartFieldDefinitionRecords": [
            {
              "Name": "Phone",
              "DisplayName": "Phone number",
              "ContentFieldDefinition": {
                "Name": "PhoneField"
              },
              "Settings": {
                "PhoneFieldSettings": {
                  "Required": true,
                  "InitialCountryMode": "Specific",
                  "SpecificCountryCode": "US"
                }
              }
            },
            {
              "Name": "Mailing",
              "DisplayName": "Mailing address",
              "ContentFieldDefinition": {
                "Name": "AddressField"
              },
              "Settings": {
                "AddressFieldSettings": {
                  "Required": false,
                  "ShowPhone": false,
                  "DefaultCountry": "US"
                }
              }
            }
          ]
        }
      ]
    },
    {
      "name": "content",
      "data": [
        {
          "ContentType": "Contact",
          "DisplayText": "Example contact",
          "ContactPart": {
            "Phone": {
              "PhoneNumber": "+14155552671",
              "CountryCode": "US",
              "NationalNumber": "4155552671"
            },
            "Mailing": {
              "Address": {
                "Name": "Jane Doe",
                "AddressLine1": "1 Market St",
                "City": "San Francisco",
                "Region": "CA",
                "PostalCode": "94105",
                "Country": "US"
              }
            }
          }
        }
      ]
    }
  ]
}

The schemas validate shape only. Imported phone numbers should already be valid E.164 with a matching uppercase country code, and imported addresses are not run through the country rules until an editor saves them.

Troubleshooting

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.