Layout elements cloning (#8538)

* Created a new element when session widget has a different container than the widget in the import context (to ensure actual cloning of the elements and not just adding a reference to the old element from the original container).

* Some comment has been rewritten for clarity
This commit is contained in:
Andrea Piovanelli
2022-03-18 12:27:11 +01:00
committed by GitHub
parent ab7c0f242e
commit 863d4a92a6

View File

@@ -13,6 +13,7 @@ using Orchard.Layouts.Framework.Drivers;
using Orchard.Layouts.Framework.Elements;
using Orchard.Layouts.Framework.Harvesters;
using Orchard.Layouts.Helpers;
using Orchard.Layouts.Models;
using Orchard.Mvc.Html;
using Orchard.Security;
using Orchard.Widgets.Layouts.Elements;
@@ -54,7 +55,7 @@ namespace Orchard.Widgets.Layouts.Providers {
}
private void LayoutSaving(ElementSavingContext context) {
// I need to save the widget element container.
// First, widget element container has to be stored.
var element = (Widget)context.Element;
if (element == null) {
return;
@@ -185,6 +186,18 @@ namespace Orchard.Widgets.Layouts.Providers {
return;
var widget = context.Session.GetItemFromSession(widgetIdentity);
// A new widget needs to be created and saved.
// This is to avoid the fact the very same element ending up in multiple layouts, causing issues when e.g. deleting a LayoutWidget of a cloned ContentItem (which would delete the elements of multiple layouts).
// The new widget is needed only when the container of the original element is different from the container of the cloned element, to ensure doing it when cloning elements and avoid doing the same when importing content.
var cp = widget.As<ICommonPart>();
if (cp != null) {
var lp = cp.Container.As<LayoutPart>();
if (lp != null && lp.Id != context.Layout.Id) {
widget = _contentManager.Value.Clone(widget);
}
}
var element = (Widget)context.Element;
element.WidgetId = widget != null ? widget.Id : default(int?);