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

tec*_*414 5 sitecore page-editor

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

(我开始回答这个问题,并提出了一个解决方案.向SOF发帖评论/增强.)

tec*_*414 13

您可以看到Sitecore如何在Sitecore.Client程序集中完成它,Sitecore.Shell.Applications.WebEdit.Commands.ChangeLanguage以及Sitecore.Shell.Applications.WebEdit.Commands.SetLanguage.

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

ChangeCountry命令

首先,显示菜单的命令.您可以看到它显示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; }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

在Commands.config或include文件中,注册新命令.

<command name="prototype:changecountry" type="Prototype.Commands.ChangeCountry,Prototype" />
Run Code Online (Sandbox Code Playgroud)

改变国家按钮

在下创建一个新的块和按钮/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());
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

在我们的示例中,我们根据所选值设置cookie并重新加载页面.传入的值基于与菜单项关联的click事件ChangeCountry.同样,配置时命令的名称需要与ChangeCountryclick事件中使用的名称相匹配.

<command name="prototype:setcountry" type="Prototype.Commands.SetCountry,Prototype" />
Run Code Online (Sandbox Code Playgroud)