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();
}
protected static CompositionType CalculateCompositionType(TContentType source, IContentTypeComposition contentTypeComposition)
=> contentTypeComposition.Id == source.ParentId
protected static CompositionType CalculateCompositionType(int contentTypeParentId, IContentTypeComposition contentTypeComposition)
=> contentTypeComposition.Id == contentTypeParentId
? CompositionType.Inheritance
: 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.DocumentType;
using Umbraco.Cms.Core.Mapping;
@@ -37,11 +36,14 @@ public class DocumentTypeMapDefinition : ContentTypeMapDefinition<IContentType,
target.AllowedDocumentTypes = source.AllowedContentTypes?.Select(ct =>
new DocumentTypeSort { DocumentType = new ReferenceByIdModel(ct.Key), SortOrder = ct.SortOrder })
.ToArray() ?? Enumerable.Empty<DocumentTypeSort>();
target.Compositions = source.ContentTypeComposition.Select(contentTypeComposition => new DocumentTypeComposition
{
DocumentType = new ReferenceByIdModel(contentTypeComposition.Key),
CompositionType = CalculateCompositionType(source, contentTypeComposition)
}).ToArray();
target.Compositions = MapNestedCompositions(
source.ContentTypeComposition,
source.ParentId,
(referenceByIdModel, compositionType) => new DocumentTypeComposition
{
DocumentType = referenceByIdModel,
CompositionType = compositionType,
});
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.MediaType;
using Umbraco.Cms.Core.Mapping;
@@ -38,11 +37,14 @@ public class MediaTypeMapDefinition : ContentTypeMapDefinition<IMediaType, Media
target.AllowedMediaTypes = source.AllowedContentTypes?.Select(ct =>
new MediaTypeSort { MediaType = new ReferenceByIdModel(ct.Key), SortOrder = ct.SortOrder })
.ToArray() ?? Enumerable.Empty<MediaTypeSort>();
target.Compositions = source.ContentTypeComposition.Select(contentTypeComposition => new MediaTypeComposition
{
MediaType = new ReferenceByIdModel(contentTypeComposition.Key),
CompositionType = CalculateCompositionType(source, contentTypeComposition)
}).ToArray();
target.Compositions = MapNestedCompositions(
source.ContentTypeComposition,
source.ParentId,
(referenceByIdModel, compositionType) => new MediaTypeComposition
{
MediaType = referenceByIdModel,
CompositionType = compositionType,
});
}
// Umbraco.Code.MapAll

View File

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