Copied to clipboard

Flag this post as spam?

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


  • Tim C 161 posts 528 karma points
    May 25, 2016 @ 11:21
    Tim C
    0

    Automatically redirect to parent node

    If have created a comments document to become a child of a main document. I only ever show them as a list underneath the parent document (ie on the parent's template)

    I am also using the excellent Frontend Editing package so users can edit from the front end.

    The problem is when you save a document, it then tries to open that document.

    I would prefer it to go back to the parent document. In other words, I would like it to auto redirect,

    I know I could use umbracoRedirect or umbracoInternalRedirectId but that would mean manually picking the parent when editing the comment, which is undesirable. Is there any way to auto redirect to the parent node?

    Otherwise, I guess I will have to do some javascript redirecting on a template on the comment

  • Dennis Adolfi 1082 posts 6450 karma points MVP 6x c-trib
    May 25, 2016 @ 13:00
    Dennis Adolfi
    100

    Hi Tim.

    You can achieve this by creating your own UrlProvider and override the URL for nodes of type comment to redirect to its parent. There is a great tutorial on 24days by Jeroen Breuer about creating you own UrlProvider and ContentFinder (but i dont think you need a contentfinder for this scenario): http://24days.in/umbraco/2014/urlprovider-and-contentfinder/

    Based on your post and Jeroens tutorial, i think something like this would do the trick for you:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using Umbraco.Core;
    using Umbraco.Web;
    using Umbraco.Web.Routing;
    
    namespace MyUmbracoApplication
    {
        public class CommentUrlProvider : IUrlProvider
        {
            public string GetUrl(UmbracoContext umbracoContext, int id, Uri current, UrlProviderMode mode)
            {
                var content = umbracoContext.ContentCache.GetById(id);
                if (content != null && content.DocumentTypeAlias == "comment" && content.Parent != null)
                {
                    return content.Parent.Url; //The comment will be redirected to its parent.
                }
    
                return null;
            }
    
            public IEnumerable<string> GetOtherUrls(UmbracoContext umbracoContext, int id, Uri current)
            {
                return Enumerable.Empty<string>();
            }
        }
    
        public class ApplicationStartUp : ApplicationEventHandler
        {
            protected override void ApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
            {
                UrlProviderResolver.Current.InsertTypeBefore<DefaultUrlProvider, CommentUrlProvider>();
            }
        }
    }
    

    I have´nt tried this together with the Frontend Editing package, but here´s the result for me when i´ve tried to set up a similar backoffice as you with articles and comments. (See animation)

    enter image description here

    Hope this could be helpful!

  • Tim C 161 posts 528 karma points
    May 25, 2016 @ 13:44
    Tim C
    1

    Thanks, that's really useful.

    Pardon me if I am being stupid, but does this code go in App_Code as an addition to core Umbraco, and don't want it overwritten with new releases?

  • Dennis Adolfi 1082 posts 6450 karma points MVP 6x c-trib
    May 25, 2016 @ 14:04
    Dennis Adolfi
    0

    Yes, you can put it in App_Code. It wont get overwritten in future releases.

    Glad i could help! Best of luck to you!!

  • Tim C 161 posts 528 karma points
    May 25, 2016 @ 14:59
    Tim C
    1

    Thanks!

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies