-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Unpublishing and editing content item with an uninitialized ContentPi…
…ckerField breaks (Lombiq Technologies: OCORE-92) (#11528)
- Loading branch information
1 parent
f409fd5
commit 778b1ea
Showing
12 changed files
with
201 additions
and
382 deletions.
There are no files selected for viewing
43 changes: 43 additions & 0 deletions
43
...Core.Modules/OrchardCore.ContentFields/Extensions/ContentPartFieldDefinitionExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
using System.Collections.Generic; | ||
using Newtonsoft.Json.Linq; | ||
|
||
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 (((JObject)contentItem.Content)[fieldDefinition.PartDefinition.Name] is not JObject jPart || | ||
jPart[fieldDefinition.Name] is not JObject 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); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.