V14: Return all (even nested) compositions when getting a content type by id (#15800)

* Adding a generic implementation to get all content type compositions, even the nested ones

* Modifying mappers to use the generic implementation
This commit is contained in:
Elitsa Marinovska
2024-03-01 10:23:40 +01:00
committed by GitHub
parent 1bcd45f4df
commit 33c1d4e1fb
4 changed files with 49 additions and 23 deletions

View File

@@ -78,8 +78,29 @@ public abstract class ContentTypeMapDefinition<TContentType, TPropertyTypeModel,
.ToArray(); .ToArray();
} }
protected static CompositionType CalculateCompositionType(TContentType source, IContentTypeComposition contentTypeComposition) protected static CompositionType CalculateCompositionType(int contentTypeParentId, IContentTypeComposition contentTypeComposition)
=> contentTypeComposition.Id == source.ParentId => contentTypeComposition.Id == contentTypeParentId
? CompositionType.Inheritance ? CompositionType.Inheritance
: CompositionType.Composition; : CompositionType.Composition;
protected static IEnumerable<T> MapNestedCompositions<T>(IEnumerable<IContentTypeComposition> directCompositions, int contentTypeParentId, Func<ReferenceByIdModel, CompositionType, T> contentTypeCompositionFactory)
{
var allCompositions = new List<T>();
foreach (var composition in directCompositions)
{
CompositionType compositionType = CalculateCompositionType(contentTypeParentId, composition);
T contentTypeComposition = contentTypeCompositionFactory(new ReferenceByIdModel(composition.Key), compositionType);
allCompositions.Add(contentTypeComposition);
// When we have composition inheritance, we have to find all ancestor compositions recursively
if (compositionType == CompositionType.Inheritance && composition.ContentTypeComposition.Any())
{
var nestedCompositions = MapNestedCompositions(composition.ContentTypeComposition, composition.ParentId, contentTypeCompositionFactory);
allCompositions.AddRange(nestedCompositions);
}
}
return allCompositions;
}
} }

View File

@@ -1,5 +1,4 @@
using Umbraco.Cms.Api.Management.Extensions; using Umbraco.Cms.Api.Management.Mapping.ContentType;
using Umbraco.Cms.Api.Management.Mapping.ContentType;
using Umbraco.Cms.Api.Management.ViewModels; using Umbraco.Cms.Api.Management.ViewModels;
using Umbraco.Cms.Api.Management.ViewModels.DocumentType; using Umbraco.Cms.Api.Management.ViewModels.DocumentType;
using Umbraco.Cms.Core.Mapping; using Umbraco.Cms.Core.Mapping;
@@ -37,11 +36,14 @@ public class DocumentTypeMapDefinition : ContentTypeMapDefinition<IContentType,
target.AllowedDocumentTypes = source.AllowedContentTypes?.Select(ct => target.AllowedDocumentTypes = source.AllowedContentTypes?.Select(ct =>
new DocumentTypeSort { DocumentType = new ReferenceByIdModel(ct.Key), SortOrder = ct.SortOrder }) new DocumentTypeSort { DocumentType = new ReferenceByIdModel(ct.Key), SortOrder = ct.SortOrder })
.ToArray() ?? Enumerable.Empty<DocumentTypeSort>(); .ToArray() ?? Enumerable.Empty<DocumentTypeSort>();
target.Compositions = source.ContentTypeComposition.Select(contentTypeComposition => new DocumentTypeComposition target.Compositions = MapNestedCompositions(
{ source.ContentTypeComposition,
DocumentType = new ReferenceByIdModel(contentTypeComposition.Key), source.ParentId,
CompositionType = CalculateCompositionType(source, contentTypeComposition) (referenceByIdModel, compositionType) => new DocumentTypeComposition
}).ToArray(); {
DocumentType = referenceByIdModel,
CompositionType = compositionType,
});
if (source.AllowedTemplates != null) if (source.AllowedTemplates != null)
{ {

View File

@@ -1,5 +1,4 @@
using Umbraco.Cms.Api.Management.Extensions; using Umbraco.Cms.Api.Management.Mapping.ContentType;
using Umbraco.Cms.Api.Management.Mapping.ContentType;
using Umbraco.Cms.Api.Management.ViewModels; using Umbraco.Cms.Api.Management.ViewModels;
using Umbraco.Cms.Api.Management.ViewModels.MediaType; using Umbraco.Cms.Api.Management.ViewModels.MediaType;
using Umbraco.Cms.Core.Mapping; using Umbraco.Cms.Core.Mapping;
@@ -38,11 +37,14 @@ public class MediaTypeMapDefinition : ContentTypeMapDefinition<IMediaType, Media
target.AllowedMediaTypes = source.AllowedContentTypes?.Select(ct => target.AllowedMediaTypes = source.AllowedContentTypes?.Select(ct =>
new MediaTypeSort { MediaType = new ReferenceByIdModel(ct.Key), SortOrder = ct.SortOrder }) new MediaTypeSort { MediaType = new ReferenceByIdModel(ct.Key), SortOrder = ct.SortOrder })
.ToArray() ?? Enumerable.Empty<MediaTypeSort>(); .ToArray() ?? Enumerable.Empty<MediaTypeSort>();
target.Compositions = source.ContentTypeComposition.Select(contentTypeComposition => new MediaTypeComposition target.Compositions = MapNestedCompositions(
{ source.ContentTypeComposition,
MediaType = new ReferenceByIdModel(contentTypeComposition.Key), source.ParentId,
CompositionType = CalculateCompositionType(source, contentTypeComposition) (referenceByIdModel, compositionType) => new MediaTypeComposition
}).ToArray(); {
MediaType = referenceByIdModel,
CompositionType = compositionType,
});
} }
// Umbraco.Code.MapAll // Umbraco.Code.MapAll

View File

@@ -1,5 +1,4 @@
using Umbraco.Cms.Api.Management.Mapping.ContentType; using Umbraco.Cms.Api.Management.Mapping.ContentType;
using Umbraco.Cms.Api.Management.ViewModels;
using Umbraco.Cms.Api.Management.ViewModels.MemberType; using Umbraco.Cms.Api.Management.ViewModels.MemberType;
using Umbraco.Cms.Core.Mapping; using Umbraco.Cms.Core.Mapping;
using Umbraco.Cms.Core.Models; using Umbraco.Cms.Core.Models;
@@ -30,12 +29,14 @@ public class MemberTypeMapDefinition : ContentTypeMapDefinition<IMemberType, Mem
target.IsElement = source.IsElement; target.IsElement = source.IsElement;
target.Containers = MapPropertyTypeContainers(source); target.Containers = MapPropertyTypeContainers(source);
target.Properties = MapPropertyTypes(source); target.Properties = MapPropertyTypes(source);
target.Compositions = MapNestedCompositions(
target.Compositions = source.ContentTypeComposition.Select(contentTypeComposition => new MemberTypeComposition source.ContentTypeComposition,
{ source.ParentId,
MemberType = new ReferenceByIdModel(contentTypeComposition.Key), (referenceByIdModel, compositionType) => new MemberTypeComposition
CompositionType = CalculateCompositionType(source, contentTypeComposition) {
}).ToArray(); MemberType = referenceByIdModel,
CompositionType = compositionType,
});
} }
// Umbraco.Code.MapAll -Collection // Umbraco.Code.MapAll -Collection