-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
Shapes.cs
100 lines (80 loc) · 4.52 KB
/
Shapes.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using OrchardCore.ContentManagement;
using OrchardCore.ContentManagement.Display;
using OrchardCore.DisplayManagement;
using OrchardCore.DisplayManagement.Descriptors;
using OrchardCore.DisplayManagement.ModelBinding;
using OrchardCore.DisplayManagement.Utilities;
namespace OrchardCore.Contents
{
public class Shapes : ShapeTableProvider
{
public override ValueTask DiscoverAsync(ShapeTableBuilder builder)
{
builder.Describe("Content")
.OnDisplaying(displaying =>
{
var shape = displaying.Shape;
var contentItem = shape.GetProperty<ContentItem>("ContentItem");
if (contentItem != null)
{
// Alternates in order of specificity.
// Display type > content type > specific content > display type for a content type > display type for specific content
// BasicShapeTemplateHarvester.Adjust will then adjust the template name
// Content__[DisplayType] e.g. Content-Summary
displaying.Shape.Metadata.Alternates.Add("Content_" + displaying.Shape.Metadata.DisplayType.EncodeAlternateElement());
var encodedContentType = contentItem.ContentType.EncodeAlternateElement();
// Content__[ContentType] e.g. Content-BlogPost,
displaying.Shape.Metadata.Alternates.Add("Content__" + encodedContentType);
// Content__[Id] e.g. Content-42,
displaying.Shape.Metadata.Alternates.Add("Content__" + contentItem.Id);
// Content_[DisplayType]__[ContentType] e.g. Content-BlogPost.Summary
displaying.Shape.Metadata.Alternates.Add("Content_" + displaying.Shape.Metadata.DisplayType + "__" + encodedContentType);
// Content_[DisplayType]__[Id] e.g. Content-42.Summary
displaying.Shape.Metadata.Alternates.Add("Content_" + displaying.Shape.Metadata.DisplayType + "__" + contentItem.Id);
}
});
// This shapes provides a way to lazily load a content item render it in any display type.
builder.Describe("ContentItem")
.OnProcessing(async context =>
{
var content = context.Shape;
var handle = content.GetProperty<string>("Handle");
var displayType = content.GetProperty<string>("DisplayType");
var alternate = content.GetProperty<string>("Alternate");
if (string.IsNullOrEmpty(handle))
{
// This code is provided for backwards compatibility and can be removed in a future version.
handle = content.GetProperty<string>("Alias");
if (string.IsNullOrEmpty(handle))
{
return;
}
}
var contentManager = context.ServiceProvider.GetRequiredService<IContentManager>();
var handleManager = context.ServiceProvider.GetRequiredService<IContentHandleManager>();
var displayManager = context.ServiceProvider.GetRequiredService<IContentItemDisplayManager>();
var updateModelAccessor = context.ServiceProvider.GetRequiredService<IUpdateModelAccessor>();
var contentItemId = await handleManager.GetContentItemIdAsync(handle);
if (string.IsNullOrEmpty(contentItemId))
{
return;
}
var contentItem = await contentManager.GetAsync(contentItemId);
if (contentItem == null)
{
return;
}
content.Properties["ContentItem"] = contentItem;
var displayShape = await displayManager.BuildDisplayAsync(contentItem, updateModelAccessor.ModelUpdater, displayType);
if (!string.IsNullOrEmpty(alternate))
{
displayShape.Metadata.Alternates.Add(alternate);
}
await context.Shape.AddAsync(displayShape, string.Empty);
});
return ValueTask.CompletedTask;
}
}
}