UI Interface Visibility Condition Macro

Charles Matvchuk asked on March 21, 2017 21:17

I am having a challenge writing a Macro. I have a UI element I only want to show when the page type is of a certain class. I can do this quite easily in C# by getting the NodeID and resolving it to get ClassName.

As a macro I am unsure how to write it.

I have a table(class) in a custom module that holds a list of the pagetypes (ClassName) of allowed pages to be able to view this tab in the UI. I need to check the table and determine if the class exists and set the Visibility condition to true.

Below is my C# code to do such and it works fine in simulation, but I guess I need to do this in the Visibility Condition with a Macro. I am passing in the NodeID, since I researched and found that I have access to that on a UI Page tab.

    public static bool ShowFeaturesTab(int nodeID)
{
    var docClass = DataClassInfoProvider.GetDataClassInfo(DocumentHelper.GetDocument(nodeID, new TreeProvider()).ClassName);

    var userQuery = XCM.FeatureDocumentTypesAllowed.Feature_DocumentTypes_AllowedInfoProvider.GetFeature_DocumentTypes_Alloweds().WhereEquals("DocumentType", docClass.ClassName);
    if (userQuery.Count > 0)
    {
        return true;
    }
    else
    {
        return false;
    }

}

My Class is FeatureDocumentTypesAllowed which contains a field called "DocumentType" which holds the Class of the page. Any advice would truly be appreciated. I seem to always get hung up on writing Macro's (K#).

Correct Answer

Trevor Fayas answered on March 21, 2017 21:25

If you already have the C# logic, just wrap it in a macro function :)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using CMS.MacroEngine;
using CMS.DataEngine;
using CMS.DocumentEngine;
using CMS;
using CMS.Helpers;


/// <summary>
/// Summary description for CustomMacroMethods
/// </summary>
[assembly: RegisterExtension(typeof(CustomMacroMethods), typeof(UtilNamespace))]
public class CustomMacroMethods : MacroMethodContainer
{
    [MacroMethod(typeof(bool), "Determines if the Features Tab should be shown", 1)]
    [MacroMethodParam(0, "nodeID", typeof(int), "The Node ID")]
    public static object ShowFeaturesTab(EvaluationContext context, params object[] parameters)
    {
        switch (parameters.Length)
        {
            case 1:
                var docClass = DataClassInfoProvider.GetDataClassInfo(DocumentHelper.GetDocument(ValidationHelper.GetInteger(parameters[0], -1), new TreeProvider()).ClassName);

                var userQuery = XCM.FeatureDocumentTypesAllowed.Feature_DocumentTypes_AllowedInfoProvider.GetFeature_DocumentTypes_Alloweds().WhereEquals("DocumentType", docClass.ClassName);
                if (userQuery.Count > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
                break;
            default:
                throw new NotSupportedException();
        }
    }
}

Called by doing {% Util.ShowFeaturesTab(TheNodeID) |(identity)GlobalAdministrator%}

0 votesVote for this answer Unmark Correct answer

Recent Answers


Charles Matvchuk answered on March 22, 2017 01:35

Trevor Thanks so much. Works like a charm.

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.