5

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

(我开始回答这个问题,并想出了一个解决方案。发布到 SOF 以征求意见/增强。)

4

1 回答 1

13

您可以看到 Sitecore 在Sitecore.Client程序集中是如何做到的,Sitecore.Shell.Applications.WebEdit.Commands.ChangeLanguage并且Sitecore.Shell.Applications.WebEdit.Commands.SetLanguage.

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

更改国家命令

首先,显示菜单的命令。您可以看到它显示了一个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 或包含文件中,注册新命令。

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

更改国家按钮

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

功能区按钮

Click 字段必须与您的命令名称匹配,ID 字段必须与上述SheerResponse.ShowPopup调用中提供的元素 ID 匹配。

设置国家命令

接下来是选择菜单/下拉菜单中的项目时将调用的命令。

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" />
于 2012-06-01T17:59:52.767 回答