Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assign menu children correctly from MenuPartDisplayDriver #15766

Merged
merged 2 commits into from
Apr 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)] != null)
{
newObj["MenuItemsListPart"] = new JsonObject { ["MenuItems"] = new JsonArray() };
newObj[nameof(MenuItemsListPart)] = 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