actionitem editmode javascript redirect Jahia 7.3

Add a redirect action in context menu in edit mode

Question

How to add a redirect action in a context menu in edit mode?

Answer

As the GoToContentActionItem don't do a redirect to a content, a JSActionItem must be used. So follow steps are needed to define this Action:

  • Define a new JSActionItem and add it to the richt context menu in edit mode (in the spring definition of the module). In the example it is added in the content tab to the context menu:

<bean id="actionID" class="org.jahia.ajax.gwt.client.widget.toolbar.action.JSActionItem" >
        <property name="execute" value="executeFunction"/>
        <property name="init" value="initFunction"/>
        <property name="handleNewSelection" value="handleNewSelectionFunction"/>
        <property name="handleNewMainNodeLoaded" value="handleNewMainNodeLoadedFunction"/>
        <property name="selectionTarget" value="main"/> // main for single node selection
</bean>

<bean id="toolbar.item.actionID" class="org.jahia.services.uicomponents.bean.toolbar.Item" >
        <property name="actionItem" ref="actionID"/>
        <property name="parent" value="contentTableContextMenuAnthracite"/>
</bean>
  • Inject a custom java script file in the edit mode. This can be done in a module spring file like:

 <bean id="gwtResources" class="org.jahia.ajax.gwt.helper.ModuleGWTResources">
        <property name="javascriptResources">
            <list>
                <value>/path/to/your/jsFile.js</value>
            </list>
        </property>
    </bean>

You can store the js file also in the module, so the path would look like: /modules/yourModuleName/javascript/jsFile.js

 

  • Inside the js file, you have to define the needed js methods (which are configured in the JSActionItem in step1:

window.InitFunction = (i) => {
    
};

window.handleNewSelectionFunction = (path) => {

};

window.handleNewMainNodeLoadedFunction = (path) => {
};

window.ExecuteFunction = (path) => {
    //handle redirection 
};

 As example for the window.ExecuteFunction you can do there a location.replace to an servlet or any other url which is needed. The path parameter specify the current content path (where the action is executed).