RE: RE: How can a portlet on a display page find what asset is being displa

thumbnail
Fernando Fernandez, modified 3 Years ago. Expert Posts: 401 Join Date: 8/22/07 Recent Posts

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

thumbnail
Russell Bohl, modified 3 Years ago. Expert Posts: 308 Join Date: 2/13/13 Recent Posts

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.

thumbnail
Fernando Fernandez, modified 3 Years ago. Expert Posts: 401 Join Date: 8/22/07 Recent Posts

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

thumbnail
Russell Bohl, modified 3 Years ago. Expert Posts: 308 Join Date: 2/13/13 Recent Posts

Great! Glad you got it solved in an even more robust way.