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");
}
}
}
}
}
}
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)
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.
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");
}
}
}
}
}
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:
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.
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
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.
Sources:
is working on a reply...