Skip to content

Commit

Permalink
Maintain widget content item ids across flowpart and widget list part…
Browse files Browse the repository at this point in the history
… when updating (#5989)
  • Loading branch information
deanmarcussen authored May 5, 2020
1 parent 60a99d9 commit 124814a
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,19 +63,26 @@ public override async Task<IDisplayResult> UpdateAsync(FlowPart part, UpdatePart

await context.Updater.TryUpdateModelAsync(model, Prefix);

part.Widgets.Clear();
var contentItems = new List<ContentItem>();

for (var i = 0; i < model.Prefixes.Length; i++)
{
var contentItem = await _contentManager.NewAsync(model.ContentTypes[i]);
var existing = part.Widgets.FirstOrDefault(x => String.Equals(x.ContentItemId, model.Prefixes[i], StringComparison.OrdinalIgnoreCase));
if (existing != null)
{
contentItem.ContentItemId = model.Prefixes[i];
}

contentItem.Weld(new FlowMetadata());

var widgetModel = await contentItemDisplayManager.UpdateEditorAsync(contentItem, context.Updater, context.IsNew, htmlFieldPrefix: model.Prefixes[i]);

part.Widgets.Add(contentItem);
contentItems.Add(contentItem);
}

part.Widgets = contentItems;

return Edit(part, context);
}

Expand Down
4 changes: 2 additions & 2 deletions src/OrchardCore.Modules/OrchardCore.Flows/Models/FlowPart.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Collections.Generic;
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using OrchardCore.ContentManagement;

Expand All @@ -7,6 +7,6 @@ namespace OrchardCore.Flows.Models
public class FlowPart : ContentPart
{
[BindNever]
public List<ContentItem> Widgets { get; } = new List<ContentItem>();
public List<ContentItem> Widgets { get; set; } = new List<ContentItem>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
CanDelete: true,
//Input hidden
//Prefixes
PrefixValue: widget.ContentItemId,
HtmlFieldPrefix: htmlFieldPrefix,
PrefixesId: Html.IdFor(x => x.Prefixes),
PrefixesName: Html.NameFor(x => x.Prefixes),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public override async Task<IDisplayResult> UpdateAsync(WidgetsListPart part, Upd

await context.Updater.TryUpdateModelAsync(model, Prefix);

part.Widgets.Clear();
var zonedContentItems = new Dictionary<string, List<ContentItem>>();

// Remove any content or the zones would be merged and not be cleared
part.Content.Widgets.RemoveAll();
Expand All @@ -106,19 +106,29 @@ public override async Task<IDisplayResult> UpdateAsync(WidgetsListPart part, Upd
var prefix = model.Prefixes[i];

var contentItem = await _contentManager.NewAsync(contentType);
if (part.Widgets.ContainsKey(zone))
{
var existing = part.Widgets[zone].FirstOrDefault(x => String.Equals(x.ContentItemId, model.Prefixes[i], StringComparison.OrdinalIgnoreCase));
if (existing != null)
{
contentItem.ContentItemId = model.Prefixes[i];
}
}

contentItem.Weld(new WidgetMetadata());

var widgetModel = await contentItemDisplayManager.UpdateEditorAsync(contentItem, context.Updater, context.IsNew, htmlFieldPrefix: prefix);

if (!part.Widgets.ContainsKey(zone))
if (!zonedContentItems.ContainsKey(zone))
{
part.Widgets.Add(zone, new List<ContentItem>());
zonedContentItems.Add(zone, new List<ContentItem>());
}

part.Widgets[zone].Add(contentItem);
zonedContentItems[zone].Add(contentItem);
}

part.Widgets = zonedContentItems;

return Edit(part, context);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using System.Collections.Generic;
using System.Collections.Generic;
using OrchardCore.ContentManagement;

namespace OrchardCore.Widgets.Models
{
// A content item with this part can have widget instances.
public class WidgetsListPart : ContentPart
{
public Dictionary<string, List<ContentItem>> Widgets { get; } = new Dictionary<string, List<ContentItem>>();
public Dictionary<string, List<ContentItem>> Widgets { get; set; } = new Dictionary<string, List<ContentItem>>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
CanDelete: true,
//Input hidden
//Prefixes
PrefixValue: widget.ContentItemId,
HtmlFieldPrefix: htmlFieldPrefix,
PrefixesId: Html.IdFor(x => x.Prefixes),
PrefixesName: Html.NameFor(x => x.Prefixes),
Expand Down

0 comments on commit 124814a

Please sign in to comment.