Automatic Custom Page Title from Title in Page Properties | Community
Skip to main content
Level 2
February 17, 2021
Solved

Automatic Custom Page Title from Title in Page Properties

  • February 17, 2021
  • 6 replies
  • 5629 views

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

 

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Kiran_Vedantam

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.

6 replies

Anudeep_Garnepudi
Community Advisor
Community Advisor
February 17, 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).

shyaAuthor
Level 2
February 17, 2021

I need to save that pagetitle with jcr:title + some fixed string under page properties in JCR once use clicks on save action button at page properties page.. Use will not provide this propery value, user will only enter title, other descriptions of the page. So how I can do that at rendering script, some guidance please ?

Kiran_Vedantam
Community Advisor
Kiran_VedantamCommunity AdvisorAccepted solution
Community Advisor
February 17, 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.

shyaAuthor
Level 2
March 2, 2021
This solution worked for me. Thanks Kiran ... really appreciate help.
BrianKasingli
Community Advisor and Adobe Champion
Community Advisor and Adobe Champion
February 17, 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>

 

 

 

shyaAuthor
Level 2
February 17, 2021

I thought of doing the same, but I have to preserve this value in JCR and some other servlet is referring the value to send page properties node json to other systems.

Level 2
February 17, 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.

arunpatidar
Community Advisor
Community Advisor
February 17, 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.

Arun Patidar
Vijayalakshmi_S
Level 10
February 17, 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)