API
Version 7.x > API > Custom Macro won't register View modes: 
User avatar
Kentico MVP
Kentico MVP
tfayas-avastonetech - 10/30/2013 10:10:50 AM
   
Custom Macro won't register
I created a custom macro, but for the life of me it won't register. I am using the object explorer view as well and it's not showing up. Can someone shed some light?
Here's my main class (in App_Code/CMS)
using System;
using System.Collections.Generic;
using System.Web;

using CMS.GlobalHelper;
using CMS.SettingsProvider;

/// <summary>
/// Example of custom module with custom macro methods registration.
/// </summary>
public static class GetParentClassMethods
{
/// <summary>
/// Registers all blog methods to macro resolver.
/// </summary>
public static void RegisterMethods()
{
// Register MyMethod with two parameters overload
MacroMethod myMethod = new MacroMethod("getParentCSSColor", getParentCSSColor)
{
Comment = "Scans Parents until Form property 'CSSColor' found and returns value.",
Type = typeof(string),
AllowedTypes = new List<Type>() { typeof(string) },
MinimumParameters = 0
};
myMethod.AddParameter("defaultStyle", typeof(string), "the CSS color (#000000 or inherit) if none found.");
MacroMethods.RegisterMethod(myMethod);
}


#region "Macro methods implementation"

/// <summary>
/// Gets the ancestor CSSColor form element
/// </summary>
public static string getParentCSSColor()
{
return getParentCSSColor("inherit");
}


/// <summary>
/// Scans ancestry and returns non blank CSSColor if found.
/// </summary>
/// <param name="param1">The default style to give if no ancestor with CSSColor found</param>
public static string getParentCSSColor(string defaultStyle)
{
string results = recursiveParentScan(CMS.CMSHelper.CMSContext.CurrentPageInfo);
return string.IsNullOrEmpty(results) ? defaultStyle : results;
}

/// <summary>
/// If CurrentPage has the value "CSSColor" and it's not blank, returns it, otherwise scans parent. Returns blank string if none found in ancestry.
/// </summary>
/// <param name="parentPage">The PageInfo page</param>
/// <returns>the CSSColor value or a blank string if none found in ancestry</returns>
private static string recursiveParentScan(CMS.DocumentEngine.PageInfo currentPage) {
if (currentPage.NodeLevel == 0)
return string.IsNullOrEmpty(currentPage.GetValue("CSSColor").ToString()) ? "" : currentPage.GetValue("CSSColor").ToString();
else
return string.IsNullOrEmpty(currentPage.GetValue("CSSColor").ToString()) ? recursiveParentScan(currentPage.ParentPageInfo) : currentPage.GetValue("CSSColor").ToString();
}


// Add your own custom methods here

#endregion


#region "MacroResolver wrapper methods"

/// <summary>
/// Wrapper method of MyMethod suitable for MacroResolver.
/// </summary>
/// <param name="parameters">Parameters of the method</param>
public static object getParentCSSColor(params object[] parameters)
{
switch (parameters.Length)
{
case 0:
return getParentCSSColor();
case 1:
// Overload with one parameter
return getParentCSSColor(ValidationHelper.GetString(parameters[0], ""));
default:
// No other overload is supported
throw new NotSupportedException();
}
}

// Add wrappers for MacroResolver for your own custom methods here
// The signature of wrapper methods has to be "public static object MyMethodName(params object[] parameters)"

#endregion
}


ANd here's my Loader:
using System;

using CMS.GlobalHelper;
using CMS.OutputFilter;
using CMS.SettingsProvider;
using CMS.IO;

/// <summary>
/// Sample custom module class. Partial class ensures correct registration. For adding new methods, modify SampleModule inner class.
/// </summary>
[SampleMacroLoader]
public partial class CMSModuleLoader
{
/// <summary>
/// Attribute class ensuring correct initialization of methods in macro resolver. You do not need to modify this class.
/// </summary>
private class GetParentClassMacroLoader : CMSLoaderAttribute
{
/// <summary>
/// Registers module methods.
/// </summary>
public override void Init()
{
// -- Custom macro methods
GetParentClassMethods.RegisterMethods();
}

}
}


ANd lastly, here's my caller:
{% ProcessCustomMacro("getParentCSSColor", "") %}

Should return "inherit" if none found, any help?

User avatar
Kentico Consulting
Kentico Consulting
Kentico_RichardS - 10/31/2013 2:45:10 AM
   
RE:Custom Macro won't register
Hi Trevor,

Thank you for your message.

Im quite confused with your code. If you want to create a custom macro please follow the http://devnet.kentico.com/docs/devguide/types_of_macros.htm.

You only need only 1 file in order to get the macro working. I'll send you a whole example of my working macro loader file in CMS/AppCode..

This is how the file can look like:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using CMS.SettingsProvider;
using CMS.GlobalHelper;
using CMS.CMSHelper;

/// <summary>
/// Summary description for CustomMacroLoader
/// </summary>
[CustomMacroLoader]
public partial class CMSModuleLoader
{
/// <summary>
/// Attribute class ensuring the registration of macro handlers.
/// </summary>
private class CustomMacroLoader : CMSLoaderAttribute
{
public override void Init()
{
// Assigns a custom macro resolving handler.
MacroResolver.OnResolveCustomMacro += MacroResolver_OnResolveCustomMacro;
}


/// <summary>
/// Resolves custom macros.
/// </summary>
/// <param name="sender">Sender</param>
/// <param name="e">Event arguments representing the resolved macro</param>
private void MacroResolver_OnResolveCustomMacro(object sender, MacroEventArgs e)
{
// Checks that the macro is not resolved yet.
if (!e.Match)
{
// Defines the return values of specific custom macro expressions.
switch (e.Expression.ToLower())
{
// Handles the {% ProcessCustomMacro("CustomExpression", "") %} macro.
case "customexpression":
e.Match = true;
e.Result = "Resolved expression";
break;

// url
case "myurl":
e.Match = true;
string url = HttpContext.Current.Request.QueryString["UserName"];
// get url
url = "Special user " + url;
e.Result = url;
break;
}
}
}


Kind regards,
Richard Sustek

User avatar
Kentico MVP
Kentico MVP
tfayas-avastonetech - 10/31/2013 8:28:36 AM
   
RE:Custom Macro won't register
I may have been using the wrong version then, they had a two file process in the samples of the Kentico that I got for registering Custom Macro Methods, didn't know that was not needed and i could use just 1 file.

Thanks so much, i finally have the ability to do more advanced coding, was banging my head against a wall trying to work around it. Got it working!