The UTIL library is a library built and hosted by Enonic, which includes a set of utility functions which may speed up the module development process significantly.
The UTIL library should normally be included in your build step (automatically downloaded upon module build) if you intend to use it as-is, or you can download the source on Github.
Include this in your build.gradle file to automatically download it in your module build:
I have a proposal for a new utility function. How many times do you get a content and then have to get a related content? This function will get a content and then take a given related content reference and get that content too and add it to the first returned content. Letâs say you want to get a âprofileâ content. If the profile has a related âdepartmentâ content, then all you get is the id for the department. But if you want to show the name of the department on a profile-show page, then you have to do another content.get(). Now you would simply use:
UTIL.content.getWithRefs(profileKey, {department: âdepartmentContentâ})
Now the object returned is the exact same as it would be with content.get(), but it also has a new data element named departmentContent which holds the department content.
This only works when you know there can only be one related content, though it could be modified. I was also thinking of simply replacing the related id with the content instead of leaving the original reference.
Also check my pull request on GitHub when you have time. I exposed the getSubMenu function in UTIL.menu because that is extremely valuable when building more than one menu on a site. I Hacked that into the UTIL files now for my current project, so canât use gradles auto include for util lib.
I propose a change to the content.getPath(id); function. We made this function while working on enonic.com. I think there was a form and we wanted to save the created content in a specified folder if the part was configured, or else save the content under the page where the form was. But while working on other modules, I didnât want it to work that way. I set a module configuration to be the default, but the local part config could override that. I needed getPath() to return null if the id was null. The gist below shows a modified getPath() function that has an optional second parameter that (when âtrueâ) tells the function to return null if the id is null or the content doesnât exist.
By the way, the difference between id, path and key is that âkeyâ could be either id or path. You wouldnât pass a âpathâ to a getPath function.
The original getPath is on top and the one Iâm proposing is on the bottom. Itâs backward compatible so it wonât break any existing code that uses it.
Version 1.0.0 of the util lib has now been published (for XP 6.0), and is available as a dependency in Gradle, and on Github (see first post in this thread).
Some of the old functionality in Util has been moved into separate libs (menu and image)