Beyond String – @TypeHint to the Rescue

Based on some questions from a partner I had to investigate whether it was possible to store values for widgets you define as part of your CQ5 custom component as something else than String. Guess what…. it’s possible (not that I was surprised…).

Storing JCR content with a specific type is possible using the @TypeHint property, as is documented as part of the Apache SlingPostServlet (more specifically in the section on Controlling Content Updates with @ Suffixes).

However, how can @TypeHint be ‘reused’ such that also the properties of a custom component can be saved as something else than String? With some help of engineering, follows a tutorial…

For illustration purposes, and definitely not making any sense in real life, let’s add a numeric value to the Title component’s dialog in the Geometrixx app in CRXDE as shown below:

numValue widget and properties

This would present itself in the actual dialog for a Title component in the following way:

Title Component dialog

Although we have specified the numValue as a widget (xtype) of numberfield, which effectively means the field will automatically provide the necessary numeric validation, the value itself will be stored as a String in the CRX repository.

As said, @TypeHint comes to the rescue. Add an additional widget to the component’s dialog (e.g.. named numValueHint) and specify the following properties:

numValueHint widget and properties

Of particular interest are:

  • name – specified as ./numValue@TypeHint; by¬†using the specific @TypeHint suffix, we actually instruct to take the value from the original property named ./numValue.
  • xtype – set to hidden as we don’t want this widget to appear in the component’s dialog.
  • value – to specify that we want to store the property as a Decimal.
  • ignoreData – to ensure we ignore the original data (and type) of the numValue property.

As a result, whenever we specify a value in the numValue numberfield in the Title’s component dialog, the numValue property on the title node will be saved out in the CRX repository like shown below.

CRX title node and properties

Exactly what we want….!

VN:F [1.9.22_1171]
Was this helpful? Please rate the content.
Rating: 5.8/10 (5 votes cast)
Beyond String - @TypeHint to the Rescue, 5.8 out of 10 based on 5 ratings
This entry was posted in ADEP, Experience Services and tagged , , , . Bookmark the permalink.

One Response to Beyond String – @TypeHint to the Rescue

  1. Chetanya says:

    I was trying the similar approach for @ValueFrom, and this does not seem to work as expected. Do you have any clue on how it works?