-
Notifications
You must be signed in to change notification settings - Fork 2.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unpublishing and editing content item with an uninitialized ContentPickerField breaks (Lombiq Technologies: OCORE-92) #11528
Merged
Merged
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f705711
Prevent problems when the ContentPickerField is null.
sarahelsaig 076f341
Add extension to DRY field index providers.
sarahelsaig b3e9b65
DRY field index providers using new extension method.
sarahelsaig ab6132c
Refactor GetContentField to not use object pattern matching.
sarahelsaig 9ec0d2a
Use "is not null".
sarahelsaig 1845039
Cast contentItem.Content to JObject.
sarahelsaig d343e9f
Code styling.
sarahelsaig File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
...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,40 @@ | ||
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 (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); | ||
Skrypt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
} |
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just some thought here. Could we do the same but without using Newtonsoft.Json and replace it with System.Text.Json while we are at it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
ContentItem.Content
points toOrchardCore.ContentManagement.ContentElement.Data
which is a NewtonsoftJObject
. This class doesn't do any serializing, just access this existing property so trying to use System.Text.Json here would require conversion. Better table it untilContentElement
itself is ported to STJ, I think.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, forget it for now then.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will Linq be concern here for allocation and performance?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We pass on the enumerables without using an intermediary list like before, so that should improve allocation. I'm not sure about the performance, but we don't do anything complex so it should be fine. Also these providers are only called when you update a content item's field; that's relatively rare so you shouldn't be concerned.