Search code examples
sitecoresitecore8

Extending the RenderField.GetFieldValue processor - not all items associated with the page are processed


I have a rich text field that has certain tokens ({{alt}} and {{title}}) for embedded images in the HTML. I extended the Sitecore.Pipelines.RenderField.GetFieldValue processor so that when a page is rendered on the front end, the processor will replace those tokens with the alt and title field values from the media item. Unfortunately my custom processor only processes a certain set of items (specifically WFFM fields), it does not process my Body field which has the tokens, even though I know the page I am browsing is rendering that field (the code run on the layout is @Html.Sitecore().Field("Body") which means it should get processed by the pipeline, correct?)

I also tried the GetTextFieldValue and GetMemoFieldValue processors but the same items were processed. Looking for some guidance as to specifically which fields are supposed to get processed by this pipeline.

Here is my Process() function:

    public void Process(RenderFieldArgs args)
    {
        Assert.ArgumentNotNull(args, "args");
        Assert.ArgumentNotNull(args.Item, "args.Item");
        Assert.ArgumentNotNull(args.GetField(), "args.GetField()");

        if (args.Item.Database == Database.GetDatabase("web"))
        {
            if (args.GetField().Value.Contains("{{alt}}"))
            {
                args.GetField().Value = ReplaceAltToken(args.GetField().Value);
            }

            if (args.GetField().Value.Contains("{{title}}"))
            {
                args.GetField().Value = ReplaceTitleToken(args.GetField().Value);
            }

            if (args.GetField().Name == "Body")
            {
                // Since we're rendering the body field differently we need to expand dynamic links
                args.Item.Editing.BeginEdit();
                args.GetField().Value = Sitecore.Links.LinkManager.ExpandDynamicLinks(args.GetField().Value);
                args.Item.Editing.EndEdit();
            }
        }
    }

My config include file:

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
<sitecore>
    <pipelines>
        <renderField>
            <processor
                type="[Redacted].Processor.RenderField.GetFieldValueExtended, [Redacted].Processor"
                patch:after="processor[@type='Sitecore.Pipelines.RenderField.GetFieldValue, Sitecore.Kernel']" />
        </renderField>
    </pipelines>
</sitecore>


Solution

  • There were multiple steps to resolving this issue. Unchecking Cacheable on all renderings was an important first step. The rest of the issues I encountered were most likely specific to my solution (not well designed and will be refactored soon).

    My local site did not have the most up to date files. When I updated the files, I still ran into the Attempt to retrieve context object of type 'Sitecore.Mvc.Presentation.PageContext' from empty stack error, which I was able to workaround by calling Sitecore.Web.UI.WebControls.FieldRenderer.Render(item, "Body") in a simple GetBody() function in the model, instead of Html.Sitecore().Field("Body"). Then in the view I simply put @Html.Raw(Model.Body).

    I think the .Field() helper did not work because the item associated with the field requested was unknown, so yet another workaround is using Html.Sitecore().Field("Body", item) which specifies the item directly in the parameters.