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" />