diff --git a/src/OrchardCore.Modules/OrchardCore.ContentTypes/Services/ContentDefinitionService.cs b/src/OrchardCore.Modules/OrchardCore.ContentTypes/Services/ContentDefinitionService.cs index 401f4e50ad6..660da6d2bab 100644 --- a/src/OrchardCore.Modules/OrchardCore.ContentTypes/Services/ContentDefinitionService.cs +++ b/src/OrchardCore.Modules/OrchardCore.ContentTypes/Services/ContentDefinitionService.cs @@ -322,7 +322,7 @@ public async Task AddFieldToPartAsync(string fieldName, string displayName, stri { if (string.IsNullOrEmpty(fieldName)) { - throw new ArgumentException("The 'fieldName' can't be null or empty.", nameof(fieldName)); + throw new ArgumentException("The '{fieldName}' can't be null or empty.", nameof(fieldName)); } var partDefinition = await _contentDefinitionManager.LoadPartDefinitionAsync(partName); @@ -390,9 +390,20 @@ await _contentDefinitionManager.AlterTypeDefinitionAsync(typeDefinition.Name, ty public Task AlterTypePartsOrderAsync(ContentTypeDefinition typeDefinition, string[] partNames) => _contentDefinitionManager.AlterTypeDefinitionAsync(typeDefinition.Name, type => { + if (partNames is null) + { + return; + } + for (var i = 0; i < partNames.Length; i++) { - var partDefinition = typeDefinition.Parts.FirstOrDefault(x => x.Name == partNames[i]); + var partDefinition = typeDefinition.Parts?.FirstOrDefault(x => x.Name == partNames[i]); + + if (partDefinition == null) + { + continue; + } + type.WithPart(partNames[i], partDefinition.PartDefinition, part => { part.MergeSettings(x => x.Position = i.ToString()); @@ -403,6 +414,11 @@ public Task AlterTypePartsOrderAsync(ContentTypeDefinition typeDefinition, strin public Task AlterPartFieldsOrderAsync(ContentPartDefinition partDefinition, string[] fieldNames) => _contentDefinitionManager.AlterPartDefinitionAsync(partDefinition.Name, type => { + if (fieldNames is null) + { + return; + } + for (var i = 0; i < fieldNames.Length; i++) { var fieldDefinition = partDefinition.Fields.FirstOrDefault(x => x.Name == fieldNames[i]); @@ -437,9 +453,9 @@ public async Task GenerateFieldNameFromDisplayNameAsync(string partName, var type = await _contentDefinitionManager.LoadTypeDefinitionAsync(partName) ?? throw new ArgumentException("The part doesn't exist: " + partName); - var typePart = type.Parts.FirstOrDefault(x => x.PartDefinition.Name == partName); + var typePart = type.Parts?.FirstOrDefault(x => x.PartDefinition.Name == partName); - // Id passed in might be that of a type w/ no implicit field. + // If passed in might be that of a type w/ no implicit field. if (typePart == null) { return displayName;