Change Icons Dynamically with Automation Scripting
In the Nuxeo Platform, document types have icons associated with them. This gives a nice look to document listings and to the left tree. But what if we could change that icon to give it more meaning, say a color for priority on folders? Wait a minute! That's already the case for Files - their icons change depending on the mime type of the attached binary file. So let's be inspired from that and do the same for Folders now!
Icon Path is a Metadata
It's actually pretty simple. On each document there is a String metadata called icon on the schema common. This metadata is the path of the icon on the server. Below is the same example as the one above but it’s now showing the icon metadata:
In the Nuxeo Platform in general and Nuxeo Studio in particular, it's pretty simple to modify metadata when a document is created or modified. We can use an event handler to trigger Automation Chains. If you are not familiar with the concept, you can learn about Events And Listeners, How to create an event handler with Studio and find additional How-To documents in our Documentation.
Ok, let's do this!
Remember our use case - we want to change the color of the Folder icons depending on some property. So we need:
- A bunch of colored icons
- A metadata field to store the desired color, or priority
- An event handler to update the Folders icon metadata on creation and modification
- The logic triggered by the event handler.
This cannot be simpler! In your Studio project, you have a Resources section where you can upload images which will then be available in the server in the
There could many ways to define how to choose the icon color. We'll will take a simple example here. We want a metadata field called ‘color’ and depending on its value we will change the icon path. Once again, our documentation describes in details what are schemas and metadata and how to define them in Studio.
What you should do is define a color schema with a String metadata field called color, assign that schema to a Folder, and expose the metadata on creation and edit layout of the Folder doc type. Maybe you can display it with a nice vocabulary for better understanding. The screenshots below show the steps:
Let's talk about the logic now. It shouldn’t be complicated. We want to set a metadata value depending on a known list of possible values of another metadata field and we also want a default value. It looks like a perfect case for a switch-case statement. This is where Automation Scripting comes in very handy!
Let's see how it goes for our use case. We fetch the color value of our input (the folder being created or modified) and then assign the path to a variable that we used to set the icon path metadata property of the document.
You noticed that you need to write the operation parameter. What if you don't know all Automation operation parameters by heart? No worries, we got you covered! Autocompletion is available in the Automation Scripting editor! Don't forget to check out our explorer to see the full list of operations in the Nuxeo Platform.
The next step is to capture the creation and modification events of folders to trigger our logic. So let's define an event handler. In reality, we want to capture modification and creation just before they happen. It's important for modification because it's the the way to avoid triggering another modification event that will be captured again by our event handler. So the events to select are:
about to createand
before document modification.
You can deploy your project and see the result in action! Here is a short video showing the result of creating a folder with a specific color and bulk editing the colors of several folders.
Category: Product & Development