Skip to content

Commit

Permalink
Fixing new content item edition (#3337)
Browse files Browse the repository at this point in the history
Fixes #3333
  • Loading branch information
sebastienros authored Mar 15, 2019
1 parent 023b3a9 commit 0e6f948
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace OrchardCore.ContentManagement
/// </summary>
public class ContentElement : IContent
{
internal Dictionary<string, ContentElement> _elements = new Dictionary<string, ContentElement>();
private Dictionary<string, ContentElement> _elements;

protected ContentElement() : this(new JObject())
{
Expand All @@ -21,6 +21,9 @@ protected ContentElement(JObject data)
Data = data;
}

[JsonIgnore]
protected internal Dictionary<string, ContentElement> Elements => _elements = _elements ?? new Dictionary<string, ContentElement>();

[JsonIgnore]
public dynamic Content { get { return Data; } }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static bool Has<TElement>(this ContentElement contentElement) where TElem
/// <returns>The content element instance or <code>null</code> if it doesn't exist.</returns>
public static ContentElement Get(this ContentElement contentElement, Type contentElementType, string name)
{
if (contentElement._elements.TryGetValue(name, out var element))
if (contentElement.Elements.TryGetValue(name, out var element))
{
return element;
}
Expand All @@ -57,7 +57,7 @@ public static ContentElement Get(this ContentElement contentElement, Type conten
result.Data = elementData;
result.ContentItem = contentElement.ContentItem;

contentElement._elements[name] = result;
contentElement.Elements[name] = result;

return result;
}
Expand All @@ -77,7 +77,7 @@ public static ContentElement Get(this ContentElement contentElement, Type conten
var newElement = new TElement();
newElement.ContentItem = contentElement.ContentItem;
contentElement.Data[name] = newElement.Data;
contentElement._elements[name] = newElement;
contentElement.Elements[name] = newElement;
return newElement;
}

Expand All @@ -92,14 +92,13 @@ public static ContentElement Get(this ContentElement contentElement, Type conten
/// <returns>The current <see cref="ContentItem"/> instance.</returns>
public static ContentElement Weld(this ContentElement contentElement, string name, ContentElement element)
{
JToken result;
if (!contentElement.Data.TryGetValue(name, out result))
if (!contentElement.Data.ContainsKey(name))
{
element.Data = JObject.FromObject(element, ContentBuilderSettings.IgnoreDefaultValuesSerializer);
element.ContentItem = contentElement.ContentItem;

contentElement.Data[name] = element.Data;
contentElement._elements[name] = element;
contentElement.Elements[name] = element;
}

return contentElement;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -315,8 +315,6 @@ public async Task UnpublishAsync(ContentItem contentItem)

protected async Task<ContentItem> BuildNewVersionAsync(ContentItem existingContentItem)
{
var buildingContentItem = await NewAsync(existingContentItem.ContentType);

ContentItem latestVersion;

if (existingContentItem.Latest)
Expand All @@ -342,6 +340,11 @@ protected async Task<ContentItem> BuildNewVersionAsync(ContentItem existingConte
latestVersion.Latest = false;
}

// We are not invoking NewAsync as we are cloning an existing item
// This will also prevent the Elements (parts) from being allocated unnecessarily
var buildingContentItem = new ContentItem();

buildingContentItem.ContentType = existingContentItem.ContentType;
buildingContentItem.ContentItemId = existingContentItem.ContentItemId;
buildingContentItem.ContentItemVersionId = _idGenerator.GenerateUniqueId(existingContentItem);
buildingContentItem.DisplayText = existingContentItem.DisplayText;
Expand Down

0 comments on commit 0e6f948

Please sign in to comment.