gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 16:54:44 26 4
gpt4 key购买 nike

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

(我开始回答这个问题,并提出了一个解决方案。发布到 SOF 以进行评论/增强。)

最佳答案

您可以在 Sitecore.Client 中了解 Sitecore 如何做到这一点。组装,Sitecore.Shell.Applications.WebEdit.Commands.ChangeLanguageSitecore.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 下创建一个新的块和按钮.此色带条也在预览模式下被引用/复制。该按钮将使用以下属性:

Ribbon Button

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

关于sitecore - 如何使用下拉菜单创建 Sitecore 功能区按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10855152/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com