Displaying editor information of a web page in Magnolia

Displaying editor information of a web page in Magnolia

Sometimes it is useful to know who last edited a certain web page. Here are some examples of information you can extract out of a content nodes meta data.

Print username

[#assign username = content.metaData.authorId!]
<p>Last edited by: ${username}</p>

Print first and last name of the user

//extend info.magnolia.module.templatingkit.templates.STKTemplateModel and use your own model
public class MyCustomSTKTemplateModel extends STKTemplateModel {
    ...

    public String getFirstAndLastnameOfEditor() {
        String username = content.getMetaData().getAuthorId();
        String firstAndLastname = SecuritySupport.Factory.getInstance().getUserManager().getUser(username).getProperty("title");

        return firstAndLastname;
    }

    ...
}

 

[#assign firstAndLastnameOfEditor = model.firstAndLastnameOfEditor!]
<p>Last edited by: ${firstAndLastNameOfEditor}</p>

Unfortunately Magnolia only saves the full name of the user in one field, so first name and last name can be read separately. But of course you can split the String on the empty space.

Getting first name and last name separately

public class MyCustomSTKTemplateModel extends STKTemplateModel {
    ...

    public String getFirstAndLastnameOfEditor() {
        ...
    }

    public String getFirstnameOfEditor() {
        String firstAndLastname = getFirstAndLastnameOfEditor();
        String firstname = org.apache.commons.lang.StringUtils.substringBefore(firstAndLastname, " ");

        return firstname;
    }

    public String getLastnameOfEditor() {
        String firstAndLastname = getFirstAndLastnameOfEditor();
        String lastname = org.apache.commons.lang.StringUtils.substringAfter(firstAndLastname, " ");

        return lastname;
    }

    ...
}

This obviously only works if the first and last name of the user are separated by an empty space.

[#assign firstname = model.firstNameOfEditor!]
[#assign lastname = model.lastNameOfEditor!]
<p>Last edited by: ${lastname}, ${firstname}</p>

Get the modification date
With helper method in the model

public String getLastModificationDate() {
    Calendar cal = content.getMetaData().getModificationDate();
    SimpleDateFormat df = new SimpleDateFormat("dd.MM.yyyy");

    return df.format(cal.getTime());
}

 

<p>Last modififed on ${model.lastModificationDate!}</p>

In Freemarker only

[#assign date = content.metaData.modificationDate?string("yyyy-MM-dd HH:mm:ss")!}
<p>Last modified on ${date}</p>

For more information on date formatting in Freemarker see Freemarker Manual – Built-ins for dates

As always please post a comment if you have questions