Expand my Community achievements bar.

SOLVED

Set AEM dialog property as page property

Avatar

Level 5

Hi all,

 

i have a requirement wherein I want to set the value authored in dialog as a page property.

What is the best way to do it?

 

1. Read component/resource's properties in value map and set it on the currentPage's valuemap in sling model.

2. Is there any way to do it directly while configuring the dialog xml?

 

thanks

@Imran__Khan@EstebanBustamante@arunpatidar@aanchal-sikka 

 

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

@aem_noob 

 

If the requirement is to share the value between page dialog and component dialog, then in component dialog you can have the property as 'abc', while in page '<crx_path_to_component>/abc'

 

Need some clarity on whats the use-case for which you would copy property from component to page. Might be able to provide suggestions accordingly.

 


Aanchal Sikka

View solution in original post

10 Replies

Avatar

Community Advisor

@aem_noob 

 

Is the component present on the page at a constant location? Basically is the path of the component to the page constant.


Aanchal Sikka

Avatar

Level 5

yes the page will have only this one component so it will be relatively fixed always

Avatar

Correct answer by
Community Advisor

@aem_noob 

 

If the requirement is to share the value between page dialog and component dialog, then in component dialog you can have the property as 'abc', while in page '<crx_path_to_component>/abc'

 

Need some clarity on whats the use-case for which you would copy property from component to page. Might be able to provide suggestions accordingly.

 


Aanchal Sikka

Avatar

Community Advisor

Hi @aem_noob ,

Let's explore the two options you mentioned:

1.Using Sling Model to Read Dialog Value and Set Page Property:
In this approach, you would create a Sling Model associated with your component. Within the Sling Model, you can inject the dialog's values using the @ValueMapValue annotation and then set these values as properties of the current page's ValueMap. This approach is flexible and allows you to manipulate the data before setting it as a page property if necessary.

 

2.Configuring Dialog XML to Set Page Property Directly:
Directly configuring the dialog XML to set a page property might not be a conventional approach, as dialogs typically interact with the component's data rather than the page's data. However, if you have a specific requirement to set a page property based on a dialog value, you might need to implement a custom handler or listener that intercepts the dialog submission and performs the desired action of setting the page property accordingly. This approach might involve more complexity and customization.

 

In most cases, the first approach using a Sling Model is preferred because it follows the separation of concerns principle and provides better maintainability and flexibility.

Thanks,
Madhur

Avatar

Community Advisor

Hi @aem_noob 
As @aanchal-sikka  mentioned read is directly from the component path(in case component path is fixed).

 

The other way is to update page property on dialog submit using sling servlet

 

Example

 

Dialog field

<pageTitle
    jcr:primaryType="nt:unstructured"
    sling:resourceType="granite/ui/components/coral/foundation/form/textfield"
    fieldLabel="Custom Property"
    name="./dialogPropName"
    />



Servlet

@SlingServlet(
    paths = "/bin/updatePageProperty",
    methods = HttpMethod.POST
)
public class UpdatePagePropertyServlet extends SlingAllMethodsServlet {
    @Override
    protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) {
        try {
            ResourceResolver resolver = request.getResourceResolver();
            String pagePath = request.getParameter("pagePath");
            String propertyValue = request.getParameter("propertyValue");

            // Get the page resource
            Resource pageResource = resolver.getResource(pagePath);
            if (pageResource != null) {
                ModifiableValueMap properties = pageResource.adaptTo(ModifiableValueMap.class);
                if (properties != null) {
                    // Set the page title property
                    properties.put("propertyName", propertyValue);
                    resolver.commit();
                    response.getWriter().println("Page title updated successfully.");
                } else {
                    response.getWriter().println("Failed to update page title: Resource is not modifiable.");
                }
            } else {
                response.getWriter().println("Failed to update page title: Page resource not found.");
            }
        } catch (IOException e) {
            // Handle IOException
        }
    }
}

 



Arun Patidar

Avatar

Community Advisor

Why can’t you put that value in page property instead of component dialog?

Avatar

Level 5

Because the content authors are authoring only the component while editing page property is restricted for them.

Avatar

Community Advisor

Hi,

 

I came late to the game, but this looks like the wrong design/approach to follow. I would recommend revisiting what you are trying to do and managing this directly in a page properties. Perhaps it's better to add a new option in the page properties and show it to a specific group of users than what you are trying to achieve. However, if I were forced to do what you are required to, I would just use a Sling model to build the path from the resource to the page and commit the properties.

 

Hope this helps.



Esteban Bustamante

Avatar

Level 5

Yes coupling component property with page property is not exactly an ideal scenario. We are looking to decouple them going forwards but for now still wanted to bind them anyways. Thanks!

Avatar

Administrator

@aem_noob Did you find the suggestions from users helpful? Please let us know if more information is required. Otherwise, please mark the answer as correct for posterity. If you have found out solution yourself, please share it with the community.



Kautuk Sahni