How can I copy a page to a given path, if the parent of the target destination does not exist yet?

StackOverflow https://stackoverflow.com/questions/18382467

  •  26-06-2022
  •  | 
  •  

Question

I want to copy a page to a target path as follows:

Page page = "pagePath";  
PageManager pageManager = getResourceResolver().adaptTo(PageManager.class);
pageManager.copy(pagePath, pagePath + "/target/newPage", null, true, false);

This works fine, if the page "target" exists (i.e. the would-be parent of the new copy). If this page doesn't exist however, PageManager will throw an Exception.

How can I tell the PageManager that it should create target if it doesn't exist already? (I.e. similar to the -p flag on the unix mkdir programme.)

Was it helpful?

Solution

How would PageManager know what content to use to create "target"?

Why not just check if target exists & create it yourself if not, with whichever template you want to use?

Page page = "pagePath";  
ResourceResolver resourceResolver = getResourceResolver();
Resource parent = resourceResolver.resolve(pagePath + "/target");
PageManager pageManager = resourceResolver.adaptTo(PageManager.class);

if (parent.getResourceType().equals(Resource.RESOURCE_TYPE_NON_EXISTING) {
    pageManager.create(pagePath, "target", "SOME_TEMPLATE_NAME", "SOME_TITLE");
}

pageManager.copy(pagePath, pagePath + "/target/newPage", null, true, false);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top