diff --git a/src/OrchardCore/OrchardCore.ContentManagement.Abstractions/Metadata/Builders/ContentPartDefinitionBuilder.cs b/src/OrchardCore/OrchardCore.ContentManagement.Abstractions/Metadata/Builders/ContentPartDefinitionBuilder.cs index 0a0424bb6c1..d343767417b 100644 --- a/src/OrchardCore/OrchardCore.ContentManagement.Abstractions/Metadata/Builders/ContentPartDefinitionBuilder.cs +++ b/src/OrchardCore/OrchardCore.ContentManagement.Abstractions/Metadata/Builders/ContentPartDefinitionBuilder.cs @@ -4,6 +4,7 @@ using System.Threading.Tasks; using Newtonsoft.Json.Linq; using OrchardCore.ContentManagement.Metadata.Models; +using OrchardCore.ContentManagement.Metadata.Settings; using OrchardCore.ContentManagement.Utilities; namespace OrchardCore.ContentManagement.Metadata.Builders @@ -123,6 +124,11 @@ public ContentPartDefinitionBuilder WithField(string fieldName) public ContentPartDefinitionBuilder WithField(string fieldName, Action configuration) { + if (string.IsNullOrWhiteSpace(fieldName)) + { + throw new ArgumentException($"'{nameof(fieldName)}' cannot be null or empty."); + } + var existingField = _fields.FirstOrDefault(x => string.Equals(x.Name, fieldName, StringComparison.OrdinalIgnoreCase)); if (existingField != null) { @@ -137,6 +143,9 @@ public ContentPartDefinitionBuilder WithField(string fieldName, Action WithFieldAsync(string fieldName, Func configurationAsync) { + if (string.IsNullOrWhiteSpace(fieldName)) + { + throw new ArgumentException($"'{nameof(fieldName)}' cannot be null or empty."); + } + var existingField = _fields.FirstOrDefault(x => string.Equals(x.Name, fieldName, StringComparison.OrdinalIgnoreCase)); if (existingField != null) @@ -160,6 +174,9 @@ public async Task WithFieldAsync(string fieldName, } var configurer = new FieldConfigurerImpl(existingField, _part); + // Assume that the display name is the same as the field name. + // Set the display name before invoking the given action, to allow the action to set the display-name explicitly. + configurer.WithDisplayName(fieldName); await configurationAsync(configurer);