Search code examples
magnolia

Get node name for the executed custom action in magnolia cms


I created a custom action in magnolia cms called MyAction. I would like to get the node name of the page on which the action is executed. Instead I am getting an empty string for the page name.

This is the code:

package ch.xxx.module.versioning;

import info.magnolia.ui.api.action.Action;
import info.magnolia.ui.api.action.ActionExecutionException;

import javax.jcr.LoginException;
import javax.jcr.RepositoryException;
import javax.jcr.Session;

import info.magnolia.context.Context;
import info.magnolia.context.MgnlContext;



public class MyAction implements Action  {


    @Override
    public void execute() throws ActionExecutionException {
        String nodeName= "null";

        Context context = MgnlContext.getInstance();
        Session session = null;
        try {
            session = context.getJCRSession("website");
        } catch (LoginException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (RepositoryException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        //Get root node
        try {
            nodeName = session.getRootNode().getName();
        } catch (RepositoryException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        System.out.println("Executed MyAction for node: " + nodeName);
    }
}

Solution

  • Thanks @Ducaz035!

    Here is the solution that worked for the custom action:

    public class MyAction extends AbstractMultiItemAction<zzzVersioning>  {
    
    
    public MyAction(zzzVersioning definition, JcrItemAdapter item, UiContext uiContext) {
                    super(definition, item, uiContext);
                    // TODO Auto-generated constructor stub
                }
    
    
    
    @Override
    public void execute() {
        try {
            System.out.println("Ran execute Action! " + getItems().get(0).getJcrItem().getName());
        } catch (RepositoryException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    
    }
    
    
    
    @Override
    public void executeOnItem(JcrItemAdapter item) throws Exception {
        // TODO Auto-generated method stub
    
    }
    
    
    
    @Override
    protected String getSuccessMessage() {
        // TODO Auto-generated method stub
        return null;
    }
    
    
    
    @Override
    protected String getFailureMessage() {
        // TODO Auto-generated method stub
        return null;
    }
    

    }

    Here is the code for the custom action definition:

    public class zzzVersioning extends CommandActionDefinition {
    
        public zzzVersioning() {
            this.setImplementationClass(MyAction.class);
        }
    }