2017년 8월 30일 수요일

Placeholder 컨트롤 수 제한하기

xEditor를 사용하다보면, 사이트코어 사용자의 권한 설정에 따라 특정한 Placeholder에 하나 이상의 컴포넌트를 제한해야하는 경우가 있다.

또한, 특정한 필드가 각각의 페이지가 아닌, 템플릿 __Standard Values에서 일괄적으로 변경을 해야하는 경우가 있는데, 이럴경우 사이트코어의 GetChromeDataArgs 클래스 파이프라인을 통하여 제한설정을 할수가 있다.


 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
using System;
using System.Collections.Generic;
using System.Linq;
using Sitecore.Pipelines.GetChromeData;
using System.Text.RegularExpressions;
using Sitecore.Diagnostics;
using Sitecore.Data.Items;
using Sitecore.Security.Accounts;
using Sitecore.Layouts;
using Sitecore.Data;
using Sitecore.Data.Fields;

namespace Sitecore.Placeholder.Restriction
{
    public class RemoveDeleteButton : GetPlaceholderChromeData
    {
        public override void Process(GetChromeDataArgs args)
        {
            Assert.ArgumentNotNull(args, "args");
            Assert.IsNotNull(args.ChromeData, "Chrome Data");
            if ("placeholder".Equals(args.ChromeType, StringComparison.OrdinalIgnoreCase))
            {
                string placeholderKey = args.CustomData["placeHolderKey"] as string;

                Sitecore.Data.Fields.LayoutField layoutField = new Sitecore.Data.Fields.LayoutField(Context.Item.Fields[Sitecore.FieldIDs.FinalLayoutField]);
                LayoutDefinition layoutDefinition = LayoutDefinition.Parse(layoutField.Value);
                DeviceDefinition deviceDefinition = layoutDefinition.GetDevice(Context.Device.ID.ToString());

                // Initial setting path is "/sitecore/system/Modules/Placeholder Restriction Manager/Remove Delete Button"
                Item removeDeleteButtonItem = Context.Item.Database.GetItem("/sitecore/system/Modules/Placeholder Restriction/Remove Delete Button");

                if (removeDeleteButtonItem == null)
                {
                    Assert.IsNull(removeDeleteButtonItem, "Item is null");
                    return;
                }

                MultilistField listOfRenderingItemsDelete = removeDeleteButtonItem.Fields["List of Rendering Items"];
                string listOfUserRolesDelete = removeDeleteButtonItem.Fields["List of Users and Roles"].Value.Trim();
                List<string> eachListOfUserRolesDelete = !String.IsNullOrEmpty(listOfUserRolesDelete) ? listOfUserRolesDelete.Split(';').ToList() : null;

                // Remove "Delete" button in placeholder
                if (eachListOfUserRolesDelete != null && eachListOfUserRolesDelete.Select(e => User.Current.IsInRole(e) || User.Current.LocalName.Equals(e)).Any())
                {
                    args.ChromeData.Custom["removeAddHereButton"] = true;
                    foreach (ID renderingItemId in listOfRenderingItemsDelete.TargetIDs)
                    {
                        RenderingItem r = RenderingItem.GetItem(renderingItemId, Context.Data.Database, true);
                        string renderingName = Regex.Replace(r.Name.ToLower(), @"\s+", "");
                        string displayName = Regex.Replace(args.ChromeData.DisplayName.ToLower(), @"\s+", "");

                        // Only when rendering name matches to its display name
                        // Only when rendering item's display name contains rendering name
                        if (renderingName == displayName || displayName.Contains(renderingName))
                        {
                            args.ChromeData.Custom["editable"] = false;
                        }
                    }
                }
            }
        }
    }
}


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
<?xml version="1.0" encoding="UTF-8"?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <pipelines>
      <getChromeData>
        <processor
          type="Sitecore.Placeholder.Restriction.RemoveDeleteButton, Sitecore.Placeholder.Restriction"
          patch:after="processor[@type='Sitecore.Pipelines.GetChromeData.GetPlaceholderChromeData, Sitecore.Kernel']"/>
      </getChromeData>
    </pipelines>
  </sitecore>
</configuration>