Created
January 21, 2018 10:28
-
-
Save hermanussen/d02eda19318947d0ce7c4f196bb95c07 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using Sitecore.Shell.Applications.ContentEditor.Gutters; | |
using Sitecore.Data.Items; | |
using Sitecore.Diagnostics; | |
using Sitecore.Data.Fields; | |
using Sitecore.Rules; | |
namespace RuleBasedGutterRenderer | |
{ | |
public class RuleBased : GutterRenderer | |
{ | |
protected override GutterIconDescriptor GetIconDescriptor(Item item) | |
{ | |
Assert.ArgumentNotNull(item, "item"); | |
string datasource = Parameters["datasource"]; | |
string fieldName = Parameters["fieldname"]; | |
Assert.IsNotNull(datasource, "No datasource set on this rule based gutter renderer"); | |
Assert.IsNotNull(fieldName, "No field name set on this rule based gutter renderer"); | |
Item dsItem = item.Database.GetItem(datasource); | |
Assert.IsNotNull(dsItem, string.Format("Datasource '{0}' is not valid", datasource)); | |
Assert.IsNotNull(dsItem.Fields[fieldName], string.Format("Item '{0}' does not have a field named '{1}'", item.ID, fieldName)); | |
RuleList<RuleContext> rules = RuleFactory.GetRules<RuleContext>(new Item[] { dsItem }, fieldName); | |
RuleContext ctx = new RuleContext() { Item = item }; | |
RuleStack ruleStack = new RuleStack(); | |
foreach (Rule<RuleContext> rule in rules.Rules) | |
{ | |
rule.Condition.Evaluate(ctx, ruleStack); | |
} | |
if ((bool)ruleStack.Pop()) | |
{ | |
GutterIconDescriptor descriptor = new GutterIconDescriptor(); | |
descriptor.Icon = dsItem.Appearance.Icon; | |
descriptor.Tooltip = string.Format("This item matches the rules as configured in {0} (field={1}).", dsItem.Paths.FullPath, fieldName); | |
return descriptor; | |
} | |
return null; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment