Skip to content

Commit

Permalink
Assign menu children correctly from MenuPartDisplayDriver
Browse files Browse the repository at this point in the history
Fix #15727
  • Loading branch information
MikeAlhayek committed Apr 15, 2024
1 parent 03c6cba commit 5eb563e
Showing 1 changed file with 39 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,32 @@ namespace OrchardCore.Menu.Drivers
public class MenuPartDisplayDriver : ContentPartDisplayDriver<MenuPart>
{
private readonly IContentDefinitionManager _contentDefinitionManager;
protected readonly IHtmlLocalizer H;
private readonly INotifier _notifier;
private readonly ILogger _logger;

protected readonly IHtmlLocalizer H;

public MenuPartDisplayDriver(
IContentDefinitionManager contentDefinitionManager,
IHtmlLocalizer<MenuPartDisplayDriver> htmlLocalizer,
INotifier notifier,
ILogger<MenuPartDisplayDriver> logger
ILogger<MenuPartDisplayDriver> logger,
IHtmlLocalizer<MenuPartDisplayDriver> htmlLocalizer
)
{
_contentDefinitionManager = contentDefinitionManager;
H = htmlLocalizer;
_notifier = notifier;
_logger = logger;
H = htmlLocalizer;
}

public override IDisplayResult Edit(MenuPart part)
{
return Initialize<MenuPartEditViewModel>("MenuPart_Edit", async model =>
{
var menuItemContentTypes = (await _contentDefinitionManager.ListTypeDefinitionsAsync()).Where(t => t.StereotypeEquals("MenuItem"));
var menuItemContentTypes = (await _contentDefinitionManager.ListTypeDefinitionsAsync())
.Where(t => t.StereotypeEquals("MenuItem"))
.ToArray();
var notify = false;
foreach (var menuItem in part.ContentItem.As<MenuItemsListPart>().MenuItems)
Expand All @@ -68,20 +72,27 @@ public override async Task<IDisplayResult> UpdateAsync(MenuPart part, IUpdateMod
{
var model = new MenuPartEditViewModel();

if (await updater.TryUpdateModelAsync(model, Prefix, t => t.Hierarchy) && !string.IsNullOrWhiteSpace(model.Hierarchy))
if (await updater.TryUpdateModelAsync(model, Prefix, t => t.Hierarchy) &&
!string.IsNullOrWhiteSpace(model.Hierarchy))
{
var originalMenuItems = part.ContentItem.As<MenuItemsListPart>();

var newHierarchy = JArray.Parse(model.Hierarchy);

var menuItems = new JsonArray();

foreach (var item in newHierarchy)
var originalMenuItems = part.ContentItem.As<MenuItemsListPart>();

if (originalMenuItems is not null)
{
menuItems.Add(ProcessItem(originalMenuItems, item as JsonObject));
var newHierarchy = JArray.Parse(model.Hierarchy);

foreach (var item in newHierarchy)
{
menuItems.Add(ProcessItem(originalMenuItems, item as JsonObject));
}
}

part.ContentItem.Content["MenuItemsListPart"] = new JsonObject { ["MenuItems"] = menuItems };
part.ContentItem.Content[nameof(MenuItemsListPart)] = new JsonObject
{
[nameof(MenuItemsListPart.MenuItems)] = menuItems,
};
}

return Edit(part);
Expand All @@ -101,27 +112,38 @@ private static JsonObject GetMenuItemAt(MenuItemsListPart menuItems, int[] index
}

var newObj = JObject.FromObject(menuItem, JOptions.Default);
if (newObj["MenuItemsListPart"] != null)

if (newObj[nameof(MenuItemsListPart.MenuItems)] != null)
{
newObj["MenuItemsListPart"] = new JsonObject { ["MenuItems"] = new JsonArray() };
newObj[nameof(MenuItemsListPart.MenuItems)] = new JsonObject
{
[nameof(MenuItemsListPart.MenuItems)] = new JsonArray()
};
}

return newObj;
}

private static JsonObject ProcessItem(MenuItemsListPart originalItems, JsonObject item)
{
var contentItem = GetMenuItemAt(originalItems, item["index"].ToString().Split('-').Select(x => Convert.ToInt32(x)).ToArray());
var indexes = item["index"]?.ToString().Split('-').Select(x => Convert.ToInt32(x)).ToArray() ?? [];

var contentItem = GetMenuItemAt(originalItems, indexes);

var children = item["children"] as JsonArray;

if (children is not null)
{
var menuItems = new JsonArray();

for (var i = 0; i < children.Count; i++)
{
menuItems.Add(ProcessItem(originalItems, children[i] as JsonObject));
contentItem["MenuItemsListPart"] = new JsonObject { ["MenuItems"] = menuItems };
}

contentItem[nameof(MenuItemsListPart)] = new JsonObject
{
[nameof(MenuItemsListPart.MenuItems)] = menuItems,
};
}

Expand Down

0 comments on commit 5eb563e

Please sign in to comment.