Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

Automatic Custom Page Title from Title in Page Properties

Avatar

Level 2

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 

 

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

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.

View solution in original post

10 Replies

Avatar

Community Advisor

@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

Level 2

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 ?

Avatar

Community Advisor

you can do it in your sling model and show it in HTL.

why do you need to save this in jcr ? (i.e if it is only display purpose)

 

@ValueMapValue(name = "jcr:title")

private String titleText;

 

public String getTitleText() {

return titleText+"YourString";

}

Avatar

Correct answer by
Community Advisor

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

Level 2
This solution worked for me. Thanks Kiran ... really appreciate help.

Avatar

Community Advisor

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

 

 

 

Avatar

Level 2

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.

Avatar

Level 2

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

Community Advisor

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

Avatar

Community Advisor

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)