Creating custom navigation in Liferay

Hi Friends,

Liferay provides the menu (root navigation). But it always shows from the current community. Sometimes, there is a need of creating a navigation from base community and not from the current community.

For e.g., In your Liferay portal, there are lot of communities and a single base community (with name BASE) which is common for all users and we need the root navigation based on that community only.

To achieve this, we need to create a ServicePreAction, which will create a new custom NavItem object and a change in navigation.vm of the theme, which will show the custom navigation.

Here are the steps to create the custom navigation

1) Create a new ServicePreAction say CustomServicePreAction using hook ( Refer on creating ServicePre hook)

2) In the run() method of CusomServicePreAction, retrieve the BASE community group object

Group baseCommunity = GroupLocalServiceUtil.getGroup( themeDisplay.getCompanyId(), "BASE");

3) After that, retrieve the default layout and layout set of the base community. If you need to show private pages, then pass "true" in the getLayouts() method, else pass "false" to retrieve the public pages.

long defaultLayoutId = LayoutLocalServiceUtil .getDefaultPlid(baseCommunity.getGroupId());

Layout defaultLayout = LayoutLocalServiceUtil .getLayout(defaultLayoutId);

List layoutSet = LayoutLocalServiceUtil.getLayouts(
baseCommunity.getGroupId, true);

4) Create the RequestVars object from the defaultLayout and layoutSet

RequestVars requestVars = new RequestVars(req,themeDisplay, defaultLayout.getAncestorPlid(),

5) finally, create the List of NavItems using RequestVars and layoutSet

List navItems = NavItem.fromLayouts(requestVars,layoutSet);

6) Now, create a new HashMap like

Map vmVariables = new HashMap();

7) Save the NavItems into that

vmVariables.put("baseNavItems", navItems);

8). Save the HashMap into the request

req.setAttribute(WebKeys.VM_VARIABLES, vmVariables);


Now, in the navigation.vm of the Theme,


#foreach ($nav_item in $nav_items) with #foreach ($nav_item in $baseNavItems)


Deploy the theme and the hook.

Now your top navigation will show the menu items from the BASE community and not from the current community.


No comments:

Post a Comment