How to animate tree icons in Umbraco v8

Posted written by Paul Seal on November 14, 2019 Umbraco

This blog post will show you how to do it yourself.

Add the Alt Text property

In the Settings section of Umbraco, go to Media Types > Images and add a new textstring property called 'Alt Text' with the alias 'altText'

Create a component to hook into the Tree Nodes Rendering event.

using Umbraco.Core; using Umbraco.Core.Composing; using Umbraco.Web; using Umbraco.Web.Trees;

namespace Talk.Core.Composing
{

    [RuntimeLevel(MinLevel = RuntimeLevel.Run)]
    public class TreeNodeRenderingComposer : ComponentComposer<TreeNodeRenderingComponent>, IUserComposer
    { }

    public class TreeNodeRenderingComponent : IComponent
    {
        private readonly IUmbracoContextAccessor _umbracoContextAccessor;

        public TreeNodeRenderingComponent(IUmbracoContextAccessor umbracoContextAccessor)
        {
            _umbracoContextAccessor = umbracoContextAccessor;
        }

        public void Initialize()
        {
            TreeControllerBase.TreeNodesRendering += TreeControllerBase_TreeNodesRendering;
        }

        public void Terminate()
        { }

        private void TreeControllerBase_TreeNodesRendering(TreeControllerBase sender, TreeNodesRenderingEventArgs e)
        {
            if (sender.TreeAlias == "media")
            {
                foreach (var node in e.Nodes)
                {
                    if (node.NodeType == "media")
                    {
                        if (int.TryParse(node.Id.ToString(), out var nodeId) && nodeId > 0)
                        {
                            var mediaItem = _umbracoContextAccessor.UmbracoContext.Media.GetById(nodeId);
                            if (mediaItem != null)
                            {
                                if (mediaItem.ContentType.Alias != "Folder" && (!mediaItem.HasValue("altText")
                                    || string.IsNullOrWhiteSpace(mediaItem.Value<string>("altText"))))
                                {
                                    node.CssClasses.Add("alt-text-missing");
                                }
                            }
                            else
                            {
                                var contentService = Current.Services.MediaService;
                                var mediaItemFromService = contentService.GetById(nodeId);
                                if (mediaItemFromService != null && mediaItemFromService.ContentType.Alias == "Folder"
                                    && (!mediaItemFromService.HasProperty("altText")
                                    || string.IsNullOrWhiteSpace(mediaItemFromService.GetValue<string>("altText"))))
                                {
                                    node.CssClasses.Add("alt-text-missing");
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

This will add class to any image which doesn't have the 'altText' value filled in.

Create a CustomStyles folder in App_Plugins

This will allow you to change the style of the backoffice.

Add a package.manifest file to this CustomStyles folder with these contents:

{
    "css": [
        "~/App_Plugins/CustomStyles/css/customstyles.css"
    ]
}

In this folder, add a css folder and in it create a file named customstyles.css

Put this in the css file:

li.alt-text-missing i.umb-tree-icon {
    color: red;
    animation: infinite-spinning 2s infinite;
}

@keyframes infinite-spinning {
    from {
        transform: rotate(0deg);
    }
    to {
        transform: rotate(360deg);
    }
}

View the changes

That should be it. To view the changes, make sure you are running in debug mode in your local environment, or increment the Client Dependency version. Reload the page using Ctrl + F5 to empty the cache and hard reload.