Component Cleanup BEFORE Submit | Community
Skip to main content
Level 4
January 26, 2017
Solved

Component Cleanup BEFORE Submit

  • January 26, 2017
  • 4 replies
  • 1764 views

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

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by deana66659071

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();
                 }
                }
         }
        }
 

4 replies

smacdonald2008
Level 10
January 26, 2017

What are you using for front end - HTL ?

Level 4
January 26, 2017

Yes, we are using html & sling (HTL)

deana66659071AuthorAccepted solution
Level 4
January 26, 2017

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();
                 }
                }
         }
        }
 

smacdonald2008
Level 10
January 26, 2017

Nice logic - thanks for sharing. This would make a nice article.