How do we store multifield that has more than one field as array in AEM 6.5 as acs-commons-nested property is deprecated in 6.5?
Solved! Go to Solution.
Views
Replies
Total Likes
If you have a component that uses both Classic UI and Touch UI dialogs, and you want to ensure that the multifield data is synchronized between the two dialogs, you will need to implement some custom logic.
Here's an approach you can take to handle this scenario:
1. In your Classic UI dialog, add a hidden field to store the multifield data as JSON.
<mycomponent
jcr:primaryType="cq:Widget"
xtype="dialog">
<items jcr:primaryType="cq:WidgetCollection">
<!-- Other fields -->
<myMultifieldData
jcr:primaryType="cq:Widget"
xtype="hidden"
name="./myMultifieldData"/>
</items>
</mycomponent>
2. In your Classic UI JSP, retrieve the multifield data from the hidden field and convert it to a JSON object.
<%
String multifieldData = properties.get("myMultifieldData", "");
JSONArray jsonArray = new JSONArray(multifieldData);
%>
3. Iterate over the JSON array and render the multifield data in your Classic UI JSP.
<%
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
String field1Value = jsonObject.getString("field1");
String field2Value = jsonObject.getString("field2");
// Render the fields as needed
}
%>
4. In your Touch UI dialog, use the Coral 3 multifield component as described in the previous answer.
5. In your Touch UI dialog's client-side JS, populate the Classic UI hidden field with the multifield data.
(function($, $document) {
"use strict";
$document.on("dialog-ready", function() {
var $multifield = $("coral-multifield[name='./myMultifield']");
$multifield.on("change", function() {
var multifieldData = [];
$multifield.find("coral-multifield-item").each(function() {
var $item = $(this);
var field1Value = $item.find("coral-autocomplete[name='./field1']").val();
var field2Value = $item.find("coral-textfield[name='./field2']").val();
multifieldData.push({
field1: field1Value,
field2: field2Value
});
});
// Store the multifield data in the Classic UI hidden field
$("input[name='./myMultifieldData']").val(JSON.stringify(multifieldData));
});
});
})(jQuery, jQuery(document));
By populating the Classic UI hidden field with the multifield data from the Touch UI dialog, you ensure that the data is synchronized between the two dialogs. This way, when the data is submitted from the Classic UI dialog, it will be rendered correctly.
Hi @chinmayis865517 ,
In AEM 6.5, the ACS Commons Nested Property approach for storing multifield data as an array is indeed deprecated. Instead, you can use Coral 3 Multifield for handling multifield data. Here's how you can implement it:
<myMultifield
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/coral/foundation/form/multifield"
fieldLabel="My Multifield"
name="./myMultifield">
<items jcr:primaryType="nt:unstructured">
<field
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/coral/foundation/form/textfield"
fieldLabel="Field 1"
name="./field1"/>
<field
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/coral/foundation/form/textarea"
fieldLabel="Field 2"
name="./field2"/>
<!-- Add more fields as needed -->
</items>
</myMultifield>
In your servlet or model class, you would then handle the multifield data as an array of objects and process it accordingly.
Thanks,
Madhur
Hi @Madhur-Madan ,
Thank you for your response.
The same component also uses classic dialog and the multifield gets stores as json here which is not in sync with touch ui dialog. In the existing JSP I have written the logic to retrieve the nodes created from touch ui, but when the data are submitted from classic ui dialog , they will not be rendered.
If you have a component that uses both Classic UI and Touch UI dialogs, and you want to ensure that the multifield data is synchronized between the two dialogs, you will need to implement some custom logic.
Here's an approach you can take to handle this scenario:
1. In your Classic UI dialog, add a hidden field to store the multifield data as JSON.
<mycomponent
jcr:primaryType="cq:Widget"
xtype="dialog">
<items jcr:primaryType="cq:WidgetCollection">
<!-- Other fields -->
<myMultifieldData
jcr:primaryType="cq:Widget"
xtype="hidden"
name="./myMultifieldData"/>
</items>
</mycomponent>
2. In your Classic UI JSP, retrieve the multifield data from the hidden field and convert it to a JSON object.
<%
String multifieldData = properties.get("myMultifieldData", "");
JSONArray jsonArray = new JSONArray(multifieldData);
%>
3. Iterate over the JSON array and render the multifield data in your Classic UI JSP.
<%
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
String field1Value = jsonObject.getString("field1");
String field2Value = jsonObject.getString("field2");
// Render the fields as needed
}
%>
4. In your Touch UI dialog, use the Coral 3 multifield component as described in the previous answer.
5. In your Touch UI dialog's client-side JS, populate the Classic UI hidden field with the multifield data.
(function($, $document) {
"use strict";
$document.on("dialog-ready", function() {
var $multifield = $("coral-multifield[name='./myMultifield']");
$multifield.on("change", function() {
var multifieldData = [];
$multifield.find("coral-multifield-item").each(function() {
var $item = $(this);
var field1Value = $item.find("coral-autocomplete[name='./field1']").val();
var field2Value = $item.find("coral-textfield[name='./field2']").val();
multifieldData.push({
field1: field1Value,
field2: field2Value
});
});
// Store the multifield data in the Classic UI hidden field
$("input[name='./myMultifieldData']").val(JSON.stringify(multifieldData));
});
});
})(jQuery, jQuery(document));
By populating the Classic UI hidden field with the multifield data from the Touch UI dialog, you ensure that the data is synchronized between the two dialogs. This way, when the data is submitted from the Classic UI dialog, it will be rendered correctly.
@chinmayis865517 , Did you find the suggestions from users helpful? Please let us know if more information is required. Otherwise, please mark the answer as correct for posterity. If you have found out solution yourself, please share it with the community.
I'm rendering the content from the touch ui and pointing the classic dialog to touch , in case the author tries to open it from the classic ui.
@chinmayis865517 Did you find the suggestions from users helpful? Please let us know if more information is required. Otherwise, please mark the answer as correct for posterity. If you have found out solution yourself, please share it with the community.
Views
Replies
Total Likes
Views
Likes
Replies
Views
Likes
Replies
Views
Likes
Replies