Copied to clipboard

Flag this post as spam?

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


  • Thomas Beckert 193 posts 469 karma points
    Jun 19, 2022 @ 10:23
    Thomas Beckert
    0

    Unpublished Status / unpublished class not set in the tree view

    In several of my different umbraco pages, there is a strange issue: if I unpublish a node, it will not be greyed out by getting the class "not-published". Instead it stays in normal color as the published nodes, so the editor can not make a difference between published, unpublished and drafted nodes.

    Thanks to codegarden 22 - #cg22 - I found a work-arround I want to share with you guys.

    It is not very performant, as we have to do an additional Database Call in the render tree event, but for small pages it works und fixes the issue above.

    If anyone has a better idea how to improve the work arround, just let me know.

    So here we go, just create a class that hook into the treerender event and execute this code:

        using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Linq;
    using System.Web;
    using Umbraco.Core;
    using Umbraco.Core.Composing;
    using Umbraco.Core.Models;
    using Umbraco.Core.Services;
    using Umbraco.Web.Models.Trees;
    using Umbraco.Web.Trees;
    using IComponent = Umbraco.Core.Composing.IComponent;
    
    namespace umbBertling2020.Controllers
    {
        [RuntimeLevel(MinLevel = RuntimeLevel.Run)]
        public class TreerenderEventComposer : ComponentComposer<TreerenderEvent>, IUserComposer
        {
        }
    
        public class TreerenderEvent : IComponent
        {
    
            private readonly IContentService _contentService;
    
            public TreerenderEvent(IContentService contentService)
            {
                this._contentService = contentService;
            }
    
            // register the event listener with a component:
            public void Initialize()
            {
                TreeControllerBase.TreeNodesRendering += TreeControllerBase_TreeNodesRendering;
            }
    
            public void Terminate()
            {
                // unsubscribe on shutdown
                TreeControllerBase.TreeNodesRendering -= TreeControllerBase_TreeNodesRendering;
            }
    
            // the event listener method:
            private void TreeControllerBase_TreeNodesRendering(TreeControllerBase sender, TreeNodesRenderingEventArgs e)
            {
                // this example will filter any content tree node whose node name starts with
    
                // 'Private', for any user that is in the customUserGroup
                if (sender.TreeAlias == "content")
                {
                    List<int> nodeCollection = new List<int>();
                    foreach(TreeNode node in e.Nodes)
                    {                   
                        nodeCollection.Add(int.Parse(node.Id.ToString()));
                    }
    
                    foreach (IContent n in _contentService.GetByIds(nodeCollection))
                    {
                        if (n != null && n.PublishedState == PublishedState.Unpublished)
                        {
                            e.Nodes.Where(x => x.Id.ToString() == n.Id.ToString()).FirstOrDefault().CssClasses.Add("not-published");
                        }
                    }
    
                }
            }
        }
    }
    
  • Bart 7 posts 88 karma points
    Oct 24, 2022 @ 11:38
    Bart
    0

    Hey Thomas,

    Thanks for supplying a solution. I will try to see if this also helps on our side. Did you ever find out why this problem occurred? (I remember this working in the past but not sure if it was on umbraco V9 or V10)

    Thanks.

  • Thomas Beckert 193 posts 469 karma points
    Oct 25, 2022 @ 07:27
    Thomas Beckert
    0

    Hi, Bart,

    yes, it worked once upon a time. :) I cannot say in which version. I discussed this issue on codegarden with the core-developer of umbraco. We ended up with the solution above for version 8. In 9 and 10 it should be fixed, as far as I know.

    Kind regards -

    Tom

  • Bart 7 posts 88 karma points
    Nov 02, 2022 @ 15:52
    Bart
    1

    Thanks Thomas,

    I still have no idea why it isn't working on our v9 installation. But I managed to create a solution using the TreeNodesRenderingNotifcation (events aren't supported in v9 anymore) based on your code. I'll just share my code if anyone else runs into this problem.

    public class TreeNotificationHandler : INotificationHandler<TreeNodesRenderingNotification>
    {
        private readonly IContentService _contentService;
    
        public TreeNotificationHandler(IContentService contentService)
        {
            _contentService = contentService;
        }
    
        public void Handle(TreeNodesRenderingNotification notification)
        {
            if (notification.TreeAlias == "content")
            {
                List<int> nodeCollection = notification.Nodes.Select(node => int.Parse(node.Id.ToString() ?? string.Empty)).ToList();
    
                foreach (var n in _contentService.GetByIds(nodeCollection))
                {
                    if (n != null && n.PublishedState == PublishedState.Unpublished)
                    {
                        notification.Nodes.FirstOrDefault(x => x.Id.ToString() == n.Id.ToString())?.CssClasses.Add("not-published");
                    }
                }
    
            }
        }
    }
    

    Sources:

Please Sign in or register to post replies

Write your reply to:

Draft