Search code examples
javaliferayfreemarkerliferay-7

How can I get instance_id from menus and breadcrumbs in liferay 7.4


I'm trying to insert custom menu with template in theme, but I don't have enough information. I have this code <@liferay.navigation_menu instance_id="XXX" />, but I don't know how can I get menu instance_id, same issue appear when I try to insert breadbrumbs. I'm using liferay 7.4.

I try this

<@liferay.navigation_menu instance_id="MainMenu" />
<@liferay.navigation_menu instance_id="MainMenu_randomString" />
<@liferay.navigation_menu instance_id="MainMenu_siteNavigationMenuId like 1234" />
<@liferay.navigation_menu instance_id="siteNavigationMenuId like 1234" />


Solution

  • What you typically do when a static portlet (which, AFAIK navigation_menu is) is embedded in a theme: You'll need to come up with a random instance ID - because this is used to create a unique key to store the portlet's configuration. Especially as a page might contain multiple navigation menus, and you'd like them to show different parts, depths, variations of the navigation.

    The way to come up with such a random value surprised me when I learned about it: You just make it up. "1234" is fine, "fwo39DvAR2" is fine as well - just make sure you don't make up the same value over and over if you embed two navigation menus on the page: Otherwise they share their configuration.

    Speaking of configuration: Configuration of an embedded portlet/widget in a page is a bit clunky and often is done over and over (e.g. whenever a page is displayed again) when done in code. As you didn't identify this as a problem, I'm just pointing to some additional reading that you might want to see. And if I look at Liferay's own source code, I don't see the instance_id configured.

    However: You're on Liferay 7.4, and the Best Practices have changed: The general recommendation to solve the same problems is to create a MasterPage (or more) and use it throughout all pages (create only the default ContentPages, no more WidgetPages). Those can be configured at runtime through the UI. For the theme/look&feel part, you'd use a stylebook - this enables you to go completely without any custom module and make runtime changes to the look&feel without redeployment of any module. Liferay's classic theme already is quite configurable in terms of stylebooks, but the (also delivered in standard) Dialect theme offers a lot more. Check how much flexibility (and complexity) you need.