Search code examples
kentico

Kentico 13: Getting the current document when registering custom macro fields


Normally in order to get the current document in K13, we must use DI to get the IPageDataContextRetriever.

However, when trying to register a custom macro field, adding a constructor to my class (in order to use DI) prevents the class from registering:

[assembly: RegisterExtension(typeof(MyMacroFields), typeof(TreeNode))]
public class MyMacroFields : MacroFieldContainer, IService
{
    public const string TitleMacro = "MyMetaTitle";
    public const string DescriptionMacro = "MyMetaDescription";

    private readonly IPageDataContextRetriever _pageDataContextRetriever;

    public MyMacroFields(IPageDataContextRetriever pageDataContextRetriever)
    {
        _pageDataContextRetriever = pageDataContextRetriever;
    }

    protected override void RegisterFields()
    {
        base.RegisterFields();
    }
}

Is it possible to use DI somehow? Or is there another way to get the current document?


Solution

  • There is a how to article here that solves the issue:

    https://devnet.kentico.com/articles/building-an-integration-(stripe)-part-3-helpers-and-event-handlers