Making it so when a content item is set as the home page it's only accessible as the home page and not at its path

- current implication for blogs is that if a blog is set as the home page its posts are then all rooted (e.g. if a blog at
  /blog is set as the home page then it's available at / and a post at, say, /my-post instead of /blog/my-post). There should
  maybe be a setting to alter the paths of the posts but (1) that's a pain at the moment, (2) hacking the URL by removing
  the post slug would result in a 404 instead of redirecting to /, the path of the blog - we don't handle redirects of that
  nature - and (3) the ability to root a blog in this manner *has* been requested by some.

--HG--
branch : dev
This commit is contained in:
Nathan Heskew
2010-12-01 00:51:42 -08:00
parent 2798850a38
commit 381f70d47c
14 changed files with 292 additions and 231 deletions

View File

@@ -58,14 +58,7 @@ namespace Orchard.Core.Routable.Controllers {
throw new ApplicationException(T("Ambiguous content").Text);
}
var item = hits.Single();
// primary action run for a home paged item shall not pass
if (!RouteData.DataTokens.ContainsKey("ParentActionViewContext")
&& item.Id == _routableHomePageProvider.GetHomePageId(_workContextAccessor.GetContext().CurrentSite.HomePage)) {
return HttpNotFound();
}
dynamic model = _contentManager.BuildDisplay(item);
dynamic model = _contentManager.BuildDisplay(hits.Single());
// Casting to avoid invalid (under medium trust) reflection over the protected View method and force a static invocation.
return View((object)model);
}

View File

@@ -16,6 +16,8 @@ namespace Orchard.Core.Routable.Handlers {
private readonly IOrchardServices _services;
private readonly IRoutablePathConstraint _routablePathConstraint;
private readonly IRoutableService _routableService;
private readonly IContentManager _contentManager;
private readonly IWorkContextAccessor _workContextAccessor;
private readonly IHomePageProvider _routableHomePageProvider;
public RoutePartHandler(
@@ -23,11 +25,14 @@ namespace Orchard.Core.Routable.Handlers {
IRepository<RoutePartRecord> repository,
IRoutablePathConstraint routablePathConstraint,
IRoutableService routableService,
IContentManager contentManager,
IWorkContextAccessor workContextAccessor,
IEnumerable<IHomePageProvider> homePageProviders) {
_services = services;
_routablePathConstraint = routablePathConstraint;
_routableService = routableService;
_contentManager = contentManager;
_workContextAccessor = workContextAccessor;
_routableHomePageProvider = homePageProviders.SingleOrDefault(p => p.GetProviderName() == RoutableHomePageProvider.Name);
T = NullLocalizer.Instance;
@@ -47,9 +52,26 @@ namespace Orchard.Core.Routable.Handlers {
OnPublished<RoutePart>((context, route) => {
FinalizePath(route, context, processSlug);
if (route.ContentItem.Id != 0 && route.PromoteToHomePage && _routableHomePageProvider != null) {
_services.WorkContext.CurrentSite.HomePage = _routableHomePageProvider.GetSettingValue(route.ContentItem.Id);
_routablePathConstraint.AddPath("");
if (route.Id != 0 && route.PromoteToHomePage && _routableHomePageProvider != null) {
var homePageSetting = _workContextAccessor.GetContext().CurrentSite.HomePage;
var currentHomePageId = !string.IsNullOrWhiteSpace(homePageSetting)
? _routableHomePageProvider.GetHomePageId(homePageSetting)
: 0;
if (currentHomePageId != route.Id) {
// reset the path on the current home page
var currentHomePage = _contentManager.Get(currentHomePageId);
if (currentHomePage != null)
FinalizePath(currentHomePage.As<RoutePart>(), context, processSlug);
// set the new home page
_services.WorkContext.CurrentSite.HomePage = _routableHomePageProvider.GetSettingValue(route.ContentItem.Id);
}
// readjust the constraints of the current current home page
_routablePathConstraint.RemovePath(route.Path);
route.Path = "";
_routableService.FixContainedPaths(route);
_routablePathConstraint.AddPath(route.Path);
}
});