Issue
-
DXP 7.0+ provides capability to create navigation menus from the UI (production menus). However, after creating one navigation menu, I am trying to refer the menu items inside theme. I know the default main nav can be accessed in the theme (as navItems in init.ftl), so I wonder if there are any service calls that can be used to get menu items from the new created navigation menu? ( ie. something similar as layoutService = serviceLocator.findService("com.liferay.portal.kernel.service.LayoutLocalService")
I think it should have a way to reference it like other objects (users, articles etc.)
Environment
- Liferay DXP 7.0+
Resolution
-
The most important thing I need is how to reference it inside a theme, not an ADT, that is, I need to get the new menu object in the theme. Is there a local service to call to get it?
If you are referring to local services that can be used to access the site navigation menu, you would need the following:
- com.liferay.dynamic.data.mapping.service.DDMTemplateLocalService to find the template
- com.liferay.site.navigation.service.SiteNavigationMenuLocalService to find the site navigation menu
<#assign ddmTemplateLocalService = serviceLocator.findService("com.liferay.dynamic.data.mapping.service.DDMTemplateLocalService")>However, a lot of the API that converts a SiteNavigationMenu into a list of NavItem objects is not public, so even with the API, it's not possible to do everything from the theme.
<#assign template = ddmTemplateLocalService.fetchDDMTemplate(...)>
<#assign siteNavigationMenuLocalService = serviceLocator.findService("com.liferay.site.navigation.service.SiteNavigationMenuLocalService")>
<#assign navigationMenu = siteNavigationMenuLocalService.fetchSiteNavigationMenu(...)>
Instead of doing everything from the theme, developers are expected to create an Application Display Template (ADT) for Navigation Menu Template. Inside of the ADT, the entries object will contain a list of NavItem objects, and you can operate on that list in the same way as you would have done in a theme during past Liferay releases.
Once you've created an ADT, you can use the liferay-site-navigation:navigation-menu tag library from inside the theme in order to run the code inside the ADT. As an example, you would have something like the following (consult the tag library documentation for additional information on the fields that aren't directly related to the ADT or the navigation menu):
<@liferay_site_navigation["navigation-menu"]
ddmTemplateGroupId=template.groupId
ddmTemplateKey=template.templateKey
displayDepth=1
expandedLevels="auto"
rootItemType="absolute"
rootItemLevel=0
siteNavigationMenuId=navigationMenu.siteNavigationMenuId /> - The Westeros Bank Theme is also an example of modifying the navigation menu inside the theme. This uses the macro to embed the portlet: https://github.com/liferay/liferay-portal/blob/7.2.x/modules/apps/frontend-theme-westeros-bank/frontend-theme-westeros-bank/src/templates/navigation.ftl
- See also the example mentioned in Liferay DXP FreeMarker Macros (Reference Examples).