Custom inbox column in Magnolia CMS

Custom inbox column in Magnolia CMS

In this example I add a new column to show template information of the objects in the inbox of Magnolia. To achieve the desired result we need three new classes.

  • CustomInbox.java
  • CustomInboxListModel.java
  • CustomInboxValueProvider.java

Let’s start with CustomInbox.java which extends the original inbox class info.magnolia.module.workflow.inbox.Inbox. In the configureList() method we add the new column, add it to the list and declare it as groupable, which will later allow us to group all the inbox entries by their template.

public class CustomInbox extends Inbox {

    Inbox inbox; //this is to prevent having a CustomInbox.html and use Inbox.html instead

    public CustomInbox(String name, HttpServletRequest request, HttpServletResponse response) {
        super(name, request, response);
        inbox = new Inbox(name, request, response); //this is to prevent having a CustomInbox.html and use Inbox.html instead
    }

    @Override
    public void configureList(ListControl list) {
        super.configureList(list);

        list.addGroupableField("template"); // make the column groupable by it's values
        list.addColumn(new ListColumn() {
            {
                setName("template");
                setLabel(MessagesManager.getMessages("info.magnolia.module.admininterface.messages").get("tree.web.template"));
                setWidth("150px");
            }
            @Override
            public Object getValue() {
                String uuid = "" + this.getListControl().getIteratorValue("uuid");
                String repository = "" + this.getListControl().getIteratorValue("repository");
                Content content = null;
                try {
                    content = MgnlContext.getSystemContext().getHierarchyManager(repository).getContentByUUID(uuid);
                } catch (RepositoryException e) {
                    return "-";
                }
                if(content == null)
                    return "-";

                String templateName = content.getTemplate();

                return StringUtils.isBlank(templateName) ? "-" : templateName;
            }
        });
    }

    @Override
    public ListModel getModel() {
        return new CustomInboxListModel(MgnlContext.getUser().getName());
    }

    @Override
    public String onRender() {
        return FreemarkerUtil.process(inbox); //this is to prevent having a CustomInbox.html and use Inbox.html instead
    }
}

Now we already would see the custom column in our inbox. But for the grouping to work we now need the other two classes. Let’s continue with our CustomInboxListModel.java which we return in the getModel() method in CustomInbox.java. The only thing this class does is to set the value provider which is used to provide the values used to group the columns. This is where we set our own value provider implementation.

public class CustomInboxListModel extends InboxListModel {

    public CustomInboxListModel(String userName) {
        super(userName);

        setValueProvider(new CustomInboxValueProvider()); //CustomInboxValueProvider provides values which are used for grouping the column. This class has yet to be created.
    }

}

As a final step we need to create the missing class CustomInboxValueProvider.java where we override the getValue() method and return the template name, which then is used to group the inbox entries.


public class CustomInboxValueProvider extends InboxValueProvider {

    @Override
    public Object getValue(String name, Object obj) {
        if(name.equals("template")) {
            String path = (String) getValue("path", obj);
            if (path != null) {
                return ContentUtil.getContent("website", path).getTemplate();
            }
        }

        return super.getValue(name, obj);
    }
}