0
def query = SQLQuery() <- my sql query
def result = query.execute()
result.nodes.each{node -> 
    node.setProperty(JcrConstants.JCR_TITLE, "new3")

I found all components with a certain resource type using groovy consoly, and I need to replace their titles with the titles of the pages in which this component is located. I need to use parent node named jcr:content

Instead of "new3" i can use node.getParent().getParent().getParent().getProperty("jcr:title"),but pages can have different structures. So, how to find parent node by name ?

toniedzwiedz
  • 17,895
  • 9
  • 86
  • 131
alex
  • 1
  • 2

1 Answers1

1

The idiomatic way to find the Page containing a resource would be via PageManager#getContainingPage.

Once you've got a Page object, you can read the relevant title. Keep in mind that there are a few options to choose from:

They can map to different dialog fields in the Page properties and certain components may use them differently, sometimes using fallbacks from one to another in case a value is absent. Same goes for different page-level components which may or may not allow authors to set a given property.

The exact use should be pretty clear if you use AEM Core Components but custom components may need a bit of investigation, especially if you're dealing with a long-lived codebase.

If none of the titles is a good fit, you could use the name of the page which should match its node name in the repository.

The PageManager itself should be available as a default binding in the Groovy console, under pageManager. Alternatively, it can be adapted from a ResourceResolver, e.g. if you need some custom permissions.

toniedzwiedz
  • 17,895
  • 9
  • 86
  • 131