如何使用下拉菜单创建Sitecore功能区按钮?

时间:2012-06-01 17:59:53

标签: sitecore page-editor

Sitecore PageEditor和Preview界面具有语言选择器按钮,可触发“下拉”/覆盖菜单,用户可在其中选择语言。我该如何复制这种行为?

(我开始回答这个问题,并想出了一个解决方案。向SOF发帖以征求意见/提升。)

1 个答案:

答案 0 :(得分:13)

您可以在Sitecore.Client汇编,Sitecore.Shell.Applications.WebEdit.Commands.ChangeLanguageSitecore.Shell.Applications.WebEdit.Commands.SetLanguage中看到Sitecore如何做到这一点。

您需要为此创建两个自己的命令。一个命令与按钮相关联,一个命令在选择子项时执行。该示例基于更改国家/地区cookie的方案。

ChangeCountry Command

首先,显示菜单的命令。您可以看到它显示带有动态选项的Menu。覆盖GetHeaderGetIcon允许按钮本身根据用户的当前选择动态显示。

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或include文件中,注册新命令。

<command name="prototype:changecountry" type="Prototype.Commands.ChangeCountry,Prototype" />

更改国家/地区按钮

/sitecore/content/Applications/WebEdit/Ribbons/WebEdit/Experience下创建一个新的块和按钮。此功能区条带也在预览模式下被引用/复制。该按钮将使用以下属性:

Ribbon Button

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中的菜单项关联的click事件。同样,配置时命令的名称需要与ChangeCountry单击事件中使用的名称相匹配。

<command name="prototype:setcountry" type="Prototype.Commands.SetCountry,Prototype" />