I was playing with SXA and wanted to have a custom element within a rendering variant that identifies current control presents on a page, otherwise it is being invisible. Looking for such a rule I suddenly realised it is not there, so had to create my custom one. I am sharing it just in case it might be useful to someone looking for a similar solution.
So, as per Rules Engine Cookbook, you create a tag, custom condition and relevant element for it:
The code is simple.
public class IsExperienceEditorEditing<T> : StringOperatorCondition<T> where T : RuleContext
{
public int Value { get; set; }
protected override bool Execute(T ruleContext)
{
return Sitecore.Context.PageMode.IsExperienceEditorEditing;
}
}
Also, working with SXA aligned with Helix principles, it would make sense to create a foundation module for custom rules, drop the above class inside as well as serialization for newly created tag and elements:
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:role="http://www.sitecore.net/xmlconfig/role/" xmlns:unicorn="http://www.sitecore.net/xmlconfig/unicorn/">
<sitecore unicorn:require="On">
<unicorn>
<configurations>
<configuration name="Foundation.Rules" description="Foundation Rules" extends="Helix.Foundation">
<predicate type="Unicorn.Predicates.SerializationPresetPredicate, Unicorn" singleInstance="true">
<include name="$(layer).$(module).Tags" database="master" path="/sitecore/system/Settings/Rules/Definitions/Tags/Experience Editor" />
<include name="$(layer).$(module).Elements" database="master" path="/sitecore/system/Settings/Rules/Definitions/Elements/Experience Editor" />
</predicate>
</configuration>
</configurations>
</unicorn>
</sitecore>
</configuration>
So now I can have an element withing rendering variant that is shown only when in editing mode:Hope you find this helpful!