Skip to content

Commit

Permalink
Fix an exception when creating content type without any part (Orchard…
Browse files Browse the repository at this point in the history
  • Loading branch information
giannik authored and urbanit committed Mar 18, 2024
1 parent 1d42634 commit fa6e997
Showing 1 changed file with 20 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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<ContentTypePartSettings>(x => x.Position = i.ToString());
Expand All @@ -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]);
Expand Down Expand Up @@ -437,9 +453,9 @@ public async Task<string> 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;
Expand Down

0 comments on commit fa6e997

Please sign in to comment.