From 3ad8e8c6bdcbff170625e6a49a0f62df3c3e98b7 Mon Sep 17 00:00:00 2001 From: Sebastien Ros Date: Fri, 10 Jan 2014 12:40:59 -0800 Subject: [PATCH] Implementing DisableThemePart Also implementing a new Match predicate for placement files, i.e. ContentPart="" which will apply the placement rules if the currently displayed content type contains a specific part. --- .../Services/PlacementService.cs | 1 + .../Drivers/DisableThemePartDriver.cs | 23 +++++++++++++++++++ .../Modules/Orchard.Themes/Migrations.cs | 13 ++++++++++- .../Orchard.Themes/Models/DisableThemePart.cs | 10 ++++++++ .../Orchard.Themes/Orchard.Themes.csproj | 9 ++++++++ .../Modules/Orchard.Themes/Placement.info | 9 ++++++++ .../DefaultContentDisplay.cs | 1 + .../Drivers/ContentPartDriver.cs | 3 ++- .../Descriptors/ShapeAlterationBuilder.cs | 2 ++ .../ShapePlacementParsingStrategy.cs | 4 ++++ src/Orchard/Themes/ThemeFilter.cs | 6 ++++- 11 files changed, 78 insertions(+), 3 deletions(-) create mode 100644 src/Orchard.Web/Modules/Orchard.Themes/Drivers/DisableThemePartDriver.cs create mode 100644 src/Orchard.Web/Modules/Orchard.Themes/Models/DisableThemePart.cs create mode 100644 src/Orchard.Web/Modules/Orchard.Themes/Placement.info diff --git a/src/Orchard.Web/Modules/Orchard.ContentTypes/Services/PlacementService.cs b/src/Orchard.Web/Modules/Orchard.ContentTypes/Services/PlacementService.cs index ddfaa1c31..f3d47e915 100644 --- a/src/Orchard.Web/Modules/Orchard.ContentTypes/Services/PlacementService.cs +++ b/src/Orchard.Web/Modules/Orchard.ContentTypes/Services/PlacementService.cs @@ -230,6 +230,7 @@ namespace Orchard.ContentTypes.Services { ShapeDescriptor descriptor; if (shapeTable.Descriptors.TryGetValue(partShapeType, out descriptor)) { var placementContext = new ShapePlacementContext { + Content = context.ContentItem, ContentType = context.ContentItem.ContentType, Stereotype = stereotype, DisplayType = displayType, diff --git a/src/Orchard.Web/Modules/Orchard.Themes/Drivers/DisableThemePartDriver.cs b/src/Orchard.Web/Modules/Orchard.Themes/Drivers/DisableThemePartDriver.cs new file mode 100644 index 000000000..ec9330e9f --- /dev/null +++ b/src/Orchard.Web/Modules/Orchard.Themes/Drivers/DisableThemePartDriver.cs @@ -0,0 +1,23 @@ +using System.Web; +using JetBrains.Annotations; +using Orchard.ContentManagement.Drivers; +using Orchard.Themes.Models; + +namespace Orchard.Themes.Drivers { + [UsedImplicitly] + public class DisableThemePartDriver : ContentPartDriver { + private readonly HttpContextBase _httpContext; + + public DisableThemePartDriver(HttpContextBase httpContext) { + _httpContext = httpContext; + } + + protected override DriverResult Display(DisableThemePart part, string displayType, dynamic shapeHelper) { + return ContentShape("Parts_DisableTheme", () => { + ThemeFilter.Disable(_httpContext.Request.RequestContext); + return null; + }); + } + } + +} \ No newline at end of file diff --git a/src/Orchard.Web/Modules/Orchard.Themes/Migrations.cs b/src/Orchard.Web/Modules/Orchard.Themes/Migrations.cs index 2039116a7..3c82c4118 100644 --- a/src/Orchard.Web/Modules/Orchard.Themes/Migrations.cs +++ b/src/Orchard.Web/Modules/Orchard.Themes/Migrations.cs @@ -1,4 +1,6 @@ -using Orchard.Data.Migration; +using Orchard.ContentManagement.MetaData; +using Orchard.Core.Contents.Extensions; +using Orchard.Data.Migration; namespace Orchard.Themes { public class ThemesDataMigration : DataMigrationImpl { @@ -6,5 +8,14 @@ namespace Orchard.Themes { public int Create() { return 1; } + + public int UpdateFrom1() { + + ContentDefinitionManager.AlterPartDefinition("DisableThemePart", builder => builder + .Attachable() + .WithDescription("When attached to a content type, disables the theme when a content item of this type is displayed.")); + + return 2; + } } } \ No newline at end of file diff --git a/src/Orchard.Web/Modules/Orchard.Themes/Models/DisableThemePart.cs b/src/Orchard.Web/Modules/Orchard.Themes/Models/DisableThemePart.cs new file mode 100644 index 000000000..2d95cae8e --- /dev/null +++ b/src/Orchard.Web/Modules/Orchard.Themes/Models/DisableThemePart.cs @@ -0,0 +1,10 @@ +using Orchard.ContentManagement; + +namespace Orchard.Themes.Models { + /// + /// When attached to a Content Type and rendered + /// it will prevent the theme from being applied + /// + public class DisableThemePart : ContentPart { + } +} \ No newline at end of file diff --git a/src/Orchard.Web/Modules/Orchard.Themes/Orchard.Themes.csproj b/src/Orchard.Web/Modules/Orchard.Themes/Orchard.Themes.csproj index 997d6fa83..275f12e21 100644 --- a/src/Orchard.Web/Modules/Orchard.Themes/Orchard.Themes.csproj +++ b/src/Orchard.Web/Modules/Orchard.Themes/Orchard.Themes.csproj @@ -65,9 +65,11 @@ + + @@ -103,6 +105,10 @@ {2D1D92BB-4555-4CBE-8D0E-63563D6CE4C6} Orchard.Framework + + {9916839c-39fc-4ceb-a5af-89ca7e87119f} + Orchard.Core + @@ -123,6 +129,9 @@ + + + 10.0 $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) diff --git a/src/Orchard.Web/Modules/Orchard.Themes/Placement.info b/src/Orchard.Web/Modules/Orchard.Themes/Placement.info new file mode 100644 index 000000000..9249e0873 --- /dev/null +++ b/src/Orchard.Web/Modules/Orchard.Themes/Placement.info @@ -0,0 +1,9 @@ + + + + + + + + + diff --git a/src/Orchard/ContentManagement/DefaultContentDisplay.cs b/src/Orchard/ContentManagement/DefaultContentDisplay.cs index 125d022fe..71d100e54 100644 --- a/src/Orchard/ContentManagement/DefaultContentDisplay.cs +++ b/src/Orchard/ContentManagement/DefaultContentDisplay.cs @@ -128,6 +128,7 @@ namespace Orchard.ContentManagement { ShapeDescriptor descriptor; if (shapeTable.Descriptors.TryGetValue(partShapeType, out descriptor)) { var placementContext = new ShapePlacementContext { + Content = context.ContentItem, ContentType = context.ContentItem.ContentType, Stereotype = stereotype, DisplayType = displayType, diff --git a/src/Orchard/ContentManagement/Drivers/ContentPartDriver.cs b/src/Orchard/ContentManagement/Drivers/ContentPartDriver.cs index 686942da4..ea714bc52 100644 --- a/src/Orchard/ContentManagement/Drivers/ContentPartDriver.cs +++ b/src/Orchard/ContentManagement/Drivers/ContentPartDriver.cs @@ -63,7 +63,8 @@ namespace Orchard.ContentManagement.Drivers { ShapeDescriptor descriptor; if(context.ShapeTable.Descriptors.TryGetValue(editor.GetShapeType(), out descriptor)) { var placementContext = new ShapePlacementContext { - ContentType = part.ContentItem.ContentType, + Content = part.ContentItem, + ContentType = part.ContentItem.ContentType, Differentiator = editor.GetDifferentiator(), DisplayType = null, Path = String.Empty diff --git a/src/Orchard/DisplayManagement/Descriptors/ShapeAlterationBuilder.cs b/src/Orchard/DisplayManagement/Descriptors/ShapeAlterationBuilder.cs index 0a714c60c..4a68ae52a 100644 --- a/src/Orchard/DisplayManagement/Descriptors/ShapeAlterationBuilder.cs +++ b/src/Orchard/DisplayManagement/Descriptors/ShapeAlterationBuilder.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; using System.Web; +using Orchard.ContentManagement; using Orchard.DisplayManagement.Implementation; using Orchard.Environment.Extensions.Models; @@ -110,6 +111,7 @@ namespace Orchard.DisplayManagement.Descriptors { } public class ShapePlacementContext { + public IContent Content { get; set; } public string ContentType { get; set; } public string Stereotype { get; set; } public string DisplayType { get; set; } diff --git a/src/Orchard/DisplayManagement/Descriptors/ShapePlacementStrategy/ShapePlacementParsingStrategy.cs b/src/Orchard/DisplayManagement/Descriptors/ShapePlacementStrategy/ShapePlacementParsingStrategy.cs index 80d24734b..2beb156d3 100644 --- a/src/Orchard/DisplayManagement/Descriptors/ShapePlacementStrategy/ShapePlacementParsingStrategy.cs +++ b/src/Orchard/DisplayManagement/Descriptors/ShapePlacementStrategy/ShapePlacementParsingStrategy.cs @@ -120,6 +120,10 @@ namespace Orchard.DisplayManagement.Descriptors.ShapePlacementStrategy { public static Func BuildPredicate(Func predicate, KeyValuePair term) { var expression = term.Value; switch (term.Key) { + case "ContentPart": + return ctx => ctx.Content != null + && ctx.Content.ContentItem.Parts.Any(part => part.PartDefinition.Name == expression) + && predicate(ctx); case "ContentType": if (expression.EndsWith("*")) { var prefix = expression.Substring(0, expression.Length - 1); diff --git a/src/Orchard/Themes/ThemeFilter.cs b/src/Orchard/Themes/ThemeFilter.cs index 2acf6df15..ba11c882e 100644 --- a/src/Orchard/Themes/ThemeFilter.cs +++ b/src/Orchard/Themes/ThemeFilter.cs @@ -32,7 +32,11 @@ namespace Orchard.Themes { public static void Apply(RequestContext context) { // the value isn't important - context.HttpContext.Items[typeof (ThemeFilter)] = null; + context.HttpContext.Items[typeof(ThemeFilter)] = null; + } + + public static void Disable(RequestContext context) { + context.HttpContext.Items.Remove(typeof(ThemeFilter)); } public static bool IsApplied(RequestContext context) {