Removing panels from my account in Liferay 7.2

In a recent upgrade project, we had to remove panels from the 'My account' section in a Liferay 7.2 environment. In liferay 6.2 this quite easy to do with some portal-ext.properties. In Liferay 7.2 these portal properties are not valid anymore. While searching for a solution I found this wonderful blog from David H. Nebinger that explained how to remove panels from my account in Liferay 7. While this blog was a real help, there are some changes to the underlying framework which changes the solution.

Therefore, in this article I will provide an updated tutorial on how you can remove panels from my account in Liferay DXP 7.2, based on the solution proposed by David Nebinger.

Migrating to the screen navigator framework

While researching the solution I found that Liferay actually tried to make customizing liferay forms easier, so no custom development was necessary. They added the option to change which panels are visible through system settings (see documentation). Unfortunately, this does not work anymore for liferay 7.2. because my account does not use the form navigator framework anymore. In Liferay 7.2 (and possibiliy 7.1 & 7.3) the screen navigator framework is used for 'My account'. Luckily, we can use the solution proposed by David Nebinger, with some alterations.

Changing the my account screen navigation Id

The first the we have to do is change the my account screen navigation id, so we can use our own custom screen navigation implementation. We will be creating a fragment bundle, targeting the correct Liferay bundle and version. So create a fragment bundle (either with blade or the IDE, and open the bnd file. In the block below this you can see we are targeting the com.liferay.users.admin.web bundle version 4.0.48 up to, but excluding version 5.0.0. Search your gogo shell for Users Admin Web to check which version of the bundle you need to target. Add this snippet with the correct version to your bnd file.

Fragment-Host:  com.liferay.users.admin.web;bundle-version="[4.0.48,5.0.0)"

We need to override the edit_user.jsp of the com.liferay.users.admin.web module. So we in the following folder /src/main/resources/META-INF/resources (same path as the original jsp) we are gonna add a jsp with the same name. In this, we are gonna copy the code from the original jsp, but we are gonna make some changes. Unfortunately, my account uses the same jsp's as the portlet where you can edit users as an admin. We only want to hide the panels when accessed through my-account, not for admins that access these panels through the admin portlet. Therefore, we have to add an if statement to only add a custom screen navigator id if the portlet id is equal to the my account portlet id. Luckily this is easy to do since the my account portlet id is set in the init.jsp .

<%@ include file="/init.jsp" %>


<%
String backURL = ParamUtil.getString(request, "backURL");
User selUser = PortalUtil.getSelectedUser(request);
PortletURL portletURL = liferayPortletResponse.createRenderURL();
if (selUser != null) {
    portletURL.setParameter("p_u_i_d", String.valueOf(selUser.getUserId()));


portletURL.setParameter("mvcRenderCommandName", "/users_admin/edit_user");
if (Validator.isNotNull(backURL)) {
    portletURL.setParameter("backURL", backURL);



/* Begin override of My Account */
//initialize to the real form navigator id.
String formNavigatorId = UserScreenNavigationEntryConstants.SCREEN_NAVIGATION_KEY_USERS;
//Only change the id if this jsp is used in my account portlet
if (portletName.equals(myAccountPortletId)) {
    // include the special prefix
    formNavigatorId = "my.account." + formNavigatorId;


/* End override of My Account */
%>



<liferay-frontend:screen-navigation
	containerCssClass="col-lg-8"
	containerWrapperCssClass="container-fluid container-fluid-max-xl container-form-lg"
	context="<%= selUser %>"
	headerContainerCssClass=""
	key="<%= formNavigatorId %>"
	menubarCssClass="menubar menubar-transparent menubar-vertical-expand-lg"
	navCssClass="col-lg-3"
	portletURL="<%= portletURL %>"
/>

Adding the panels we want to show

This was all we had to do for now to hide the original panels. If you don't want to show any panels then you can stop at this stage. We have changed the key of this form navigator to a custom key, therefore no ScreenNavigator categories or entries will be available.

However, I assume you would still like people to be able to change their password & general information. Therefore, we will be recreating these panels based on the original implementation. Let's create a new module called custom-my-account-screennavigator-entries. See the source code I have included at the bottom of this blog for the correct gradle dependencies. First, let's focus on creating the prefix constant, so we can manage this prefix in one place. We will create a Constants file with the prefix we added in our fragment bundle.

public class Constants {


    public static final String MY_ACCOUNT_PREFIX = "my.account.";


}

The next step is to implement the Panel categories we do want to show.

package nl.finalist.custom.my.account.screennavigator.category;


import com.liferay.frontend.taglib.servlet.taglib.ScreenNavigationCategory;
import com.liferay.portal.kernel.language.LanguageUtil;
import com.liferay.users.admin.constants.UserScreenNavigationEntryConstants;


import java.util.Locale;


import org.osgi.service.component.annotations.Component;


import nl.finalist.custom.my.account.screennavigator.constants.Constants;


@Component(
property = "screen.navigation.category.order:Integer=10", 
service = ScreenNavigationCategory.class)
public class UserGeneralScreenNavigationCategory implements ScreenNavigationCategory {


	@Override
	public String getCategoryKey() {
		return UserScreenNavigationEntryConstants.CATEGORY_KEY_GENERAL;
	}


	@Override
	public String getLabel(Locale locale) {
		return LanguageUtil.get(locale, "general");
	}


	@Override
	public String getScreenNavigationKey() {
		return Constants.MY_ACCOUNT_PREFIX + UserScreenNavigationEntryConstants.SCREEN_NAVIGATION_KEY_USERS;
	}




This class isa copy of the UserGeneralScreenNavigationCategory with a small change in the getScreenNavigationKey method, namely the adding of our custom prefix.

We can mostly do the same for the screen navigation entries, i.e. copying the class from the original implementation and changing the screen navigation key to contain the custom prefix.

There is another small change we have to do this class to prevent runtime errors, namely we can't set the JSP renderer the same as in the original implementation. We need to set it with a setter method, otherwise it will be null at runtime.

package nl.finalist.custom.my.account.screennavigator.entry;


import com.liferay.frontend.taglib.servlet.taglib.ScreenNavigationEntry;
import com.liferay.frontend.taglib.servlet.taglib.util.JSPRenderer;
import com.liferay.portal.kernel.language.LanguageUtil;
import com.liferay.portal.kernel.model.User;
import com.liferay.portal.kernel.util.AggregateResourceBundle;
import com.liferay.portal.kernel.util.Portal;
import com.liferay.portal.kernel.util.PortalUtil;
import com.liferay.portal.kernel.util.ResourceBundleUtil;
import com.liferay.users.admin.constants.UserScreenNavigationEntryConstants;
import com.liferay.users.admin.web.internal.constants.UsersAdminWebKeys;


import java.io.IOException;
import java.util.Locale;
import java.util.ResourceBundle;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.osgi.service.component.annotations.Reference;


import nl.finalist.custom.my.account.screennavigator.constants.Constants;


public abstract class BaseUserScreenNavigationEntry implements ScreenNavigationEntry<User> {


	JSPRenderer jspRenderer;


	@Reference
	protected Portal portal;


	public abstract String getActionCommandName();


	public abstract String getJspPath();


	@Override
	public String getLabel(Locale locale) {
	    return LanguageUtil.get(getResourceBundle(locale), getEntryKey());
	}


	protected ResourceBundle getResourceBundle(Locale locale) {
	    ResourceBundle resourceBundle = 
            ResourceBundleUtil.getBundle("content.Language", locale, getClass());


	    return new AggregateResourceBundle(resourceBundle, 
            PortalUtil.getResourceBundle(locale));
	}


	@Override
	public String getScreenNavigationKey() {
	     return Constants.MY_ACCOUNT_PREFIX + 
             UserScreenNavigationEntryConstants.SCREEN_NAVIGATION_KEY_USERS;
	}


	public boolean isEditable(HttpServletRequest httpServletRequest, 
           HttpServletResponse httpServletResponse) {


	    return true;
	}


	public boolean isShowControls() {
		return true;
	}


	public boolean isShowTitle() {
		return true;
	};


	@Override
	public void render(HttpServletRequest httpServletRequest, 
          HttpServletResponse httpServletResponse)throws IOException {


	    if (getJspPath() == null) {
		return;
	    }


	    httpServletRequest.setAttribute(UsersAdminWebKeys.ACTION_COMMAND_NAME, 
                getActionCommandName());
	    httpServletRequest.setAttribute(UsersAdminWebKeys.EDITABLE,
		isEditable(httpServletRequest, httpServletResponse));
	    httpServletRequest.setAttribute(UsersAdminWebKeys.FORM_LABEL, 
                getLabel(httpServletRequest.getLocale()));
	    httpServletRequest.setAttribute(UsersAdminWebKeys.JSP_PATH, getJspPath());
            httpServletRequest.setAttribute(UsersAdminWebKeys.SHOW_CONTROLS, isShowControls());
	    httpServletRequest.setAttribute(UsersAdminWebKeys.SHOW_TITLE, isShowTitle());
	    
            jspRenderer.renderJSP(httpServletRequest, httpServletResponse, 
                "/edit_user_navigation.jsp");
	}


	/*
	 * Always override when extending this class, otherwise jsp renderer is null in
	 * the render method
	 */
	@Reference(unbind = "-")
	public void setJSPRenderer(JSPRenderer jspRenderer) {
		this.jspRenderer = jspRenderer;
	}


Deploying and testing

That is all we need to do to show our custom panels. Now deploy the modules and let's test it. Log in and go to your account settings. You can see that only the panels we created in our module are shown.

My account settings

A last check is to see whether we can still see all panels when we edit the user as an admin, so go to Control panel - Users - Users and organisations and edit the same user. We can see still see all the panels in this mode. You have now successfully hidden the panels from my account.

Source code

Danielle Ardon

Fullstack developer[email protected]LinkedIn
Meer van Danielle Ardon

Meer artikelen

Composer
Composer 2
Dependency hell
Bash

Use multiple composer versions simultaneously

Valerie Valkenburg
4 minuten lezen
Lees het artikel Use multiple composer versions simultaneously
liferay
search
prefiltering
modelprefiltercontributor
layout
Liferay dxp
portlet

Allow portlet pages to be shown in Liferay Search

Danielle Ardon
4 minuten lezen
Lees het artikel Allow portlet pages to be shown in Liferay Search
React
pwa
scraping
json
php

Make an app, scrape the net

Emmanuel Maltete
12 minuten lezen
Lees het artikel Make an app, scrape the net