mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-10-15 19:54:57 +08:00
Adding Site tokens and custom fields
--HG-- branch : 1.x
This commit is contained in:
@@ -8,5 +8,5 @@ e7fc05ff6137ed5459d198b2bea9a5804818b0bd src/Orchard.Web/Modules/Orchard.Forms
|
||||
aca1f5aeb5dd426bc80c6142afc7f2717fc6d5a1 src/Orchard.Web/Modules/Orchard.Tokens
|
||||
f6fecd1702066225a84a482ac029e3e6daff38f3 src/Orchard.Web/Modules/Orchard.ViewPermissions
|
||||
4ed51e0e76c2aacc2de90ce9984fd00cfdfae2ce src/orchard.web/Modules/Orchard.Alias
|
||||
cca0d94ab4000687eab9ed2bf1fb90f673fb36fb src/orchard.web/Modules/Orchard.Projections
|
||||
109844c63717ca7f881fb126a845184cca8de6f6 src/orchard.web/Modules/Orchard.Projections
|
||||
0826b86677cd77cfb05342b3ba570f522dc3e786 src/orchard.web/modules/Orchard.Fields
|
||||
|
@@ -1,4 +1,5 @@
|
||||
using Orchard.Data.Migration;
|
||||
using Orchard.ContentManagement.MetaData;
|
||||
using Orchard.Data.Migration;
|
||||
|
||||
namespace Orchard.Core.Settings {
|
||||
public class Migrations : DataMigrationImpl {
|
||||
@@ -121,5 +122,11 @@ namespace Orchard.Core.Settings {
|
||||
|
||||
return 3;
|
||||
}
|
||||
|
||||
public int UpdateFrom3() {
|
||||
ContentDefinitionManager.AlterTypeDefinition("Site", cfg => { });
|
||||
|
||||
return 4;
|
||||
}
|
||||
}
|
||||
}
|
89
src/Orchard.Web/Core/Settings/Tokens/SettingsTokens.cs
Normal file
89
src/Orchard.Web/Core/Settings/Tokens/SettingsTokens.cs
Normal file
@@ -0,0 +1,89 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using Orchard.ContentManagement;
|
||||
using Orchard.ContentManagement.MetaData;
|
||||
using Orchard.Events;
|
||||
using Orchard.Localization;
|
||||
using Orchard.Settings;
|
||||
using Orchard.ContentManagement.FieldStorage;
|
||||
|
||||
namespace Orchard.Core.Settings.Tokens {
|
||||
public interface ITokenProvider : IEventHandler {
|
||||
void Describe(dynamic context);
|
||||
void Evaluate(dynamic context);
|
||||
}
|
||||
|
||||
public class SettingsTokens : ITokenProvider {
|
||||
private readonly IOrchardServices _orchardServices;
|
||||
private readonly IContentDefinitionManager _contentDefinitionManager;
|
||||
|
||||
public SettingsTokens(IOrchardServices orchardServices, IContentDefinitionManager contentDefinitionManager) {
|
||||
_orchardServices = orchardServices;
|
||||
_contentDefinitionManager = contentDefinitionManager;
|
||||
}
|
||||
|
||||
public Localizer T { get; set; }
|
||||
|
||||
public void Describe(dynamic context) {
|
||||
|
||||
context.For("Site", T("Site Settings"), T("Tokens for Site Settings"))
|
||||
.Token("SiteName", T("Site Name"), T("The name of the site."))
|
||||
.Token("SuperUser", T("Super User"), T("The name of the super user."))
|
||||
.Token("Culture", T("Site Culture"), T("The current culture of the site."))
|
||||
.Token("BaseUrl", T("Base Url"), T("The base url the site."))
|
||||
.Token("TimeZone", T("Time Zone"), T("The current time zone of the site."))
|
||||
;
|
||||
|
||||
// Token descriptors for fields
|
||||
var customSettingsPart = _contentDefinitionManager.GetPartDefinition("Site");
|
||||
if (customSettingsPart != null && customSettingsPart.Fields.Any()) {
|
||||
var partContext = context.For("Site");
|
||||
foreach (var partField in customSettingsPart.Fields) {
|
||||
var field = partField;
|
||||
var tokenName = field.Name;
|
||||
|
||||
// the token is chained with the technical name
|
||||
partContext.Token(tokenName, T("{0}", field.Name), T("The setting named {0}.", partField.DisplayName), field.Name);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void Evaluate(dynamic context) {
|
||||
var forContent = context.For<ISite>("Site", (Func<ISite>)(() => _orchardServices.WorkContext.CurrentSite));
|
||||
|
||||
forContent
|
||||
.Token("SiteName", (Func<ISite, object>)(content => content.SiteName))
|
||||
.Token("SuperUser", (Func<ISite, object>)(content => content.SuperUser))
|
||||
.Token("Culture", (Func<ISite, object>) (content => content.SiteCulture))
|
||||
.Token("BaseUrl", (Func<ISite, object>) (content => content.BaseUrl))
|
||||
.Token("TimeZone", (Func<ISite, object>) (content => content.SiteTimeZone))
|
||||
;
|
||||
|
||||
if (context.Target == "Site") {
|
||||
// is there a content available in the context ?
|
||||
if (forContent.Data != null && forContent.Data.ContentItem != null) {
|
||||
var customSettingsPart = _contentDefinitionManager.GetPartDefinition("Site");
|
||||
foreach (var partField in customSettingsPart.Fields) {
|
||||
var field = partField;
|
||||
var tokenName = partField.Name;
|
||||
forContent.Token(
|
||||
tokenName,
|
||||
(Func<IContent, object>)(content => LookupField(content, field.Name).Storage.Get<string>()));
|
||||
forContent.Chain(
|
||||
tokenName,
|
||||
partField.FieldDefinition.Name,
|
||||
(Func<IContent, object>)(content => LookupField(content, field.Name)));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static ContentField LookupField(IContent content, string fieldName) {
|
||||
return content.ContentItem.Parts
|
||||
.Where(part => part.PartDefinition.Name == "Site")
|
||||
.SelectMany(part => part.Fields.Where(field => field.Name == fieldName))
|
||||
.FirstOrDefault();
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user