Skip to content

Commit

Permalink
Unpublishing and editing content item with an uninitialized ContentPi…
Browse files Browse the repository at this point in the history
…ckerField breaks (Lombiq Technologies: OCORE-92) (#11528)
  • Loading branch information
sarahelsaig authored Apr 28, 2022
1 parent f409fd5 commit 778b1ea
Show file tree
Hide file tree
Showing 12 changed files with 201 additions and 382 deletions.
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);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
using System.Collections.Generic;
using System.Linq;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json.Linq;
using OrchardCore.ContentFields.Fields;
using OrchardCore.ContentManagement;
using OrchardCore.ContentManagement.Metadata;
using OrchardCore.ContentManagement.Metadata.Models;
using YesSql.Indexes;

namespace OrchardCore.ContentFields.Indexing.SQL
Expand All @@ -18,7 +18,7 @@ public class BooleanFieldIndex : ContentFieldIndex
public class BooleanFieldIndexProvider : ContentFieldIndexProvider
{
private readonly IServiceProvider _serviceProvider;
private readonly HashSet<string> _ignoredTypes = new HashSet<string>();
private readonly HashSet<string> _ignoredTypes = new();
private IContentDefinitionManager _contentDefinitionManager;

public BooleanFieldIndexProvider(IServiceProvider serviceProvider)
Expand Down Expand Up @@ -67,40 +67,20 @@ public override void Describe(DescribeContext<ContentItem> context)
return null;
}
var results = new List<BooleanFieldIndex>();
foreach (var fieldDefinition in fieldDefinitions)
{
var jPart = (JObject)contentItem.Content[fieldDefinition.PartDefinition.Name];
if (jPart == null)
{
continue;
}
var jField = (JObject)jPart[fieldDefinition.Name];
if (jField == null)
{
continue;
}
var field = jField.ToObject<BooleanField>();
results.Add(new BooleanFieldIndex
{
Latest = contentItem.Latest,
Published = contentItem.Published,
ContentItemId = contentItem.ContentItemId,
ContentItemVersionId = contentItem.ContentItemVersionId,
ContentType = contentItem.ContentType,
ContentPart = fieldDefinition.PartDefinition.Name,
ContentField = fieldDefinition.Name,
Boolean = field.Value
});
}
return results;
return fieldDefinitions
.GetContentFields<BooleanField>(contentItem)
.Select(pair =>
new BooleanFieldIndex
{
Latest = contentItem.Latest,
Published = contentItem.Published,
ContentItemId = contentItem.ContentItemId,
ContentItemVersionId = contentItem.ContentItemVersionId,
ContentType = contentItem.ContentType,
ContentPart = pair.Definition.PartDefinition.Name,
ContentField = pair.Definition.Name,
Boolean = pair.Field.Value,
});
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
using System.Collections.Generic;
using System.Linq;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json.Linq;
using OrchardCore.ContentFields.Fields;
using OrchardCore.ContentManagement;
using OrchardCore.ContentManagement.Metadata;
using OrchardCore.ContentManagement.Metadata.Models;
using YesSql.Indexes;

namespace OrchardCore.ContentFields.Indexing.SQL
Expand All @@ -18,7 +18,7 @@ public class ContentPickerFieldIndex : ContentFieldIndex
public class ContentPickerFieldIndexProvider : ContentFieldIndexProvider
{
private readonly IServiceProvider _serviceProvider;
private readonly HashSet<string> _ignoredTypes = new HashSet<string>();
private readonly HashSet<string> _ignoredTypes = new();
private IContentDefinitionManager _contentDefinitionManager;

public ContentPickerFieldIndexProvider(IServiceProvider serviceProvider)
Expand Down Expand Up @@ -67,44 +67,23 @@ public override void Describe(DescribeContext<ContentItem> context)
return null;
}
var results = new List<ContentPickerFieldIndex>();
// Get all field values
foreach (var fieldDefinition in fieldDefinitions)
{
var jPart = (JObject)contentItem.Content[fieldDefinition.PartDefinition.Name];
if (jPart == null)
{
continue;
}
var jField = (JObject)jPart[fieldDefinition.Name];
if (jField == null)
{
continue;
}
var field = jField.ToObject<ContentPickerField>();
foreach (var contentItemId in field.ContentItemIds)
{
results.Add(new ContentPickerFieldIndex
return fieldDefinitions
.GetContentFields<ContentPickerField>(contentItem)
.SelectMany(pair =>
pair.Field.ContentItemIds.Select(id => (pair.Definition, ContentItemId: id)))
.Select(pair =>
new ContentPickerFieldIndex
{
Latest = contentItem.Latest,
Published = contentItem.Published,
ContentItemId = contentItem.ContentItemId,
ContentItemVersionId = contentItem.ContentItemVersionId,
ContentType = contentItem.ContentType,
ContentPart = fieldDefinition.PartDefinition.Name,
ContentField = fieldDefinition.Name,
SelectedContentItemId = contentItemId
ContentPart = pair.Definition.PartDefinition.Name,
ContentField = pair.Definition.Name,
SelectedContentItemId = pair.ContentItemId,
});
}
}
return results;
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
using System.Collections.Generic;
using System.Linq;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json.Linq;
using OrchardCore.ContentFields.Fields;
using OrchardCore.ContentManagement;
using OrchardCore.ContentManagement.Metadata;
using OrchardCore.ContentManagement.Metadata.Models;
using YesSql.Indexes;

namespace OrchardCore.ContentFields.Indexing.SQL
Expand All @@ -18,7 +18,7 @@ public class DateFieldIndex : ContentFieldIndex
public class DateFieldIndexProvider : ContentFieldIndexProvider
{
private readonly IServiceProvider _serviceProvider;
private readonly HashSet<string> _ignoredTypes = new HashSet<string>();
private readonly HashSet<string> _ignoredTypes = new();
private IContentDefinitionManager _contentDefinitionManager;

public DateFieldIndexProvider(IServiceProvider serviceProvider)
Expand Down Expand Up @@ -67,40 +67,19 @@ public override void Describe(DescribeContext<ContentItem> context)
return null;
}
var results = new List<DateFieldIndex>();
foreach (var fieldDefinition in fieldDefinitions)
{
var jPart = (JObject)contentItem.Content[fieldDefinition.PartDefinition.Name];
if (jPart == null)
{
continue;
}
var jField = (JObject)jPart[fieldDefinition.Name];
if (jField == null)
{
continue;
}
var field = jField.ToObject<DateField>();
results.Add(new DateFieldIndex
return fieldDefinitions
.GetContentFields<DateField>(contentItem)
.Select(pair => new DateFieldIndex
{
Latest = contentItem.Latest,
Published = contentItem.Published,
ContentItemId = contentItem.ContentItemId,
ContentItemVersionId = contentItem.ContentItemVersionId,
ContentType = contentItem.ContentType,
ContentPart = fieldDefinition.PartDefinition.Name,
ContentField = fieldDefinition.Name,
Date = field.Value
ContentPart = pair.Definition.PartDefinition.Name,
ContentField = pair.Definition.Name,
Date = pair.Field.Value,
});
}
return results;
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
using System.Collections.Generic;
using System.Linq;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json.Linq;
using OrchardCore.ContentFields.Fields;
using OrchardCore.ContentManagement;
using OrchardCore.ContentManagement.Metadata;
using OrchardCore.ContentManagement.Metadata.Models;
using YesSql.Indexes;

namespace OrchardCore.ContentFields.Indexing.SQL
Expand All @@ -18,7 +18,7 @@ public class DateTimeFieldIndex : ContentFieldIndex
public class DateTimeFieldIndexProvider : ContentFieldIndexProvider
{
private readonly IServiceProvider _serviceProvider;
private readonly HashSet<string> _ignoredTypes = new HashSet<string>();
private readonly HashSet<string> _ignoredTypes = new();
private IContentDefinitionManager _contentDefinitionManager;

public DateTimeFieldIndexProvider(IServiceProvider serviceProvider)
Expand Down Expand Up @@ -67,40 +67,20 @@ public override void Describe(DescribeContext<ContentItem> context)
return null;
}
var results = new List<DateTimeFieldIndex>();
foreach (var fieldDefinition in fieldDefinitions)
{
var jPart = (JObject)contentItem.Content[fieldDefinition.PartDefinition.Name];
if (jPart == null)
{
continue;
}
var jField = (JObject)jPart[fieldDefinition.Name];
if (jField == null)
{
continue;
}
var field = jField.ToObject<DateTimeField>();
results.Add(new DateTimeFieldIndex
{
Latest = contentItem.Latest,
Published = contentItem.Published,
ContentItemId = contentItem.ContentItemId,
ContentItemVersionId = contentItem.ContentItemVersionId,
ContentType = contentItem.ContentType,
ContentPart = fieldDefinition.PartDefinition.Name,
ContentField = fieldDefinition.Name,
DateTime = field.Value
});
}
return results;
return fieldDefinitions
.GetContentFields<DateTimeField>(contentItem)
.Select(pair =>
new DateTimeFieldIndex
{
Latest = contentItem.Latest,
Published = contentItem.Published,
ContentItemId = contentItem.ContentItemId,
ContentItemVersionId = contentItem.ContentItemVersionId,
ContentType = contentItem.ContentType,
ContentPart = pair.Definition.PartDefinition.Name,
ContentField = pair.Definition.Name,
DateTime = pair.Field.Value,
});
});
}
}
Expand Down
Loading

0 comments on commit 778b1ea

Please sign in to comment.