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:
This would present itself in the actual dialog for a Title component in the following way:
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:
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.
Exactly what we want….!