How to update a contentlet with vtl

30 views
Skip to first unread message

Jon Ku

unread,
Aug 14, 2024, 11:15:22 AMAug 14
to dotCMS User Group
We plan to use a Category based taxonomy for our site. The native UI is more friendly for this, rather than my original idea to use relationships. Categories have a native parent and child relationship and the business prefers the native Category UI to the native relationships UI.

We have a business requirement to support legacy URLs of the type
/?contentType/id=1234&taxonomy=5678 so content and taxonomy need to have a non-native id as well as dotCMS identifiers.

The goal is to have a simple way to generate breadcrumbs and menus based on a relatively fixed taxonomy, which can be stored in a variable somewhere and referenced on each page. Content will be related to one or more nodes in the taxonomy.

I have a content type called ApplicationScope. 
[ note: I had hoped to use a site variable to store this, but that seems problematic. The code will be the same or similar in either case.]

The idea is to cache a json-ish object in a text field of a single instance of the content type, and when the taxonomy is edited, update various pre-generated values (breadcrumb for example) within the structure.

A very similar structure can be consumed by a Dojo tree control to create a hierarchical folder UI element to view and edit the taxonomy within the back end, in addition to the Category edit function.

The category edit function does not seem able to change the parent of a category but there are ways to do that with script I believe. We will need to initially import a taxonomy (1,000 nodes) via API or other method and create each with its appropriate parent.

At any rate, the problem I've run into is this. Although I can read the current value of my contentlet's taxObject field, I cannot update it.

Reading the value is okay:

#set($item = $dotcontent.load(*inode*))
#set($taxonomyObject = $item.taxObject)

This gets me the taxonomy object that I can work with to find my current taxonomy and its breadcrumb. At the bottom of this post is an example of a taxonomy object.

But when we update the taxonomy by adding a node or moving those around with the tree control, how can I save the new structure?

Another question of course is how to script the import and editing of Categories, since the export/import function doesn't support the parent property.

Any hints are welcome, thanks.

- Jon

Here's an example of the taxonomy object that can be queried by the key of the taxonomy you want. This is also a structure very similar to one that can be consumed by the Dojo tree control to generate the folder UI element.

id = dotCMS key field for the category

Since categories don't support language, we plan to use the keywords field to store the French (or other future language) names.

This is generated with a recursive macro using 
$categories.getChildrenCategoriesByKey( $catKey ).

$taxObject = {
'tx-root': { name: 'Taxonomy', 'id': 'tx-root', 'parentName': 'none', 'parent': '', 'inode': 'unknown', 'breadcrumbKey': '[]', 'breadcrumbName': '[]' },

'consumerMobility': { name: 'Consumer Mobility', id: 'consumerMobility', parentName: 'Taxonomy', parent: 'tx-root', inode: '53b7acde91b86c58fca0bc39475247d0', breadcrumbKey: '[tx-root]', breadcrumbName: '[Taxonomy]' },

{ name: 'Home Solutions', id: 'homeSolutions', parentName: 'Taxonomy', parent: 'tx-root', inode: '3815eaa994283b7b6e5829169b588f6a', breadcrumbKey: '[tx-root]', breadcrumbName: '[Taxonomy]' },

'tx13859': { name: 'News', id: 'tx13859', parentName: 'Home Solutions', parent: 'homeSolutions', inode: '8c3aa5cbb4c86f9f9daee5c47d02e92a', breadcrumbKey: '[tx-root, homeSolutions]', breadcrumbName: '[Taxonomy, Home Solutions]' },

'tx13855': { name: 'Daily Topics', id: 'tx13855', parentName: 'News', parent: 'tx13859', inode: '06d15052696c053f9e35bd8bbdd51223', breadcrumbKey: '[tx-root, homeSolutions, tx13859]', breadcrumbName: '[Taxonomy, Home Solutions, News]' },

'tx14927': { name: 'SmartHome', id: 'tx14927', parentName: 'News', parent: 'tx13859', inode: '734cb7a4b5c46bd94348a405fbc316fa', breadcrumbKey: '[tx-root, homeSolutions, tx13859]', breadcrumbName: '[Taxonomy, Home Solutions, News]' },

'tx14913': { name: 'Quebec', id: 'tx14913', parentName: 'News', parent: 'tx13859', inode: '322c5ca4a6121baa62b812d5f034f42b', breadcrumbKey: '[tx-root, homeSolutions, tx13859]', breadcrumbName: '[Taxonomy, Home Solutions, News]' },

'tx13876': { name: 'Products & pricing', id: 'tx13876', parentName: 'Home Solutions', parent: 'homeSolutions', inode: '4f8fdd1ae28e2cec04a585ed90c1a6d1', breadcrumbKey: '[tx-root, homeSolutions]', breadcrumbName: '[Taxonomy, Home Solutions]' } ... 1,000 nodes
}

Reply all
Reply to author
Forward
0 new messages