Skip to content

Commit

Permalink
Assign the technical name of the field as the display name by default
Browse files Browse the repository at this point in the history
Fix #14570
  • Loading branch information
MikeAlhayek committed Oct 23, 2023
1 parent e228e45 commit 951c1a2
Showing 1 changed file with 17 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -123,6 +124,11 @@ public ContentPartDefinitionBuilder WithField(string fieldName)

public ContentPartDefinitionBuilder WithField(string fieldName, Action<ContentPartFieldDefinitionBuilder> 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)
{
Expand All @@ -137,13 +143,21 @@ public ContentPartDefinitionBuilder WithField(string fieldName, Action<ContentPa
existingField = new ContentPartFieldDefinition(null, fieldName, new JObject());
}
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);
configuration(configurer);
_fields.Add(configurer.Build());
return this;
}

public async Task<ContentPartDefinitionBuilder> WithFieldAsync(string fieldName, Func<ContentPartFieldDefinitionBuilder, Task> 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)
Expand All @@ -160,6 +174,9 @@ public async Task<ContentPartDefinitionBuilder> 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);

Expand Down

0 comments on commit 951c1a2

Please sign in to comment.