Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Mikael Axel Kleinwort 106 posts 414 karma points c-trib
    1 week ago
    Mikael Axel Kleinwort
    0

    Modify data values of certain data types before they are rendered on frontend

    I am looking for a way to change the data of certain data types before it is rendered on a page.

    Background:

    • for selected properties (data types), content editors can use certain placeholder keys in text.
    • if a page renders such a property, the placeholder keys need to be replaced with real-time data. The replacement is only performed for rendering on frontend.

    Right now I am modifying the values of the affected properties in the view files, and it works, but it requires a lot of manually written code and I am looking for an automatic way.

    What I am struggling with is to get my hands on the data before it hits the controller. I need some sort of event that is called for each doc type property regardless of its datatype, so I can check the property's data type and perform the replacement if needed.

    How can I approach this? Any input is greatly welcome :-)

  • Marc Goodson 1621 posts 10721 karma points MVP 6x c-trib
    1 week ago
    Marc Goodson
    0

    Hi Mikael

    There is an event that is fired just after the 'PublishedContent' for a particular page is assembled.... and just before it is sent to the controller for processing and rendering...

    It's called the PublishedRequest.Prepared event!

    https://our.umbraco.com/apidocs/v8/csharp/api/Umbraco.Web.Routing.PublishedRequest.html#UmbracoWebRoutingPublishedRequestPrepared

    And that gives you access to all the content that is about to be prepared and you can manipulate it here to make last minute adjustments... would that give you what you need to be able to make your placeholder replacements in one place?

    To hook into this in V8 you'd create a composer + component

    using System;
    using Umbraco.Core;
    using Umbraco.Core.Composing;
    using Umbraco.Web.Routing;
    
    namespace Umbraco8.Components
    {
        [RuntimeLevel(MinLevel = RuntimeLevel.Run)]
        public class SubscribeToPublishedRequestComposer : ComponentComposer<PublishedRequestComponent>
        { }
    
      public class PublishedRequestComponent : IComponent
        {
            public void Initialize()
            {
                PublishedRequest.Prepared += PublishedRequest_Prepared;
            }
    
            private void PublishedRequest_Prepared(object sender, EventArgs e)
            {
                 var request = sender as PublishedRequest;
                 // do something…
            }
    
            public void Terminate() {
                //unsubscribe during shutdown
                PublishedRequest.Prepared -= PublishedRequest_Prepared;
    }
        }
    

    anyway might be worth experimenting with.

    regards

    marc

Please Sign in or register to post replies

Write your reply to:

Draft