Skip to content

Instantly share code, notes, and snippets.

@sotirisf
Created December 19, 2020 16:01
Show Gist options
  • Save sotirisf/952a319374c67820201255df387b0290 to your computer and use it in GitHub Desktop.
Save sotirisf/952a319374c67820201255df387b0290 to your computer and use it in GitHub Desktop.
Hide upublished variants 1
using System.Configuration;
using System.Linq;
using Umbraco.Core;
using Umbraco.Core.Composing;
using Umbraco.Web;
using Umbraco.Web.Trees;
namespace Site.Core
{
public class SiteEvents : IUserComposer
{
public void Compose(Composition composition)
{
composition.Components().Append<SubscribeToEvents>();
}
}
public class SubscribeToEvents : IComponent
{
private readonly IUmbracoContextFactory _context;
public SubscribeToEvents(IUmbracoContextFactory context)
{
_context = context;
}
public void Initialize()
{
TreeControllerBase.MenuRendering += TreeControllerBase_MenuRendering;
}
public void Terminate()
{
}
private void TreeControllerBase_MenuRendering(TreeControllerBase sender, MenuRenderingEventArgs e)
{
if (sender.TreeAlias == "content" && (ConfigurationManager.AppSettings.Get("HideUnpublishedVariantsFromTree") ?? "false") == "true")
{
using (var cref = _context.EnsureUmbracoContext())
{
var cache = cref.UmbracoContext.Content;
if (string.IsNullOrEmpty(e.NodeId) || e.NodeId == "-1" || e.NodeId.Equals(Constants.System.RecycleBinContentString)) { return; }
var node = cache.GetById(contentId: int.Parse(e.NodeId));
if (node.Level == 1)
{
var toggleMenuItem = new Umbraco.Web.Models.Trees.MenuItem("toggleMlNodes", "Toggle not created variants");
toggleMenuItem.Icon = "axis-rotation";
toggleMenuItem.SeparatorBefore = true;
toggleMenuItem.ExecuteJsMethod("HideShowTreeNodes()");
e.Menu.Items.Insert(e.Menu.Items.Count(), toggleMenuItem);
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment