Copied to clipboard

Flag this post as spam?

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


  • Johannes Lantz 156 posts 839 karma points c-trib
    Feb 27, 2022 @ 11:43
    Johannes Lantz
    0

    Remove an already added custom tree

    Hi!

    I was wondering if there is a way to remove an custom tree once it's already added. I found that you can remove a already added dashboard by doing this:

        public void Compose(IUmbracoBuilder builder)
        {
            builder.Dashboards().Remove<Dashboard>();
        }
    

    So I was wondering if there is something similar you can do for a tree?

    I tried using "Notifications". And it "kinda" works. The issue I am having is, when I try to add a child to a child. The children shows up on the same level instead of under each other. If I do it in a TreeController it works fine.

    An idea I have is to simple use "Notifications" to add a css class to the already added tree and hide it. It works but it feels a bit janky.

    Any input is appreciated!

    //Johannes

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Feb 28, 2022 @ 02:23
    Lee Kelleher
    0

    Hi Johannes,

    While there isn't an official API to remove a tree, I do have code that uses Reflection to do it.

    e.g. https://github.com/leekelleher/umbraco-contentment/blob/3.1.0/src/Umbraco.Community.Contentment/Core/Trees/TreeCollectionExtensions.cs#L17

    With that extension method, you should be able to call it with the .Trees() method.

    e.g. https://github.com/leekelleher/umbraco-contentment/blob/3.1.0/src/Umbraco.Community.Contentment/Trees/CompositionExtensions.cs#L20

    I hope this helps?

    Cheers,
    - Lee

  • Bjarne Fyrstenborg 1285 posts 4039 karma points MVP 8x c-trib
    Feb 28, 2022 @ 06:29
    Bjarne Fyrstenborg
    101

    Hi Johannes

    Since v9.1.0 it has been possible to remove a tree using RemoveTreeController: https://github.com/umbraco/Umbraco-CMS/pull/11250

    public class TreeComposer : IComposer
    {
        public void Compose(IUmbracoBuilder builder)
        {
            builder.Trees().RemoveTreeController<DictionaryTreeController>();
        }
    }
    
  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Feb 28, 2022 @ 07:56
    Lee Kelleher
    1

    I had no idea that this had been put into Umbraco core! 😆 I'm happy that I can remove that extension method from my Contentment package in a future release. 😁

  • Bjarne Fyrstenborg 1285 posts 4039 karma points MVP 8x c-trib
    Feb 28, 2022 @ 08:52
    Bjarne Fyrstenborg
    1

    Yeah, I have used it in a project to remove Content Templates tree from settings section and added it to content section instead.

    Removing the core tree:

    public class TreeComposer : IComposer
    {
        public void Compose(IUmbracoBuilder builder)
        {
            // Remove default Content Templates tree - this is added in content section in ContentTemplatesController.
            builder.Trees().RemoveTreeController<ContentBlueprintTreeController>();
         }
     }
    

    and then registering a custom tree inheriting from ContentBlueprintTreeController:

    [Authorize(Policy = AuthorizationPolicies.SectionAccessContent)]
    [Tree(Constants.Applications.Content, Constants.Trees.ContentBlueprints, SortOrder = 10)] //TreeGroup = Constants.Trees.Groups.Settings
    [PluginController(Constants.Web.Mvc.BackOfficeTreeArea)]
    [CoreTree]
    public class ContentTemplatesController : ContentBlueprintTreeController
    {
        public ContentTemplatesController(
            ILocalizedTextService localizedTextService,
            UmbracoApiControllerTypeCollection umbracoApiControllerTypeCollection,
            IMenuItemCollectionFactory menuItemCollectionFactory,
            IContentService contentService,
            IContentTypeService contentTypeService,
            IEntityService entityService,
            IEventAggregator eventAggregator) 
            : base(localizedTextService, umbracoApiControllerTypeCollection, menuItemCollectionFactory, 
                  contentService, contentTypeService, entityService, eventAggregator)
        {
    
        }
    
        protected override ActionResult<TreeNode> CreateRootNode(FormCollection queryStrings)
        {
            var root = base.CreateRootNode(queryStrings);
    
            if (!string.IsNullOrEmpty(root.Value.RoutePath))
            {
                // Update route to content section
                root.Value.RoutePath = root.Value.RoutePath.Replace(Constants.Applications.Settings, Constants.Applications.Content);
            }
    
            return root;
        }
    
        protected override ActionResult<MenuItemCollection> GetMenuForNode(string id, FormCollection queryStrings)
        {
            var collection = base.GetMenuForNode(id, queryStrings);
    
            if (collection.Value.Items.Count > 0)
            {
                foreach (var menuItem in collection.Value.Items)
                {
                    if (menuItem.Action is ActionCreateBlueprintFromContent)
                    {
                        var route = menuItem.AdditionalData["actionRoute"]?.ToString();
    
                        if (!string.IsNullOrEmpty(route))
                        {
                            // Update route to content section
                            menuItem.NavigateToRoute(route.Replace($"/{Constants.Applications.Settings}/", $"/{Constants.Applications.Content}/"));
                        }
                    }
                }
            }
    
            return collection;
        }
    
    }
    
  • Johannes Lantz 156 posts 839 karma points c-trib
    Feb 28, 2022 @ 13:12
    Johannes Lantz
    1

    Thank you so much Bjarne & Lee! You are life savers!

Please Sign in or register to post replies

Write your reply to:

Draft