Dear all,
Is there any way to extract in system level (or through out any script or tool) to have the components' text input limit information?
For exmaple, if a form component has 5 fields and each of them have different input limit, would like to have the result as:
Component Name: contact_us_form1
Field 1 input limit: 20 characters(varchar)
Field 2 input limit: 20 characters(varchar)
Field 3 input limit: 25 characters(varchar)
Field 4 input limit: 7 characters (numbers only)
Field 5 input limit: 200 characters (varchar)
Not just for one component but all components in the system that have text input function, I would like to extract.
(So the extraction should be able to pass or indicate if a component has image/video space)
Many thanks!
Solved! Go to Solution.
Views
Replies
Total Likes
Hi @nak1to ,
To systematically extract the text field input limitations of components in AEM, you can create a script that traverses the component definitions, inspects the dialog nodes for input field configurations, and extracts the relevant information. This process can be automated using Groovy, Sling Models, or any script that can interact with the JCR repository. Here, I'll provide an example using Groovy script with the ACS AEM Commons package, which provides a convenient way to run Groovy scripts in AEM.
Install ACS AEM Commons: Ensure that you have the ACS AEM Commons package installed on your AEM instance. This package provides the Groovy Console, which allows you to execute Groovy scripts within AEM.
Write the Groovy Script: The following Groovy script traverses all components under /apps and /libs, inspects their dialog definitions, and extracts the text field input limitations.
import org.apache.jackrabbit.commons.JcrUtils
import javax.jcr.Node
import javax.jcr.RepositoryException
import org.apache.sling.api.resource.Resource
import org.apache.sling.api.resource.ResourceResolver
import org.apache.sling.api.resource.ValueMap
// Function to traverse and inspect components
def extractTextFieldLimits(ResourceResolver resourceResolver, String path) {
def results = []
Resource rootResource = resourceResolver.getResource(path)
rootResource.listChildren().each { Resource componentResource ->
def componentNode = componentResource.adaptTo(Node.class)
if (componentNode != null) {
def dialogPath = "${componentResource.path}/dialog"
Resource dialogResource = resourceResolver.getResource(dialogPath)
if (dialogResource != null) {
dialogResource.listChildren().each { Resource tabResource ->
tabResource.listChildren().each { Resource fieldResource ->
def fieldProperties = fieldResource.adaptTo(ValueMap.class)
def fieldName = fieldResource.getName()
if (fieldProperties != null && fieldProperties.get("sling:resourceType") == "cq/gui/components/authoring/dialog/textfield") {
def inputLimit = fieldProperties.get("maxlength")
def fieldType = fieldProperties.get("fieldDescription")
if (inputLimit != null) {
results << [
component: componentResource.getPath(),
field: fieldName,
limit: inputLimit,
type: fieldType
]
}
}
}
}
}
}
}
return results
}
// Combine results from both /apps and /libs
def results = []
def resolver = resourceResolverFactory.getAdministrativeResourceResolver(null)
results.addAll(extractTextFieldLimits(resolver, "/apps"))
results.addAll(extractTextFieldLimits(resolver, "/libs"))
// Print results
results.each { result ->
println "Component: ${result.component}, Field: ${result.field}, Limit: ${result.limit}, Type: ${result.type}"
}
Run the Script:
You can customize the script to include more fields or different types of input limitations by modifying the conditions and properties being checked. Additionally, you can extend the script to export the results in a specific format, such as JSON or CSV, for easier analysis.
By using this approach, you can systematically extract and document the input limitations for all text fields in the components across your AEM instance.
Hi @nak1to ,
To systematically extract the text field input limitations of components in AEM, you can create a script that traverses the component definitions, inspects the dialog nodes for input field configurations, and extracts the relevant information. This process can be automated using Groovy, Sling Models, or any script that can interact with the JCR repository. Here, I'll provide an example using Groovy script with the ACS AEM Commons package, which provides a convenient way to run Groovy scripts in AEM.
Install ACS AEM Commons: Ensure that you have the ACS AEM Commons package installed on your AEM instance. This package provides the Groovy Console, which allows you to execute Groovy scripts within AEM.
Write the Groovy Script: The following Groovy script traverses all components under /apps and /libs, inspects their dialog definitions, and extracts the text field input limitations.
import org.apache.jackrabbit.commons.JcrUtils
import javax.jcr.Node
import javax.jcr.RepositoryException
import org.apache.sling.api.resource.Resource
import org.apache.sling.api.resource.ResourceResolver
import org.apache.sling.api.resource.ValueMap
// Function to traverse and inspect components
def extractTextFieldLimits(ResourceResolver resourceResolver, String path) {
def results = []
Resource rootResource = resourceResolver.getResource(path)
rootResource.listChildren().each { Resource componentResource ->
def componentNode = componentResource.adaptTo(Node.class)
if (componentNode != null) {
def dialogPath = "${componentResource.path}/dialog"
Resource dialogResource = resourceResolver.getResource(dialogPath)
if (dialogResource != null) {
dialogResource.listChildren().each { Resource tabResource ->
tabResource.listChildren().each { Resource fieldResource ->
def fieldProperties = fieldResource.adaptTo(ValueMap.class)
def fieldName = fieldResource.getName()
if (fieldProperties != null && fieldProperties.get("sling:resourceType") == "cq/gui/components/authoring/dialog/textfield") {
def inputLimit = fieldProperties.get("maxlength")
def fieldType = fieldProperties.get("fieldDescription")
if (inputLimit != null) {
results << [
component: componentResource.getPath(),
field: fieldName,
limit: inputLimit,
type: fieldType
]
}
}
}
}
}
}
}
return results
}
// Combine results from both /apps and /libs
def results = []
def resolver = resourceResolverFactory.getAdministrativeResourceResolver(null)
results.addAll(extractTextFieldLimits(resolver, "/apps"))
results.addAll(extractTextFieldLimits(resolver, "/libs"))
// Print results
results.each { result ->
println "Component: ${result.component}, Field: ${result.field}, Limit: ${result.limit}, Type: ${result.type}"
}
Run the Script:
You can customize the script to include more fields or different types of input limitations by modifying the conditions and properties being checked. Additionally, you can extend the script to export the results in a specific format, such as JSON or CSV, for easier analysis.
By using this approach, you can systematically extract and document the input limitations for all text fields in the components across your AEM instance.
@nak1to Did you find the suggestions from users helpful? Please let us know if you require more information. Otherwise, please mark the answer as correct for posterity. If you have found out solution yourself, please share it with the community.
Views
Replies
Total Likes
If it is for aem sites , u can set maxLength of type Long property to text field to limit input characters