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!
Files are different icons depending on the binary mime type.
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:
Icons are controlled by a 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.
Icons
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 /img/
folder.
Use Studio resources to upload the icons
Color Metadata
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:
Create a color schema with a String color metadata
Assign the color schema to the Folder doc type
Create a vocabulary to hold the possible color values
Display the Color metadata in the Folder layouts using a vocabulary
Automation Scripting
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!
Automation Scripting is one of the new features that was introduced in the Nuxeo Platform 7.2 release. It adds all the possibilities of JavaScript logic to Automation. You can check out the Documentation: Automation Scripting documentation. While creating a simple JavaScript, you can call the Automation operations directly. So you have the power of Automation to interact with the server and you can also wrap some advanced logic around it.
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.
Full logic made possible for Automation, thanks to Automation Scripting
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.
Autocompletion in Automation Scripting
Event Handler
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 create
and before document modification
.
Event handler on about to create and before document modification.
Enjoy!
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.