From c0e46f2930e498c3666f309413e71f6888fdd2f6 Mon Sep 17 00:00:00 2001 From: Sipke Schoorstra Date: Sun, 22 Feb 2015 10:28:18 +0100 Subject: [PATCH] Handling optional Content context of content part element rendering. Content is optional context, so if it's null, we can't render the part element. This typically only happens when the layout editor is used outside the context of a content item and still renders the various content part elements as part of the toolbox. --- .../Orchard.Layouts/Drivers/ContentPartElementDriver.cs | 6 ++++++ .../Views/Elements/ContentPart.Design.cshtml | 4 +++- .../Orchard.Layouts/Views/Elements/ContentPart.cshtml | 8 +++++--- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/Orchard.Web/Modules/Orchard.Layouts/Drivers/ContentPartElementDriver.cs b/src/Orchard.Web/Modules/Orchard.Layouts/Drivers/ContentPartElementDriver.cs index 3c117bda1..dd8c5c6db 100644 --- a/src/Orchard.Web/Modules/Orchard.Layouts/Drivers/ContentPartElementDriver.cs +++ b/src/Orchard.Web/Modules/Orchard.Layouts/Drivers/ContentPartElementDriver.cs @@ -20,6 +20,12 @@ namespace Orchard.Layouts.Drivers { } protected override void OnDisplaying(ContentPart element, ElementDisplayContext context) { + // Content is optional context, so if it's null, we can't render the part element. + // This typically only happens when the layout editor is used outside the context of + // a content item and still renders the various content part elements as part of the toolbox. + if (context.Content == null) + return; + var contentItem = context.Content.ContentItem; var contentPartName = (string)element.Descriptor.StateBag["ElementTypeName"]; var contentPart = contentItem.Parts.FirstOrDefault(x => x.PartDefinition.Name == contentPartName); diff --git a/src/Orchard.Web/Modules/Orchard.Layouts/Views/Elements/ContentPart.Design.cshtml b/src/Orchard.Web/Modules/Orchard.Layouts/Views/Elements/ContentPart.Design.cshtml index b77a02d06..4aa9a867a 100644 --- a/src/Orchard.Web/Modules/Orchard.Layouts/Views/Elements/ContentPart.Design.cshtml +++ b/src/Orchard.Web/Modules/Orchard.Layouts/Views/Elements/ContentPart.Design.cshtml @@ -1 +1,3 @@ -@Display(Model.Content) \ No newline at end of file +@if (Model.Content != null) { + @Display(Model.Content) +} \ No newline at end of file diff --git a/src/Orchard.Web/Modules/Orchard.Layouts/Views/Elements/ContentPart.cshtml b/src/Orchard.Web/Modules/Orchard.Layouts/Views/Elements/ContentPart.cshtml index a2e8a5985..ae463b484 100644 --- a/src/Orchard.Web/Modules/Orchard.Layouts/Views/Elements/ContentPart.cshtml +++ b/src/Orchard.Web/Modules/Orchard.Layouts/Views/Elements/ContentPart.cshtml @@ -3,6 +3,8 @@ @{ var tagBuilder = TagBuilderExtensions.AddCommonElementAttributes(new OrchardTagBuilder("div"), Model); } -@tagBuilder.StartElement -@Display(Model.Content) -@tagBuilder.EndElement \ No newline at end of file +@if (Model.Content != null) { + @tagBuilder.StartElement + @Display(Model.Content) + @tagBuilder.EndElement +} \ No newline at end of file