From 28f0bd52edb35a77fe0241314e31124505d4c0b9 Mon Sep 17 00:00:00 2001 From: Benedek Farkas Date: Sun, 5 Oct 2025 16:26:36 +0200 Subject: [PATCH] #8830: Fixing that fields attached to a Menu Item are not updated (#8844) * Navigation: Fixing that saving a menu item should not force creating a draft version * Navigation: Adding support to the AdminController for the Delete button rendered by the Contents feature * Navigation: Fixing that fields attached to a Menu Item should also be updated when creating the item * MainMenuService: Fixing that unpublished menu items could not be deleted * Adding notification when deleting a menu items --- .../Navigation/Controllers/AdminController.cs | 42 +++++++++++++------ .../Navigation/Services/MainMenuService.cs | 12 +++--- 2 files changed, 36 insertions(+), 18 deletions(-) diff --git a/src/Orchard.Web/Core/Navigation/Controllers/AdminController.cs b/src/Orchard.Web/Core/Navigation/Controllers/AdminController.cs index 2337598da..184791a48 100644 --- a/src/Orchard.Web/Core/Navigation/Controllers/AdminController.cs +++ b/src/Orchard.Web/Core/Navigation/Controllers/AdminController.cs @@ -2,7 +2,6 @@ using System.Collections.Generic; using System.Linq; using System.Web.Mvc; -using System.Web.Routing; using Orchard.ContentManagement; using Orchard.ContentManagement.Aspects; using Orchard.ContentManagement.Handlers; @@ -14,6 +13,7 @@ using Orchard.Data; using Orchard.Exceptions; using Orchard.Localization; using Orchard.Logging; +using Orchard.Mvc; using Orchard.Mvc.Extensions; using Orchard.Mvc.Html; using Orchard.Security; @@ -131,11 +131,15 @@ namespace Orchard.Core.Navigation.Controllers { return RedirectToAction("Index", new { menuId }); } + [HttpPost, ActionName("Edit")] + [FormValueRequired("submit.Delete")] + public ActionResult EditDeletePOST(int id) => Delete(id); + [HttpPost] public ActionResult Delete(int id) { - MenuPart menuPart = _menuService.Get(id); int? menuId = null; + if (!_authorizer.Authorize( Permissions.ManageMenus, menuPart == null ? null : _menuService.GetMenu(menuPart.Menu.Id), @@ -152,7 +156,7 @@ namespace Orchard.Core.Navigation.Controllers { .ToList(); foreach (var menuItem in menuItems.Concat(new[] { menuPart })) { - // if the menu item is a concrete content item, don't delete it, just unreference the menu + // if the menu item is a concrete content item, don't delete it, just remove the menu reference if (!menuPart.ContentItem.TypeDefinition.Settings.ContainsKey("Stereotype") || menuPart.ContentItem.TypeDefinition.Settings["Stereotype"] != "MenuItem") { menuPart.Menu = null; @@ -162,6 +166,11 @@ namespace Orchard.Core.Navigation.Controllers { } } + _notifier.Information(T.Plural( + "The menu item '{1}' has been deleted.", + "The menu item '{1}' and its children have been deleted.", + menuItems.Count() + 1, + menuPart.MenuText)); } return RedirectToAction("Index", new { menuId }); @@ -172,7 +181,8 @@ namespace Orchard.Core.Navigation.Controllers { return new HttpUnauthorizedResult(); // create a new temporary menu item - var menuPart = _contentManager.New(id); + var contentItem = _contentManager.New(id); + var menuPart = contentItem.As(); if (menuPart == null) return HttpNotFound(); @@ -187,7 +197,7 @@ namespace Orchard.Core.Navigation.Controllers { // filter the content items for this specific menu menuPart.MenuPosition = Position.GetNext(_navigationManager.BuildMenu(menu)); menuPart.Menu = menu; - var model = _contentManager.BuildEditor(menuPart); + var model = _contentManager.BuildEditor(contentItem); return View(model); } @@ -206,23 +216,32 @@ namespace Orchard.Core.Navigation.Controllers { public ActionResult CreateMenuItemPost(string id, int menuId, string returnUrl) { if (!_authorizer.Authorize(Permissions.ManageMenus, _menuService.GetMenu(menuId), T("Couldn't manage the menu"))) return new HttpUnauthorizedResult(); - var menuPart = _contentManager.New(id); + + var contentItem = _contentManager.New(id); + var menuPart = contentItem.As(); + if (menuPart == null) return HttpNotFound(); + // load the menu var menu = _contentManager.Get(menuId); if (menu == null) return HttpNotFound(); + _contentManager.Create(contentItem); + menuPart.Menu = menu; - var model = _contentManager.UpdateEditor(menuPart, this); menuPart.MenuPosition = Position.GetNext(_navigationManager.BuildMenu(menu)); - _contentManager.Create(menuPart); + + var model = _contentManager.UpdateEditor(contentItem, this); + if (!ModelState.IsValid) { _transactionManager.Cancel(); return View(model); } - _notifier.Information(T("Your {0} has been added.", menuPart.TypeDefinition.DisplayName)); + + _notifier.Information(T("Your {0} has been added.", contentItem.TypeDefinition.DisplayName)); + return this.RedirectLocal(returnUrl, () => RedirectToAction("Index")); } @@ -308,7 +327,8 @@ namespace Orchard.Core.Navigation.Controllers { } private ActionResult EditPOST(int id, string returnUrl, Action conditionallyPublish) { - var menuPart = _contentManager.GetDraftRequired(id); + var contentItem = _contentManager.GetLatest(id); + var menuPart = contentItem.As(); if (menuPart == null) return HttpNotFound(); @@ -316,8 +336,6 @@ namespace Orchard.Core.Navigation.Controllers { if (!_authorizer.Authorize(Permissions.ManageMenus, menuPart.Menu, T("Couldn't manage the menu"))) return new HttpUnauthorizedResult(); - var contentItem = menuPart.ContentItem; - string previousRoute = null; if (contentItem.Has() && !string.IsNullOrWhiteSpace(returnUrl) diff --git a/src/Orchard.Web/Core/Navigation/Services/MainMenuService.cs b/src/Orchard.Web/Core/Navigation/Services/MainMenuService.cs index 682df695d..d314896a6 100644 --- a/src/Orchard.Web/Core/Navigation/Services/MainMenuService.cs +++ b/src/Orchard.Web/Core/Navigation/Services/MainMenuService.cs @@ -25,7 +25,7 @@ namespace Orchard.Core.Navigation.Services { } public IContent GetMenu(string menuName) { - if(string.IsNullOrWhiteSpace(menuName)) { + if (string.IsNullOrWhiteSpace(menuName)) { return null; } @@ -37,19 +37,19 @@ namespace Orchard.Core.Navigation.Services { } public IContent GetMenu(int menuId) { - return _contentManager.Get(menuId, VersionOptions.Published); + return _contentManager.Get(menuId, VersionOptions.Published); } public MenuPart Get(int menuPartId) { - return _contentManager.Get(menuPartId); + return _contentManager.Get(menuPartId, VersionOptions.Latest); } public IContent Create(string name) { - - if(string.IsNullOrWhiteSpace(name)) { + + if (string.IsNullOrWhiteSpace(name)) { throw new ArgumentNullException(name); } - + var menu = _contentManager.Create("Menu"); menu.As().Title = name;