-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
Copy pathContentPartFieldDefinitionExtensions.cs
43 lines (39 loc) · 1.51 KB
/
ContentPartFieldDefinitionExtensions.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
using System.Collections.Generic;
using System.Text.Json.Nodes;
namespace OrchardCore.ContentManagement.Metadata.Models;
public static class ContentPartFieldDefinitionExtensions
{
/// <summary>
/// Returns the value of the defined content field from the <paramref name="contentItem"/>.
/// </summary>
public static TField GetContentField<TField>(
this ContentPartFieldDefinition fieldDefinition,
ContentItem contentItem)
where TField : ContentField
{
if (((JsonObject)contentItem.Content)[fieldDefinition.ContentTypePartDefinition.Name] is not JsonObject jPart ||
jPart[fieldDefinition.Name] is not JsonObject jField)
{
return null;
}
return jField.ToObject<TField>();
}
/// <summary>
/// Returns each field from <paramref name="fieldDefinitions"/> that exists in <paramref name="contentItem"/> in a
/// tuple along with its <see cref="ContentPartFieldDefinition"/>.
/// </summary>
public static IEnumerable<(ContentPartFieldDefinition Definition, TField Field)> GetContentFields<TField>(
this IEnumerable<ContentPartFieldDefinition> fieldDefinitions,
ContentItem contentItem)
where TField : ContentField
{
foreach (var fieldDefinition in fieldDefinitions)
{
var field = fieldDefinition.GetContentField<TField>(contentItem);
if (field is not null)
{
yield return (fieldDefinition, field);
}
}
}
}