Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
BedrockMission!

Learn More

View all

Sign in to view all badges

Automatic Custom Page Title from Title in Page Properties

Avatar

Avatar
Level 1
shya
Level 1

Likes

0 likes

Total Posts

9 posts

Correct Reply

0 solutions
View profile

Avatar
Level 1
shya
Level 1

Likes

0 likes

Total Posts

9 posts

Correct Reply

0 solutions
View profile
shya
Level 1

16-02-2021

Hi Experts,

New in AEM.

I have to customize page title textfield at page properties using jcr:title (Title) string by appending some fixed string every time. Have to programmatically achieve this before saving the page properties to JCR. 

I am thinking to do this using JS, by assigning the pagetitle value before "SimpleSave" or "Save & Close" action is called. 

 

Where I can make this change if my approach is correct ? I am unable to find the location or there is any better way of doing that ?

 

Please reply.

 @kautuk_sahni @BrianKasingli @Arun_Patidar 

 

Accepted Solutions (1)

Accepted Solutions (1)

Avatar

Avatar
Springboard
Level 7
KiranVedantam1992
Level 7

Likes

169 likes

Total Posts

175 posts

Correct Reply

54 solutions
Top badges earned
Springboard
Give Back 5
Ignite 1
Affirm 50
Validate 1
View profile

Avatar
Springboard
Level 7
KiranVedantam1992
Level 7

Likes

169 likes

Total Posts

175 posts

Correct Reply

54 solutions
Top badges earned
Springboard
Give Back 5
Ignite 1
Affirm 50
Validate 1
View profile
KiranVedantam1992
Level 7

17-02-2021

Hi @shya,

 

For your use-case, you can leverage Event handling functionality in AEM. We have multiple ways to do it and I will discuss one of the ways.

  • As you are creating a page, which is a JCR level event, write an Event Listener with the event Event.NODE_ADDED.
  • This listener will get triggered whenever there is a node of a particular type [in your case cq:page] added in the provided location [The content path where you are adding the page].
  • Inside the onEvent() method, fetch the page node via resource, get the title already added and saved by the author, and append the static text you need.
  • Also, this same action can be done via workflows [triggering them on particular content paths], schedulers [updating the content pages at a particular time], and so on...

For more info on the event listeners please visit my blog here.

 

Hope this helps.

 

Thanks,

Kiran Vedantam.

Answers (5)

Answers (5)

Avatar

Avatar
Coach
MVP
Arun_Patidar
MVP

Likes

1,345 likes

Total Posts

3,220 posts

Correct Reply

914 solutions
Top badges earned
Coach
Contributor 2
Ignite 10
Give Back 700
Boost 1000
View profile

Avatar
Coach
MVP
Arun_Patidar
MVP

Likes

1,345 likes

Total Posts

3,220 posts

Correct Reply

914 solutions
Top badges earned
Coach
Contributor 2
Ignite 10
Give Back 700
Boost 1000
View profile
Arun_Patidar
MVP

17-02-2021

Hi, we had the same requirement to add a prefix to the title, we added a new field in the page property and read it when we created <title> tag, since this property always be available in JCR and you can reuse it, you can customize it as well. you can add default value as well to this new property to get autofill value on creating.

Avatar

Avatar
Establish
MVP
Anudeep_Garnepudi
MVP

Likes

274 likes

Total Posts

320 posts

Correct Reply

87 solutions
Top badges earned
Establish
Ignite 1
Give Back 5
Give Back 3
Give Back 10
View profile

Avatar
Establish
MVP
Anudeep_Garnepudi
MVP

Likes

274 likes

Total Posts

320 posts

Correct Reply

87 solutions
Top badges earned
Establish
Ignite 1
Give Back 5
Give Back 3
Give Back 10
View profile
Anudeep_Garnepudi
MVP

16-02-2021

@shya 

If the String that you want to append is fixed, then better append it in your rendering script rather than doing it from front end(Javascript).

Avatar

Avatar
Boost 500
MVP
Vijayalakshmi_S
MVP

Likes

500 likes

Total Posts

637 posts

Correct Reply

211 solutions
Top badges earned
Boost 500
Give Back 50
Give Back 5
Ignite 10
Ignite 5
View profile

Avatar
Boost 500
MVP
Vijayalakshmi_S
MVP

Likes

500 likes

Total Posts

637 posts

Correct Reply

211 solutions
Top badges earned
Boost 500
Give Back 50
Give Back 5
Ignite 10
Ignite 5
View profile
Vijayalakshmi_S
MVP

17-02-2021

Hi @shya,

Your approach is correct. Create a clientlib with OOTB categories as "cq.authoring.dialog" (and dependencies as "cq.jquery") as it is needed only in author.

In the JS logic, amend the title conditionally (that is only if it doesn't contains the fixed string, otherwise for every submit of page properties dialog, title value will be amended with fixed string)

 

Avatar

Avatar
Boost 1
Level 1
T_101
Level 1

Like

1 like

Total Posts

1 post

Correct Reply

0 solutions
Top badges earned
Boost 1
Affirm 1
View profile

Avatar
Boost 1
Level 1
T_101
Level 1

Like

1 like

Total Posts

1 post

Correct Reply

0 solutions
Top badges earned
Boost 1
Affirm 1
View profile
T_101
Level 1

17-02-2021

Hi @shya 

 

One approach is that you can always control the value of the fields in the page properties by using a simple js like the one below

 

(function (document, $, Granite) {
    "use strict";
    $(document).on("click", ".granite-form-saveactivator", function (e) {
        saveCustomTitle(e)
    });

    $(document).on("click", ".foundation-wizard-control", function(e) {
        if($(this)[0].innerText=='Create'){
            saveCustomTitle(e)
        }
    })
})(document, Granite.$, Granite);

function saveCustomTitle(e){
        e.stopPropagation();
        e.preventDefault();
        let titleEl = $("input[name='./jcr:title']");
        titleEl.val(titleEl.val() + " custom text");
        e.target.dispatchEvent(new MouseEvent('click', e.nativeEvent));
}

 

The js file should be added to an appropriate clientlib folder so that it gets loaded while rendering the page properties dialog.

Avatar

Avatar
Establish
MVP
BrianKasingli
MVP

Likes

610 likes

Total Posts

588 posts

Correct Reply

230 solutions
Top badges earned
Establish
Ignite 1
Give Back 5
Give Back 3
Give Back 10
View profile

Avatar
Establish
MVP
BrianKasingli
MVP

Likes

610 likes

Total Posts

588 posts

Correct Reply

230 solutions
Top badges earned
Establish
Ignite 1
Give Back 5
Give Back 3
Give Back 10
View profile
BrianKasingli
MVP

17-02-2021

@shya how are you planning to use this information, is this for SEO? you can easily do this with sightly:

 

<head>
   <title>${pageProperties['jcr:title']} | My Company Name</title>
</head>