From ad34be7c174422a82012675fefda54807a667632 Mon Sep 17 00:00:00 2001 From: Giuliano Dessimone Date: Wed, 11 Oct 2023 15:48:53 +0200 Subject: [PATCH] Exclude ContentWidgets layer by default in order to increase performance opening the widget admin page This layer can still be displayed if explicitly chosen --- .../Controllers/AdminController.cs | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/Orchard.Web/Modules/Orchard.Widgets/Controllers/AdminController.cs b/src/Orchard.Web/Modules/Orchard.Widgets/Controllers/AdminController.cs index 3d7f6f89c47..5af6f120790 100644 --- a/src/Orchard.Web/Modules/Orchard.Widgets/Controllers/AdminController.cs +++ b/src/Orchard.Web/Modules/Orchard.Widgets/Controllers/AdminController.cs @@ -63,6 +63,8 @@ public ActionResult Index(int? layerId, string culture) { IEnumerable layers = _widgetsService.GetLayers().OrderBy(x => x.Name).ToList(); + + if (!layers.Any()) { Services.Notifier.Error(T("There are no widget layers defined. A layer will need to be added in order to add widgets to any part of the site.")); return RedirectToAction("AddLayer"); @@ -84,14 +86,27 @@ public ActionResult Index(int? layerId, string culture) { string zonePreviewImagePath = string.Format("{0}/{1}/ThemeZonePreview.png", currentTheme.Location, currentTheme.Id); string zonePreviewImage = _virtualPathProvider.FileExists(zonePreviewImagePath) ? zonePreviewImagePath : null; - var widgets = _widgetsService.GetWidgets(); + //Only when "ContentWidgets" layer is explicitly chosen the widgets are listed, not by default (for performance reasons) + IEnumerable widgets; + var IdLayerContentWidget = layers.Where(x => x.Name.ToLowerInvariant() == "contentwidgets").Select(x => x.Id).FirstOrDefault(); + if (IdLayerContentWidget > 0) { + //Ok the layer exists + var IdsLayers = layers.Where(x => x.Name.ToLowerInvariant() != "contentwidgets").Select(x => x.Id).ToList().ToArray(); + if (layerId != null && (int)layerId == IdLayerContentWidget) { + //If ContentWidgets is chosen explicitly then IdsLayers is not filtered, note that the editors should be advised that this can take lot of time + IdsLayers = layers.Select(x => x.Id).ToList().ToArray(); + } + widgets = _widgetsService.GetWidgets(IdsLayers); + } + else { + widgets = _widgetsService.GetWidgets(); + } if (!String.IsNullOrWhiteSpace(culture)) { widgets = widgets.Where(x => { if (x.Has()) { return String.Equals(x.As().Culture, culture, StringComparison.InvariantCultureIgnoreCase); } - return false; }).ToList(); }