こちらの記事はSitecore Advent Calender 2014の12月17日の記事です。
今日は”テンプレートから挿入”について書いてみたいと思います。
”テンプレートから挿入”がバージョン7となって、テンプレートの”検索”タブが追加され、ユーザーに使いやすさを提供した一方で適切でないテンプレートを選択し、問題となる可能性が出てきます。
サイトコアの管理者また、CMSユーザが下記のアイテムへのアクセス権限を持っていればこの”テンプレートから挿入”が表示されます。
基本的にテンプレートを作成する際にスタンダードバリューにて挿入オプションを設定するのはお勧めですが、忘れたりする場合を考えてこの”テンプレートから挿入”を隠す場合もあります。これを対処するにはいくつかの方法を書いてみたいと思います。
サイトコアの設定のみ対応する
コードを使用せず、設定のみで対応するにはサイトコアのCoreデーターベースにてこの“Insert from Template“アイテムを隠すまたアクセスを制限すればいいです。
一番簡単なのはこのアイテムを別の場所へ移動して隠します。
このアイテムが移動されると、メニューから隠されます。
このアイテムのアクセスを制限し、ユーザー別に管理することもできます。
サイトコアの設定とコードで対応する
もっと柔軟な制御をしたいなら、コマンドテンプレートを実装するします。ルールエンジンを使用し、ルールで制御したり、uiGetMastersパイプラインプロセッサを挿入し、挿入オプションを割り当てたりすることもできます。JohnWestさんがこれについて詳しい手順をの記事にて書いていますので参照してください。
上記に参照している日本語のドキュメントは下記になります:直接関連しないですが、もし既存のメニューにてエントリをコマンドを追加されたいなら、この記事を目を通すことをお勧めします.
まず、右クリックして挿入するというのはおそらくコマンドアクションであろうと思って、Commands.Configをみたら、item:newというコマンドがありました。
DLLをみれば、ItemNew にあるGetSubmenuItems関数にてテンプレートから挿入が追加されています。この関数をオーバーライトすればいいかと思いました。
必要なコードをプロジェクトへコピーしコマンドアクションを変更します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
namespace local.sc75.Website.sitecore.shell.Framework.Commands { #region using System; using System.Collections; using Sitecore; using Sitecore.Data.Items; using Sitecore.Data.Masters; using Sitecore.Diagnostics; using Sitecore.Security.Accounts; using Sitecore.Shell.Framework.Commands; using Sitecore.Web.UI.HtmlControls; #endregion /// <summary> /// Represents the ItemNew command. /// </summary> [Serializable] public class ItemNew : Command { #region Public Methods and Operators /// <summary> /// Executes the command in the specified context. /// </summary> /// <param name="context">The context.</param> public override void Execute(CommandContext context) { throw new NotImplementedException(); } /// <summary> /// Gets the click event. /// </summary> /// <param name="context">The context.</param> /// <param name="click">The default click event.</param> /// <returns> /// The click event. /// </returns> public override string GetClick(CommandContext context, string click) { return string.Empty; } /// <summary> /// Gets the submenu items. /// </summary> /// <param name="context">The context.</param> /// <returns> /// The submenu. /// </returns> public override Control[] GetSubmenuItems(CommandContext context) { if (context.Items.Length != 1) { return null; } var parent = context.Items[0]; var arrayList = new ArrayList(); foreach (var master in Masters.GetMasters(context.Items[0])) { var menuItem = new MenuItem(); menuItem.Header = master.DisplayName; menuItem.Icon = master.Appearance.Icon; menuItem.Click = parent == null ? Masters.GetClick(master) : Masters.GetClick(master, parent); arrayList.Add(menuItem); } //custom insert logic bool showInsertFormTemplate = false; //example when user is an admin //サンプル:特定のアドメインユーザーのみ表示する User user = User.FromName(@"sitecore\hyin", true); if (user != null && user.IsAdministrator) showInsertFormTemplate = true; //サンプル:特定のルールに属するユーザのみ表示する //Sitecore.Security.Accounts.Role checkRole = Sitecore.Security.Accounts.Role.FromName("sitecore\\Sitecore Client Maintaining"); if(showInsertFormTemplate) InsertFromTemplate(arrayList, parent.ID.ToString()); return arrayList.ToArray(typeof(Control)) as Control[]; } public virtual ArrayList InsertFromTemplate(ArrayList al, string pId) { var obj1 = Context.Database.Items["/sitecore/content/Applications/Content Editor/Menues/New"]; if (obj1 != null && obj1.HasChildren) { if (al.Count > 0) { al.Add(new MenuDivider()); } foreach (Item obj2 in obj1.Children) { var menuItem = new MenuItem(); menuItem.Header = obj2["Display Name"]; menuItem.Icon = obj2["Icon"]; menuItem.Click = obj2["Message"].Replace("$Target", pId); al.Add(menuItem); } } return al; } /// <summary> /// Queries the state of the command. /// </summary> /// <param name="context">The context.</param> /// <returns> /// The state of the command. /// </returns> /// public override CommandState QueryState(CommandContext context) { Error.AssertObject(context, "context"); if (context.Items.Length != 1 || !context.Items[0].Access.CanCreate() || !context.Items[0].Access.CanWriteLanguage()) { return CommandState.Disabled; } else { return base.QueryState(context); } } #endregion } } |