From 1a9e5f4681ee746db7a6ede88e67e6b7955501ca Mon Sep 17 00:00:00 2001 From: Sipke Schoorstra Date: Fri, 21 Nov 2014 23:57:26 -0800 Subject: [PATCH] Adding support for fetching of element descriptors from the blue print element harvester. The problem before this change was that the cache manager would cache an empty list of elements when trying to locate a blueprint element without specifying a content item. The VaryByParam property allows additional control over the cache key. --- .../Orchard.Layouts/Framework/Display/ElementDisplay.cs | 2 +- .../Orchard.Layouts/Providers/BlueprintElementHarvester.cs | 4 ++-- .../Orchard.Layouts/Services/DescribeElementsContext.cs | 4 +++- .../Modules/Orchard.Layouts/Services/ElementManager.cs | 4 ++-- 4 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/Orchard.Web/Modules/Orchard.Layouts/Framework/Display/ElementDisplay.cs b/src/Orchard.Web/Modules/Orchard.Layouts/Framework/Display/ElementDisplay.cs index d1e1c5328..2f465d793 100644 --- a/src/Orchard.Web/Modules/Orchard.Layouts/Framework/Display/ElementDisplay.cs +++ b/src/Orchard.Web/Modules/Orchard.Layouts/Framework/Display/ElementDisplay.cs @@ -93,7 +93,7 @@ namespace Orchard.Layouts.Framework.Display { var dictionary = new Dictionary { {"Element", element}, {"Elements", children}, - {"ContentItem", content.ContentItem} + {"ContentItem", content != null ? content.ContentItem : default(ContentItem)} }; if (elementState != null) { diff --git a/src/Orchard.Web/Modules/Orchard.Layouts/Providers/BlueprintElementHarvester.cs b/src/Orchard.Web/Modules/Orchard.Layouts/Providers/BlueprintElementHarvester.cs index 3f93b2f99..8e83df94d 100644 --- a/src/Orchard.Web/Modules/Orchard.Layouts/Providers/BlueprintElementHarvester.cs +++ b/src/Orchard.Web/Modules/Orchard.Layouts/Providers/BlueprintElementHarvester.cs @@ -28,8 +28,8 @@ namespace Orchard.Layouts.Providers { var blueprints = _elementBlueprintService.Value.GetBlueprints().ToArray(); var query = - from blueprint in blueprints - let describeContext = DescribeElementsContext.Empty + from blueprint in blueprints + let describeContext = new DescribeElementsContext { Content = context.Content, CacheVaryParam = "Blueprints"} let baseElementDescriptor = _elementManager.Value.GetElementDescriptorByTypeName(describeContext, blueprint.BaseElementTypeName) let baseElement = _elementManager.Value.ActivateElement(baseElementDescriptor) select new ElementDescriptor( diff --git a/src/Orchard.Web/Modules/Orchard.Layouts/Services/DescribeElementsContext.cs b/src/Orchard.Web/Modules/Orchard.Layouts/Services/DescribeElementsContext.cs index 544a71f15..3a7f18f7a 100644 --- a/src/Orchard.Web/Modules/Orchard.Layouts/Services/DescribeElementsContext.cs +++ b/src/Orchard.Web/Modules/Orchard.Layouts/Services/DescribeElementsContext.cs @@ -3,6 +3,8 @@ using Orchard.ContentManagement; namespace Orchard.Layouts.Services { public class DescribeElementsContext { public IContent Content { get; set; } - public static DescribeElementsContext Empty = new DescribeElementsContext(); + public string CacheVaryParam { get; set; } + + public static readonly DescribeElementsContext Empty = new DescribeElementsContext(); } } \ No newline at end of file diff --git a/src/Orchard.Web/Modules/Orchard.Layouts/Services/ElementManager.cs b/src/Orchard.Web/Modules/Orchard.Layouts/Services/ElementManager.cs index e7971508f..0621e48c5 100644 --- a/src/Orchard.Web/Modules/Orchard.Layouts/Services/ElementManager.cs +++ b/src/Orchard.Web/Modules/Orchard.Layouts/Services/ElementManager.cs @@ -37,7 +37,7 @@ namespace Orchard.Layouts.Services { public IEnumerable DescribeElements(DescribeElementsContext context) { var contentType = context.Content != null ? context.Content.ContentItem.ContentType : default(string); - var cacheKey = String.Format("LayoutElementTypes-{0}", contentType ?? "AnyType"); + var cacheKey = String.Format("LayoutElementTypes-{0}-{1}", contentType ?? "AnyType", context.CacheVaryParam); return _cacheManager.Get(cacheKey, acquireContext => { var harvesterContext = new HarvestElementsContext { Content = context.Content @@ -55,7 +55,7 @@ namespace Orchard.Layouts.Services { public IEnumerable GetCategories(DescribeElementsContext context) { var contentType = context.Content != null ? context.Content.ContentItem.ContentType : default(string); - return _cacheManager.Get(String.Format("ElementCategories-{0}", contentType ?? "AnyType"), acquireContext => { + return _cacheManager.Get(String.Format("ElementCategories-{0}-{1}", contentType ?? "AnyType", context.CacheVaryParam), acquireContext => { var elements = DescribeElements(context); var categoryDictionary = GetCategories(); var categoryDescriptorDictionary = new Dictionary();