特定のタイプのアイテムが特定のタイプのアイテムの子を 1 つしか持てないようにする方法はサイトコアにありますか? ルール エンジン クックブックを読んでいますが、詳細がわかりません。
1917 次
1 に答える
6
私が担当したサイトの 1 つでは、特定のアイテム タイプの下に存在できる子アイテムは 6 つまでという要件がありました。挿入オプション ルールの使用を検討しましたが、アイテムのコピー、移動、または複製を防止できなかったため、アイデアを放棄することにしました。
代わりにitem:created
、このタスク専用のハンドラーでイベントを拡張することにしました。以下は、それがどのように機能するかの簡略化された例です。明白な改善の 1 つは、親アイテムのフィールドから子の最大制限を取得することです (もちろん、管理者のみに表示されます)。ここでもルールエンジンを活用することもできます...
public void OnItemCreated(object sender, EventArgs args)
{
var createdArgs = Event.ExtractParameter(args, 0) as ItemCreatedEventArgs;
Sitecore.Diagnostics.Assert.IsNotNull(createdArgs, "args");
if (createdArgs != null)
{
Sitecore.Diagnostics.Assert.IsNotNull(createdArgs.Item, "item");
if (createdArgs.Item != null)
{
var item = createdArgs.Item;
// NOTE: you may want to do additional tests here to ensure that the item
// descends from /sitecore/content/home
if (item.Parent != null &&
item.Parent.TemplateName == "Your Template" &&
item.Parent.Children.Count() > 6)
{
// Delete the item, warn user
SheerResponse.Alert(
String.Format("Sorry, you cannot add more than 6 items to {0}.",
item.Parent.Name), new string[0]);
item.Delete();
}
}
}
}
于 2012-08-29T20:43:24.127 に答える