We have a component that allows our authors to dynamically select the number of columns and the associated widths. Example 12, 7-5, 6-6, 4-4-4, 3-5-4, etc
Once they have selected this click save, they can add components into each of the corresponding parsys.
If the user changes their selection from 4-4-4 (3 columns) to 12 (1 column), nothing "cleans up" the orphaned nodes.
Desired Result Example:
4-4-4 - 3 Columns
columnControl
col0
componenta
col1
componentb
col2
component
12 - 1 column
columnControl
col0
componenta
Unfortunately, this is the actual result .... meaning col1 & col2 are orphans and NOT removed from the parent nodes.
Actual Result Example:
12 - 1 Column
columnControl
col0
componenta
col1
componentb
col2
componentc
Is there something I can do to remove the orphaned content BEFORE the new structure is saved?
Thank you for your assistance.
-Dean
Solved! Go to Solution.
Views
Replies
Total Likes
Here is what I added in the activate() method to resolve:
Node currentNode = getResource().adaptTo(Node.class);
NodeIterator ni = currentNode.getNodes();
if (columns != null) {
int columnsSize = columns.size();
int nodeCounter = 0;
boolean changesMade = false;
if (ni != null) {
while (ni.hasNext()) {
Node childNode = ni.nextNode();
if (childNode != null) {
String childNodeName = childNode.getName();
if (childNodeName != null && childNodeName.startsWith("abc")) {
nodeCounter++;
if (nodeCounter > columnsSize) {
childNode.remove();
changesMade = true;
}
}
}
}
if (changesMade) {
Session session = currentNode.getSession();
if (session != null) {
session.save();
}
}
}
}
Views
Replies
Total Likes
What are you using for front end - HTL ?
Views
Replies
Total Likes
Yes, we are using html & sling (HTL)
Views
Replies
Total Likes
Here is what I added in the activate() method to resolve:
Node currentNode = getResource().adaptTo(Node.class);
NodeIterator ni = currentNode.getNodes();
if (columns != null) {
int columnsSize = columns.size();
int nodeCounter = 0;
boolean changesMade = false;
if (ni != null) {
while (ni.hasNext()) {
Node childNode = ni.nextNode();
if (childNode != null) {
String childNodeName = childNode.getName();
if (childNodeName != null && childNodeName.startsWith("abc")) {
nodeCounter++;
if (nodeCounter > columnsSize) {
childNode.remove();
changesMade = true;
}
}
}
}
if (changesMade) {
Session session = currentNode.getSession();
if (session != null) {
session.save();
}
}
}
}
Views
Replies
Total Likes
Nice logic - thanks for sharing. This would make a nice article.
Views
Replies
Total Likes
Views
Likes
Replies
Views
Likes
Replies