Copied to clipboard

Flag this post as spam?

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


  • Steffen Hornbæk Nielsen 54 posts 165 karma points
    Jan 10, 2024 @ 09:34
    Steffen Hornbæk Nielsen
    0

    Add custom handler to "save and preview" event?

    Hi folks,

    I im trying to create a custom handler, for when the button "save and preview" is pressed. So far i haven't had any luck doing this, as i have been trying to achieve this using the normal notification system in Umbraco 11.

    We are doing som custom handling on save, but we need to know if the user pressed the "Save and preview" button instead of just save.

    Anyone with an idea on how to achieve this?

  • Steffen Hornbæk Nielsen 54 posts 165 karma points
    Jan 15, 2024 @ 13:39
    Steffen Hornbæk Nielsen
    0

    I have had some time to look more into this and these are my findings.

    1. The "Save and preview" works by first saving the content, and then "ui" does the go-to-preview bit.
    2. It is not directly possible by default, to interupt the event, since umbraco does not produce such an event.

    Solution: Hijack the preview url using a new controller class, and do the custom handling of data, before redirecting the user further.

    Hope this can help other people in the future, but please Umbraco, add this to the event system - please :D

  • Asembli 86 posts 260 karma points
    Jan 15, 2024 @ 21:21
    Asembli
    0

    I usually don't like to give links, but doesn't this help you? https://docs.umbraco.com/umbraco-cms/reference/notifications/contentservice-notifications ?

    I just copy-pasted this example and it works ok. All the events you need are covered here.

    You can only rename DontShout to something more edible :)

    Regards

  • Sibren 40 posts 212 karma points c-trib
    May 05, 2024 @ 19:34
    Sibren
    0

    I might be a little late to the party, but check this page out: https://docs.umbraco.com/umbraco-cms/reference/routing/custom-controllers

    public class CustomControllerRouteController : RenderController
    {
        public CustomControllerRouteController(ILogger<RenderController> logger, ICompositeViewEngine compositeViewEngine, IUmbracoContextAccessor umbracoContextAccessor) : base(logger, compositeViewEngine, umbracoContextAccessor)
        {
    
        }
        public override IActionResult Index()
        {
            // you can access the CurrentPage directly in the controller, but just so you know you can use it:
            var currentPage = CurrentPage;
            if (UmbracoContext.InPreviewMode)
            {
                // Do something
            }
            // maybe do an else here, depending on what you want to do above
            return CurrentTemplate(CurrentPage);
        }
    }
    

    Then in your program.cs below the .Build() of your builder.CreateUmbracoBuilder add:

    builder.Services.Configure<UmbracoRenderingDefaultsOptions>(c =>
    {
        c.DefaultControllerType = typeof(CustomControllerRouteController);
    });
    
Please Sign in or register to post replies

Write your reply to:

Draft