Update Page Properties with component data

Avatar

Avatar

jbrar

Employee

Avatar

jbrar

Employee

jbrar
Employee

10-05-2019

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.

Accepted Solutions (1)

Accepted Solutions (1)

Avatar

Avatar

Jörg_Hoh

Employee

Total Posts

3.0K

Likes

910

Correct Answer

1.0K

Avatar

Jörg_Hoh

Employee

Total Posts

3.0K

Likes

910

Correct Answer

1.0K
Jörg_Hoh
Employee

11-05-2019

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

Answers (4)

Answers (4)

Avatar

Avatar

Arun_Patidar

MVP

Total Posts

2.9K

Likes

1.0K

Correct Answer

831

Avatar

Arun_Patidar

MVP

Total Posts

2.9K

Likes

1.0K

Correct Answer

831
Arun_Patidar
MVP

10-05-2019

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));

Avatar

Avatar

alexandriths

Avatar

alexandriths

alexandriths

10-05-2019

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

Avatar

Arun_Patidar

MVP

Total Posts

2.9K

Likes

1.0K

Correct Answer

831

Avatar

Arun_Patidar

MVP

Total Posts

2.9K

Likes

1.0K

Correct Answer

831
Arun_Patidar
MVP

12-05-2019

Agree with Joerg.

Please use page properties in component but not vice versa.

Avatar

Avatar

ankurk67503819

Avatar

ankurk67503819

ankurk67503819

10-05-2019

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