Is there anything in the CQ API so you can create a node from the cq: component?

I am wondering if there is anything in the CQ API that will allow me to create a node based on cq:component, like what will happen when the author adds the component to parsys.

Since I needed to make a piece, I went ahead and did it manually. I included this solution to find out if anyone can go "about the person there. * A method that you can use to do just that."

This is what I do:

public static Node createFromComponent(Node dstParent, Node srcComponent, String targetName) {
    Node newNode = null;

    try {
        //if there is a template use it
        if (srcComponent.hasNode("cq:template")) {
            newNode = JcrUtil.copy(srcComponent.getNode("cq:template"), dstParent, targetName);
        }
        else {
            newNode = dstParent.addNode(targetName);
        }

        //set the resourceType to the path of the component sent over
        newNode.setProperty("sling:resourceType", srcComponent.getPath());

        newNode.getSession().save();
    }
    catch(Exception e) {
        LOGGER.error("Error creating node from component: ", e);
    }

    return newNode;
}

This is pretty straight forward. I watched the JcrUtilclass , but I do not think that he has what I am looking for.

+4
source share
2 answers

Unfortunately, there is no built-in method that:

  • cq:template node
  • node,
  • sling:resourceType node.

sidekick parsys, HTTP POST Sling. cq:template, HTTP- :

./@CopyFrom:/apps/my/component/cq:template

. API, HTTP-, .

+3

API Sling, JCR. cq:template node ValueMap, ResourceResolver.create()...

.

<%  
    Resource templateAsResource = resourceResolver.resolve("/path/to/cq:template");
    ValueMap templateProperties = templateAsResource.adaptTo(ValueMap.class);
    resourceResolver.create(parentResource, "newResource", templateProperties);
    resourceResolver.commit();
%>

- Author . ( Publish , , , , node, cq: template /apps)

+2

Source: https://habr.com/ru/post/1539348/


All Articles