From da9091716f72e22e238f4ebb96eb1ae87b937f25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Piquot?= Date: Sun, 16 Dec 2018 19:09:45 +0100 Subject: [PATCH] Settings[nameof(ContentTypeSettings.Stereotype)] can return a null value. So calling Settings[nameof(ContentTypeSettings.Stereotype)].ToString() can throw a null reference exception. --- .../ContentPartDisplayDriverTPart.cs | 102 +++++++----------- 1 file changed, 37 insertions(+), 65 deletions(-) diff --git a/src/OrchardCore/OrchardCore.ContentManagement.Display/ContentDisplay/ContentPartDisplayDriverTPart.cs b/src/OrchardCore/OrchardCore.ContentManagement.Display/ContentDisplay/ContentPartDisplayDriverTPart.cs index 4a1816d9ba8..6c4469d6528 100644 --- a/src/OrchardCore/OrchardCore.ContentManagement.Display/ContentDisplay/ContentPartDisplayDriverTPart.cs +++ b/src/OrchardCore/OrchardCore.ContentManagement.Display/ContentDisplay/ContentPartDisplayDriverTPart.cs @@ -1,5 +1,6 @@ using System; using System.Threading.Tasks; + using OrchardCore.ContentManagement.Display.Models; using OrchardCore.ContentManagement.Metadata.Models; using OrchardCore.ContentManagement.Metadata.Settings; @@ -18,6 +19,18 @@ namespace OrchardCore.ContentManagement.Display.ContentDisplay { private ContentTypePartDefinition _typePartDefinition; + public virtual IDisplayResult Display(TPart part, BuildPartDisplayContext context) => Display(part); + + public virtual IDisplayResult Display(TPart part) => null; + + public virtual Task DisplayAsync(TPart part, BuildPartDisplayContext context) => Task.FromResult(Display(part, context)); + + public virtual IDisplayResult Edit(TPart part, BuildPartEditorContext context) => Edit(part); + + public virtual IDisplayResult Edit(TPart part) => null; + + public virtual Task EditAsync(TPart part, BuildPartEditorContext context) => Task.FromResult(Edit(part, context)); + public override ShapeResult Factory(string shapeType, Func> shapeBuilder, Func initializeAsync) { // e.g., HtmlBodyPart.Summary, HtmlBodyPart-BlogPost, BagPart-LandingPage-Services @@ -29,18 +42,25 @@ public override ShapeResult Factory(string shapeType, Func UpdateAsync(TPart part, IUpdateModel updater, UpdatePartEditorContext context) => UpdateAsync(part, context); + + public virtual Task UpdateAsync(TPart part, BuildPartEditorContext context) => UpdateAsync(part, context.Updater); + + public virtual Task UpdateAsync(TPart part, IUpdateModel updater) => Task.FromResult(null); + async Task IContentPartDisplayDriver.BuildDisplayAsync(ContentPart contentPart, ContentTypePartDefinition typePartDefinition, BuildDisplayContext context) { var part = contentPart as TPart; @@ -198,51 +224,6 @@ async Task IContentPartDisplayDriver.UpdateEditorAsync(ContentPa return result; } - public virtual Task DisplayAsync(TPart part, BuildPartDisplayContext context) - { - return Task.FromResult(Display(part, context)); - } - - public virtual IDisplayResult Display(TPart part, BuildPartDisplayContext context) - { - return Display(part); - } - - public virtual IDisplayResult Display(TPart part) - { - return null; - } - - public virtual Task EditAsync(TPart part, BuildPartEditorContext context) - { - return Task.FromResult(Edit(part, context)); - } - - public virtual IDisplayResult Edit(TPart part, BuildPartEditorContext context) - { - return Edit(part); - } - - public virtual IDisplayResult Edit(TPart part) - { - return null; - } - - public virtual Task UpdateAsync(TPart part, IUpdateModel updater, UpdatePartEditorContext context) - { - return UpdateAsync(part, context); - } - - public virtual Task UpdateAsync(TPart part, BuildPartEditorContext context) - { - return UpdateAsync(part, context.Updater); - } - - public virtual Task UpdateAsync(TPart part, IUpdateModel updater) - { - return Task.FromResult(null); - } - protected string GetEditorShapeType(string shapeType, ContentTypePartDefinition typePartDefinition) { var editor = typePartDefinition.Editor(); @@ -251,20 +232,11 @@ protected string GetEditorShapeType(string shapeType, ContentTypePartDefinition : shapeType; } - protected string GetEditorShapeType(string shapeType, BuildPartEditorContext context) - { - return GetEditorShapeType(shapeType, context.TypePartDefinition); - } + protected string GetEditorShapeType(string shapeType, BuildPartEditorContext context) => GetEditorShapeType(shapeType, context.TypePartDefinition); - protected string GetEditorShapeType(ContentTypePartDefinition typePartDefinition) - { - return GetEditorShapeType(typeof(TPart).Name + "_Edit", typePartDefinition); - } + protected string GetEditorShapeType(ContentTypePartDefinition typePartDefinition) => GetEditorShapeType(typeof(TPart).Name + "_Edit", typePartDefinition); - protected string GetEditorShapeType(BuildPartEditorContext context) - { - return GetEditorShapeType(context.TypePartDefinition); - } + protected string GetEditorShapeType(BuildPartEditorContext context) => GetEditorShapeType(context.TypePartDefinition); private void BuildPrefix(ContentTypePartDefinition typePartDefinition, string htmlFieldPrefix) { @@ -276,4 +248,4 @@ private void BuildPrefix(ContentTypePartDefinition typePartDefinition, string ht } } } -} +} \ No newline at end of file