Skip to content

Commit

Permalink
Set a default display name only when none is specified (OrchardCMS#15013
Browse files Browse the repository at this point in the history
)

Co-authored-by: Mike Alhayek <mike@crestapps.com>
  • Loading branch information
2 people authored and urbanit committed Mar 18, 2024
1 parent 8b87aad commit 6d9089e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public ContentPartDefinition Build()
public ContentPartDefinitionBuilder Named(string name)
{
Name = name;

return this;
}

Expand All @@ -72,19 +73,22 @@ public ContentPartDefinitionBuilder RemoveField(string fieldName)
{
_fields.Remove(existingField);
}

return this;
}

[Obsolete("Use WithSettings<T>. This will be removed in a future version.")]
public ContentPartDefinitionBuilder WithSetting(string name, string value)
{
_settings[name] = value;

return this;
}

public ContentPartDefinitionBuilder MergeSettings(JObject settings)
{
_settings.Merge(settings, ContentBuilderSettings.JsonMergeSettings);

return this;
}

Expand All @@ -101,6 +105,7 @@ public ContentPartDefinitionBuilder MergeSettings<T>(Action<T> setting) where T
var settingsToMerge = existingJObject.ToObject<T>();
setting(settingsToMerge);
_settings[typeof(T).Name] = JObject.FromObject(settingsToMerge, ContentBuilderSettings.IgnoreDefaultValuesSerializer);

return this;
}

Expand Down Expand Up @@ -134,12 +139,19 @@ 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);

if (string.IsNullOrEmpty(existingField.DisplayName()))
{
// If there is no display name, let's use the field name by default.
configurer.WithDisplayName(fieldName);
}

_fields.Add(configurer.Build());

return this;
}

Expand Down Expand Up @@ -182,11 +194,14 @@ 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);

if (string.IsNullOrEmpty(existingField.DisplayName()))
{
// If there is no display name, let's use the field name by default.
configurer.WithDisplayName(fieldName);
}

_fields.Add(configurer.Build());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public static string DisplayName(this ContentPartFieldDefinition partField)

if (string.IsNullOrEmpty(displayName))
{
displayName = partField.FieldDefinition.Name;
displayName = partField.FieldDefinition?.Name;
}

return displayName;
Expand Down

0 comments on commit 6d9089e

Please sign in to comment.