Ask Questions and Find Answers
Important:
Ask is now read-only. You can review any existing questions and answers, but not add anything new.
But - don't panic! While ask is no more, we've replaced it with discuss - the new Liferay Discussion Forum! Read more here here or just visit the site here:
discuss.liferay.com
How can a portlet on a display page find what asset is being displayed?
Hi all,
I'm sure there's a fast&simple way way to do this, but I'm not seeing it...
I'd like to put a portlet on a display page, which is not initially bound to a certain webcontent.
At runtime, I'd like my portlet to find out what webcontent is being displayed and work that info.
How can a portlet find out what asset/web content is being shown by the display page template?
TIA
Fernando
Hi Fernando, I can see that you received an answer in slack. I'm not sure if we have a policy about posting in both places, but it's probably best to at least link to the other context if you do post in both.
By the way, when I saw the answer in slack it struck me as brittle. I'd hope ther was an API or utilities to get or construct the URL. But I don't know so I can't give you a good answer on it.
Hi Russell,
I always come here to post my questions, before I go to Slack, because answers in Slack are not easily found by others. :-) In this case, I though this post didn't go ahead, because I was getting an error when posting, so I went to Slack instead.
Meanwhile, looking at the source code of RelatedAssetsInfoCollectionProvider.java, I got the answer I really needed.
While Vitaly's answer is quite usable, if you're working in Java you can use the API to find the assetId with this code:
private long _getLayoutAssetEntryId() {
ServiceContext serviceContext =
ServiceContextThreadLocal.getServiceContext();
HttpServletRequest httpServletRequest = serviceContext.getRequest();
AssetEntry layoutAssetEntry =
(AssetEntry)httpServletRequest.getAttribute(
WebKeys.LAYOUT_ASSET_ENTRY);
if (layoutAssetEntry != null) {
return layoutAssetEntry.getEntryId();
}
return 0;
}
Fernando
Great! Glad you got it solved in an even more robust way.
Powered by Liferay™