Automatic Custom Page Title from Title in Page Properties

Avatar

Avatar

shya

Avatar

shya

shya

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

T_101

Avatar

T_101

T_101

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.

Answers (5)

Answers (5)

Avatar

Avatar

Arun_Patidar

MVP

Total Posts

3.1K

Likes

1.2K

Correct Reply

879

Avatar

Arun_Patidar

MVP

Total Posts

3.1K

Likes

1.2K

Correct Reply

879
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

KiranVedantam1992

Avatar

KiranVedantam1992

KiranVedantam1992

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.

Avatar

Avatar

Anudeep_Garnepudi

MVP

Avatar

Anudeep_Garnepudi

MVP

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

Vijayalakshmi_S

MVP

Avatar

Vijayalakshmi_S

MVP

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

BrianKasingli

MVP

Avatar

BrianKasingli

MVP

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>