Expand my Community achievements bar.

SOLVED

Is there any automated/systematic way to extract components' text field input limitation?

Avatar

Level 1

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!

1 Accepted Solution

Avatar

Correct answer by
Level 10

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.

Steps to Extract Text Field Input Limitations

  1. 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.

  2. 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.

    groovy
     

 

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}"
}

 

  1. Run the Script:

    • Open the Groovy Console in AEM: http://<AEM-HOST>:<PORT>/etc/groovyconsole.html
    • Paste the above script into the console and execute it.

Explanation

  • Resource Traversal: The script starts at the root of the component definitions (/apps and /libs) and recursively traverses all child nodes.
  • Dialog Inspection: For each component, it looks for a dialog node and inspects each field within the dialog.
  • Field Extraction: It checks if the field is a text field (cq/gui/components/authoring/dialog/textfield) and extracts the maxlength property, which defines the input limit.
  • Result Compilation: The script compiles the results into a list and prints the component path, field name, input limit, and type.

Customization

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.

View solution in original post

4 Replies

Avatar

Community Advisor

Hi @nak1to 
Is this related to AEM Sites or AEM forms?

In AEM sites, you can try groovy script to extract jcr node property and input length. 



Arun Patidar

Avatar

Correct answer by
Level 10

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.

Steps to Extract Text Field Input Limitations

  1. 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.

  2. 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.

    groovy
     

 

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}"
}

 

  1. Run the Script:

    • Open the Groovy Console in AEM: http://<AEM-HOST>:<PORT>/etc/groovyconsole.html
    • Paste the above script into the console and execute it.

Explanation

  • Resource Traversal: The script starts at the root of the component definitions (/apps and /libs) and recursively traverses all child nodes.
  • Dialog Inspection: For each component, it looks for a dialog node and inspects each field within the dialog.
  • Field Extraction: It checks if the field is a text field (cq/gui/components/authoring/dialog/textfield) and extracts the maxlength property, which defines the input limit.
  • Result Compilation: The script compiles the results into a list and prints the component path, field name, input limit, and type.

Customization

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.

Avatar

Administrator

@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.



Kautuk Sahni

Avatar

Level 6

If it is for aem sites , u can set maxLength of type Long property to text field to limit input characters 

maxlength="{Long}50"