Search code examples
aemday-cq

Pass data from one component to another in adobe CQ


I'm using Adobe CQ5. I have made two components that are independent of each other. However, I want to use data from one component in the other one. How do I achieve this? How can I make two components interact with each other?

Thanks in advance.


Solution

  • You can use javax.jcr.Node and javax.jcr.Property interface to get properties of another component. For example, you have added component1 and component2 to page1. In the repository you should have structure similar to this:

    /content
        /project
            /page1
                /jcr:content
                    /parsys
                        /component1
                            /...some properties
                        /component2
                            /...some properties
    

    If you want to get properties of component2 while in component1, you can use something like:

    Node parsys = currentNode.getParent();
    if(parsys.hasNode("component2")) {
        Node component2 = parsys.getNode("component2");
        if(component2.hasProperty("someProperty"))
          Property someProperty = component2.getProperty("someProperty");
    }