mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-10-15 11:44:58 +08:00
Filtering menu items based on current culture
--HG-- branch : 1.x
This commit is contained in:
@@ -8,5 +8,5 @@ e7fc05ff6137ed5459d198b2bea9a5804818b0bd src/Orchard.Web/Modules/Orchard.Forms
|
||||
419399ef2e37122a000e6cc8674148d3183d7032 src/Orchard.Web/Modules/Orchard.TaskLease
|
||||
aca1f5aeb5dd426bc80c6142afc7f2717fc6d5a1 src/Orchard.Web/Modules/Orchard.Tokens
|
||||
4ed51e0e76c2aacc2de90ce9984fd00cfdfae2ce src/orchard.web/Modules/Orchard.Alias
|
||||
d3a8dac3aca8deb5fd45e8864a4273a88a8741ee src/orchard.web/Modules/Orchard.Projections
|
||||
8812c9cb3495450ce4a3f9c9a3e594c06e18fe4a src/orchard.web/Modules/Orchard.Projections
|
||||
5a8c67141b56f29d56d2315904dee15223419dc4 src/orchard.web/modules/Orchard.Fields
|
||||
|
@@ -3,6 +3,7 @@ using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using Orchard.ContentManagement;
|
||||
using Orchard.ContentManagement.Aspects;
|
||||
using Orchard.ContentManagement.Drivers;
|
||||
using Orchard.ContentManagement.Handlers;
|
||||
using Orchard.Core.Navigation.Models;
|
||||
@@ -48,9 +49,25 @@ namespace Orchard.Core.Navigation.Drivers {
|
||||
}
|
||||
|
||||
var menuName = menu.As<TitlePart>().Title.HtmlClassify();
|
||||
var currentCulture = _workContextAccessor.GetContext().CurrentCulture;
|
||||
|
||||
IEnumerable<MenuItem> menuItems = _navigationManager.BuildMenu(menu);
|
||||
|
||||
var localized = new List<MenuItem>();
|
||||
foreach(var menuItem in menuItems) {
|
||||
// if there is no associated content, it as culture neutral
|
||||
if(menuItem.Content == null) {
|
||||
localized.Add(menuItem);
|
||||
}
|
||||
|
||||
// if the menu item is culture neutral or of the current culture
|
||||
if (String.IsNullOrEmpty(menuItem.Culture) || String.Equals(menuItem.Culture, currentCulture, StringComparison.OrdinalIgnoreCase)) {
|
||||
localized.Add(menuItem);
|
||||
}
|
||||
}
|
||||
|
||||
menuItems = localized;
|
||||
|
||||
var routeData = _workContextAccessor.GetContext().HttpContext.Request.RequestContext.RouteData;
|
||||
|
||||
var selectedPath = NavigationHelper.SetSelectedPath(menuItems, routeData);
|
||||
|
@@ -7,12 +7,23 @@ using Orchard.ContentManagement.Handlers;
|
||||
namespace Orchard.Core.Navigation.Handlers {
|
||||
[UsedImplicitly]
|
||||
public class ContentMenuItemPartHandler : ContentHandler {
|
||||
private readonly IContentManager _contentManager;
|
||||
|
||||
public ContentMenuItemPartHandler(IContentManager contentManager, IRepository<ContentMenuItemPartRecord> repository) {
|
||||
_contentManager = contentManager;
|
||||
Filters.Add(new ActivatingFilter<ContentMenuItemPart>("ContentMenuItem"));
|
||||
Filters.Add(StorageFilter.For(repository));
|
||||
|
||||
OnLoading<ContentMenuItemPart>((context, part) => part._content.Loader(p => contentManager.Get(part.Record.ContentMenuItemRecord.Id)));
|
||||
}
|
||||
|
||||
protected override void GetItemMetadata(GetContentItemMetadataContext context) {
|
||||
var contentMenuItemPart = context.ContentItem.As<ContentMenuItemPart>();
|
||||
|
||||
// the display route for the menu item is the one for the referenced content item
|
||||
if(contentMenuItemPart != null) {
|
||||
context.Metadata.DisplayRouteValues = _contentManager.GetItemMetadata(contentMenuItemPart.Content).DisplayRouteValues;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using JetBrains.Annotations;
|
||||
using Orchard.ContentManagement;
|
||||
using Orchard.ContentManagement.Handlers;
|
||||
using Orchard.Core.Navigation.Models;
|
||||
using Orchard.Data;
|
||||
@@ -14,5 +15,13 @@ namespace Orchard.Core.Navigation.Handlers {
|
||||
x.MenuText = String.Empty;
|
||||
});
|
||||
}
|
||||
|
||||
protected override void GetItemMetadata(GetContentItemMetadataContext context) {
|
||||
var part = context.ContentItem.As<MenuPart>();
|
||||
|
||||
if (part != null) {
|
||||
context.Metadata.DisplayText = part.MenuText;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,7 +1,5 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Web;
|
||||
using Orchard.ContentManagement;
|
||||
|
||||
namespace Orchard.Core.Navigation.Services {
|
||||
|
@@ -1,5 +1,6 @@
|
||||
using System.Web;
|
||||
using Orchard.ContentManagement;
|
||||
using Orchard.ContentManagement.Aspects;
|
||||
using Orchard.Core.Navigation.Models;
|
||||
using Orchard.Localization;
|
||||
using Orchard.UI.Navigation;
|
||||
@@ -23,10 +24,17 @@ namespace Orchard.Core.Navigation.Services {
|
||||
if (menuPart != null) {
|
||||
var part = menuPart;
|
||||
|
||||
// fetch the culture of the menu item, if any
|
||||
string culture = null;
|
||||
var localized = part.As<ILocalizableAspect>();
|
||||
if(localized != null) {
|
||||
culture = localized.Culture;
|
||||
}
|
||||
|
||||
if (part.Is<MenuItemPart>())
|
||||
builder.Add(new LocalizedString(HttpUtility.HtmlEncode(part.MenuText)), part.MenuPosition, item => item.Url(part.As<MenuItemPart>().Url).Content(part));
|
||||
builder.Add(new LocalizedString(HttpUtility.HtmlEncode(part.MenuText)), part.MenuPosition, item => item.Url(part.As<MenuItemPart>().Url).Content(part).Culture(culture));
|
||||
else
|
||||
builder.Add(new LocalizedString(HttpUtility.HtmlEncode(part.MenuText)), part.MenuPosition, item => item.Action(_contentManager.GetItemMetadata(part.ContentItem).DisplayRouteValues).Content(part));
|
||||
builder.Add(new LocalizedString(HttpUtility.HtmlEncode(part.MenuText)), part.MenuPosition, item => item.Action(_contentManager.GetItemMetadata(part.ContentItem).DisplayRouteValues).Content(part).Culture(culture));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -4,5 +4,5 @@
|
||||
<fieldset>
|
||||
<label for="@Html.FieldIdFor(m => m.Url)">@T("Url")</label>
|
||||
@Html.TextBoxFor(m => m.Url, new { @class = "large text" })
|
||||
<span class="hint">@T("A valid url, i.e. http://orchardproject.net, /content/file.pdf, ...")</span>
|
||||
<span class="hint">@T("A valid url, i.e. ~/my-page, http://orchardproject.net, /content/file.pdf, ...")</span>
|
||||
</fieldset>
|
||||
|
@@ -1,2 +1,8 @@
|
||||
@using Orchard.ContentManagement
|
||||
<a href="@Url.ItemDisplayUrl((ContentItem)Model.Content.ContentItem.ContentMenuItemPart.Content)">@Model.Text</a>
|
||||
@{
|
||||
ContentItem contentItem = Model.Content.ContentItem.ContentMenuItemPart.Content;
|
||||
}
|
||||
|
||||
@if (contentItem != null) {
|
||||
<a href="@Model.Href">@Model.Text</a>
|
||||
}
|
@@ -1,6 +1,9 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Orchard.UI.Navigation {
|
||||
/// <summary>
|
||||
/// Provides a way to alter the main navigation, for instance by dynamically injecting new items
|
||||
/// </summary>
|
||||
public interface INavigationFilter : IDependency {
|
||||
IEnumerable<MenuItem> Filter(IEnumerable<MenuItem> menuItems);
|
||||
}
|
||||
|
@@ -21,6 +21,7 @@ namespace Orchard.UI.Navigation {
|
||||
public string Position { get; set; }
|
||||
public bool LinkToFirstChild { get; set; }
|
||||
public bool LocalNav { get; set; }
|
||||
public string Culture { get; set; }
|
||||
public bool Selected { get; set; }
|
||||
public RouteValueDictionary RouteValues { get; set; }
|
||||
public IEnumerable<MenuItem> Items { get; set; }
|
||||
|
@@ -33,6 +33,11 @@ namespace Orchard.UI.Navigation {
|
||||
return this;
|
||||
}
|
||||
|
||||
public NavigationItemBuilder Culture(string culture) {
|
||||
_item.Culture = culture;
|
||||
return this;
|
||||
}
|
||||
|
||||
public NavigationItemBuilder IdHint(string idHint) {
|
||||
_item.IdHint = idHint;
|
||||
return this;
|
||||
|
@@ -103,6 +103,7 @@ namespace Orchard.UI.Navigation {
|
||||
Position = item.Position,
|
||||
RouteValues = item.RouteValues,
|
||||
LocalNav = item.LocalNav,
|
||||
Culture = item.Culture,
|
||||
Text = item.Text,
|
||||
IdHint = item.IdHint,
|
||||
Classes = item.Classes,
|
||||
@@ -222,22 +223,25 @@ namespace Orchard.UI.Navigation {
|
||||
}
|
||||
|
||||
static MenuItem Join(IEnumerable<MenuItem> items) {
|
||||
if (items.Count() < 2)
|
||||
return items.Single();
|
||||
var list = items.ToArray();
|
||||
|
||||
if (list.Count() < 2)
|
||||
return list.Single();
|
||||
|
||||
var joined = new MenuItem {
|
||||
Text = items.First().Text,
|
||||
IdHint = items.Select(x => x.IdHint).FirstOrDefault(x => !string.IsNullOrWhiteSpace(x)),
|
||||
Classes = items.Select(x => x.Classes).FirstOrDefault(x => x != null && x.Count > 0),
|
||||
Url = items.Select(x => x.Url).FirstOrDefault(x => !string.IsNullOrWhiteSpace(x)),
|
||||
Href = items.Select(x => x.Href).FirstOrDefault(x => !string.IsNullOrWhiteSpace(x)),
|
||||
LinkToFirstChild = items.First().LinkToFirstChild,
|
||||
RouteValues = items.Select(x => x.RouteValues).FirstOrDefault(x => x != null),
|
||||
LocalNav = items.Any(x => x.LocalNav),
|
||||
Items = Merge(items.Select(x => x.Items)).ToArray(),
|
||||
Position = SelectBestPositionValue(items.Select(x => x.Position)),
|
||||
Permissions = items.SelectMany(x => x.Permissions).Distinct(),
|
||||
Content = items.First().Content
|
||||
Text = list.First().Text,
|
||||
IdHint = list.Select(x => x.IdHint).FirstOrDefault(x => !string.IsNullOrWhiteSpace(x)),
|
||||
Classes = list.Select(x => x.Classes).FirstOrDefault(x => x != null && x.Count > 0),
|
||||
Url = list.Select(x => x.Url).FirstOrDefault(x => !string.IsNullOrWhiteSpace(x)),
|
||||
Href = list.Select(x => x.Href).FirstOrDefault(x => !string.IsNullOrWhiteSpace(x)),
|
||||
LinkToFirstChild = list.First().LinkToFirstChild,
|
||||
RouteValues = list.Select(x => x.RouteValues).FirstOrDefault(x => x != null),
|
||||
LocalNav = list.Any(x => x.LocalNav),
|
||||
Culture = list.First().Culture,
|
||||
Items = Merge(list.Select(x => x.Items)).ToArray(),
|
||||
Position = SelectBestPositionValue(list.Select(x => x.Position)),
|
||||
Permissions = list.SelectMany(x => x.Permissions).Distinct(),
|
||||
Content = list.First().Content
|
||||
};
|
||||
|
||||
return joined;
|
||||
|
Reference in New Issue
Block a user