From 2873eb89913e8ab4ed6814c28303879489a00f27 Mon Sep 17 00:00:00 2001 From: Sipke Schoorstra Date: Thu, 2 Jul 2015 13:37:20 +0300 Subject: [PATCH] #5337: Fixed homepage becoming lost after publishing layout template being used by the homepage. Work Item: 5337 --- .../Handlers/LayoutPartHandler.cs | 32 +++++++++++++++++-- .../Orchard.Layouts/Orchard.Layouts.csproj | 8 +++++ 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/src/Orchard.Web/Modules/Orchard.Layouts/Handlers/LayoutPartHandler.cs b/src/Orchard.Web/Modules/Orchard.Layouts/Handlers/LayoutPartHandler.cs index 04648fb23..78f9a595b 100644 --- a/src/Orchard.Web/Modules/Orchard.Layouts/Handlers/LayoutPartHandler.cs +++ b/src/Orchard.Web/Modules/Orchard.Layouts/Handlers/LayoutPartHandler.cs @@ -1,9 +1,13 @@ -using Orchard.ContentManagement; +using System; +using Orchard.Alias; +using Orchard.Autoroute.Models; +using Orchard.ContentManagement; using Orchard.ContentManagement.Handlers; using Orchard.Data; using Orchard.DisplayManagement; using Orchard.Layouts.Models; using Orchard.Layouts.Services; +using Orchard.Utility.Extensions; namespace Orchard.Layouts.Handlers { public class LayoutPartHandler : ContentHandler { @@ -13,6 +17,7 @@ namespace Orchard.Layouts.Handlers { private readonly IShapeDisplay _shapeDisplay; private readonly ILayoutSerializer _serializer; private readonly IStaticHttpContextScopeFactory _staticHttpContextScopeFactory; + private readonly IAliasService _aliasService; public LayoutPartHandler( IRepository repository, @@ -21,7 +26,8 @@ namespace Orchard.Layouts.Handlers { IContentPartDisplay contentPartDisplay, IShapeDisplay shapeDisplay, ILayoutSerializer serializer, - IStaticHttpContextScopeFactory staticHttpContextScopeFactory) { + IStaticHttpContextScopeFactory staticHttpContextScopeFactory, + IAliasService aliasService) { _layoutManager = layoutManager; _contentManager = contentManager; @@ -29,6 +35,7 @@ namespace Orchard.Layouts.Handlers { _shapeDisplay = shapeDisplay; _serializer = serializer; _staticHttpContextScopeFactory = staticHttpContextScopeFactory; + _aliasService = aliasService; Filters.Add(StorageFilter.For(repository)); OnPublished(UpdateTemplateClients); @@ -77,6 +84,12 @@ namespace Orchard.Layouts.Handlers { draft.LayoutData = _serializer.Serialize(updatedLayout); if (isPublished) { + // If the content being published is currently the homepage, we need to change the DisplayAlias from "" to "/" + // so that the autoroute part handler will not regenerate the alias and causes the homepage to become "lost". + if (IsHomePage(layout)) { + PromoteToHomePage(draft); + } + // We don't have to recurse here, since invoking Publish on a Layout will cause this handler to execute again. _contentManager.Publish(draft.ContentItem); } @@ -85,5 +98,20 @@ namespace Orchard.Layouts.Handlers { } } } + + private bool IsHomePage(IContent content) { + var homepage = _aliasService.Get(String.Empty); + var displayRouteValues = _contentManager.GetItemMetadata(content).DisplayRouteValues; + return homepage.Match(displayRouteValues); + } + + private void PromoteToHomePage(IContent content) { + var autoroutePart = content.As(); + + if (autoroutePart == null) + return; + + autoroutePart.DisplayAlias = "/"; + } } } \ No newline at end of file diff --git a/src/Orchard.Web/Modules/Orchard.Layouts/Orchard.Layouts.csproj b/src/Orchard.Web/Modules/Orchard.Layouts/Orchard.Layouts.csproj index c89069046..df50eea91 100644 --- a/src/Orchard.Web/Modules/Orchard.Layouts/Orchard.Layouts.csproj +++ b/src/Orchard.Web/Modules/Orchard.Layouts/Orchard.Layouts.csproj @@ -287,6 +287,14 @@ {3158c928-888c-4a84-8bc1-4a8257489538} Markdown + + {475b6c45-b27c-438b-8966-908b9d6d1077} + Orchard.Alias + + + {66fccd76-2761-47e3-8d11-b45d0001ddaa} + Orchard.Autoroute + {642a49d7-8752-4177-80d6-bfbbcfad3de0} Orchard.Forms