Sitecore PageEditor和Preview界面具有一个语言选择器按钮,用于触发用户可以选择语言的“下拉”/叠加菜单。我如何复制这种行为?如何使用下拉菜单创建Sitecore功能区按钮?
(我要回答这个问题,一个解决方案上来。张贴到SOF的意见/增强。)
Sitecore PageEditor和Preview界面具有一个语言选择器按钮,用于触发用户可以选择语言的“下拉”/叠加菜单。我如何复制这种行为?如何使用下拉菜单创建Sitecore功能区按钮?
(我要回答这个问题,一个解决方案上来。张贴到SOF的意见/增强。)
你可以看到Sitecore的是如何做的Sitecore.Client
装配,Sitecore.Shell.Applications.WebEdit.Commands.ChangeLanguage
和Sitecore.Shell.Applications.WebEdit.Commands.SetLanguage
。
您需要为此创建两个自己的命令。一个命令与按钮关联,一个命令在选择子项目时执行。该示例基于更改国家cookie的场景。
ChangeCountry命令
首先,命令以显示菜单。您可以看到它显示带动态选项的Menu
。覆盖GetHeader
和GetIcon
允许按钮本身基于用户的当前选择是动态的。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sitecore.Shell.Applications.WebEdit.Commands;
using Sitecore.Diagnostics;
using Sitecore.Data.Items;
using Sitecore.Web.UI.Sheer;
using Sitecore.Web.UI.HtmlControls;
using Sitecore.StringExtensions;
using System.Web;
namespace Prototype.Commands
{
public class ChangeCountry : WebEditCommand
{
protected Dictionary<string, CountryOption> _countries = new Dictionary<string, CountryOption>
{
{"US", new CountryOption {
ID = "US",
Name = "United States",
Icon = "Flags/32x32/flag_usa.png"
}},
{"CA", new CountryOption {
ID = "CA",
Name = "Canada",
Icon = "Flags/32x32/flag_canada.png"
}},
{"MX", new CountryOption {
ID = "MX",
Name = "Mexico",
Icon = "Flags/32x32/flag_mexico.png"
}},
{"DE", new CountryOption {
ID = "DE",
Name = "Germany",
Icon = "Flags/32x32/flag_germany.png"
}}
};
public override void Execute(Sitecore.Shell.Framework.Commands.CommandContext context)
{
Assert.ArgumentNotNull(context, "context");
if (context.Items.Length == 1)
{
Item item = context.Items[0];
SheerResponse.DisableOutput();
Menu control = new Menu();
//replace with lookup and loop of available values
foreach (var key in _countries.Keys)
{
var country = _countries[key];
string id = country.ID;
string header = country.Name;
string icon = country.Icon;
string click = "prototype:setcountry(country={0})".FormatWith(key);
control.Add(id, header, icon, string.Empty, click, false, string.Empty, MenuItemType.Normal);
}
SheerResponse.EnableOutput();
SheerResponse.ShowPopup("ChangeCountryButton", "below", control);
}
}
public override string GetHeader(Sitecore.Shell.Framework.Commands.CommandContext context, string header)
{
HttpCookie country = HttpContext.Current.Request.Cookies["country"];
if (country != null && _countries.ContainsKey(country.Value))
{
return _countries[country.Value].Name;
}
return base.GetHeader(context, header);
}
public override string GetIcon(Sitecore.Shell.Framework.Commands.CommandContext context, string icon)
{
HttpCookie country = HttpContext.Current.Request.Cookies["country"];
if (country != null && _countries.ContainsKey(country.Value))
{
return _countries[country.Value].Icon;
}
return base.GetIcon(context, icon);
}
protected class CountryOption
{
public string ID { get; set; }
public string Name { get; set; }
public string Icon { get; set; }
}
}
}
在Commands.config或包含文件中,注册新命令。
<command name="prototype:changecountry" type="Prototype.Commands.ChangeCountry,Prototype" />
改变国家按钮
/sitecore/content/Applications/WebEdit/Ribbons/WebEdit/Experience
下创建一个新的块和按钮。此功能区带也在预览模式下被引用/复制。该按钮将使用以下属性:
的Click场必须在命令的名称匹配,并且ID字段必须在上述SheerResponse.ShowPopup
呼叫提供元素ID匹配。
SetCountry命令
其次是在选择你的菜单/下拉列表中的项目将被称为命令。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sitecore.Shell.Applications.WebEdit.Commands;
using System.Net;
using Sitecore.Diagnostics;
using Sitecore.Web.UI.Sheer;
using System.Web;
namespace Prototype.Commands
{
public class SetCountry : WebEditCommand
{
public override void Execute(Sitecore.Shell.Framework.Commands.CommandContext context)
{
Assert.ArgumentNotNull(context, "context");
var country = context.Parameters["country"];
Assert.IsNotNullOrEmpty(country, "Country not found");
HttpCookie cookie = new HttpCookie("country", country);
HttpContext.Current.Response.Cookies.Add(cookie);
WebEditCommand.Reload(WebEditCommand.GetUrl());
}
}
}
在我们的示例中,我们将根据所选值并重新加载页面来设置Cookie。传入的值基于与ChangeCountry
中的菜单项关联的点击事件。同样,配置时命令的名称需要与ChangeCountry
单击事件中使用的名称相匹配。
<command name="prototype:setcountry" type="Prototype.Commands.SetCountry,Prototype" />