Expand my Community achievements bar.

Guidelines for the Responsible Use of Generative AI in the Experience Cloud Community.
SOLVED

Update Page Properties with component data

Avatar

Employee Advisor

Is it possible to take information entered into a component field, and have it put into fields within the page properties?

We’d like to have a component where the page author could enter a title and description and have the values overwrite the page title and the page description as found in the page properties dialog.

1 Accepted Solution

Avatar

Correct answer by
Employee Advisor

Hi,

Please, don't overwrite page properties with properties from a specific component (by whatever means). Instead you should write your title component in a way, that it can display the correct value by reading it from that specific sub-component. In that case I would explicitly build logic into the title component Sling model, which looks for another component on that page and reads the correct title from there (preferably using the model class of that component instead of directly reading from the repo).

In general, I consider this not a good pattern. Because the title of the page is defined at a page level, and not on a component level. You should rethink if your properties are located at the right components.

HTH

View solution in original post

5 Replies

Avatar

Level 4

You can write custom code to iterate over your page node and update the pageproperties once you update your component dialog.

Avatar

Level 1

Yes, it is possible, but I'm not sure if a component would be the best way to perform this task.

For example, in order to write the custom code for updating the page properties, you would most likely create a Java Sling Model for the component.

In this Sling Model, you could get the current page, check if the title matches the value mapped from the configuration of the component, and if they don't match, update the page properties.

The reason why you likely wouldn't want to do this with a component is because that Sling Model will do that check every time it is rendered on a page, and you would basically only want to run that check once since it would just be an update that occurs once when the component is authored or edited.

Not sure what your exact use case is, but editing the page properties to have a different title isn't that difficult from an authoring standpoint, and I don't really see the ability of being able to update the page title via a component creating a better authoring experience.

Perhaps training your authors on to how to easily update the page properties to have a different title might be a better route than having to do a custom implementation on a component.

Hope this helps.

Avatar

Community Advisor

Hi,

You can use dialog value to update page property by writing a listener of dialog.

As soon as you submit the dialog, make a ajax call to the sling servlet or sling post servlet like curl with properties and update the nodes.

Below are the example/way how to do it, They are not the working code.

e.g. 1 Sling custom servlet

(function ($, $document) {

  "use strict";

  $(document).on("click", ".cq-dialog-submit", function (e) {

var dialogprop = $('#filedId');

var page = Granite.author.pageInfoHelper.json.status.path

$.post('/bin/utils/updatepage', { field: dialogprop, path : page},

    function(returnedData){

         console.log(returnedData);

});

  });

})($, $(document));

e.g. 2 sling post servlet

(function ($, $document) {

  "use strict";

  $(document).on("click", ".cq-dialog-submit", function (e) {

var dialogprop = $('#filedId');

var page = Granite.author.pageInfoHelper.json.status.path

$.post('page', { "jcr:title": dialogprop},

    function(returnedData){

         console.log(returnedData);

});

  });

})($, $(document));



Arun Patidar

Avatar

Correct answer by
Employee Advisor

Hi,

Please, don't overwrite page properties with properties from a specific component (by whatever means). Instead you should write your title component in a way, that it can display the correct value by reading it from that specific sub-component. In that case I would explicitly build logic into the title component Sling model, which looks for another component on that page and reads the correct title from there (preferably using the model class of that component instead of directly reading from the repo).

In general, I consider this not a good pattern. Because the title of the page is defined at a page level, and not on a component level. You should rethink if your properties are located at the right components.

HTH

Avatar

Community Advisor

Agree with Joerg.

Please use page properties in component but not vice versa.



Arun Patidar