RE: Freemarker: get Asset Publisher entry URL without version number

thumbnail
Alessandro Candini, modified 6 Years ago. Regular Member Posts: 130 Join Date: 10/17/15 Recent Posts
In Lilferay 7.2 CE GA1, I use the following Widget Template for an Asset Publisher:
<#list entries as curEntry>
  <#assign urlViewInContext = curEntry.getAssetRenderer().getURLViewInContext(renderRequest, renderResponse, currentURL) />
&nbsp; <a href="${urlViewInContext}" target="_self">${curEntry.getTitle(locale)}</a>
<!--#list-->
This creates the following entry URL:  http://myportal/mysite/-/my-web-content-title/2.1
If I click on the URL, the detail page is showed with a default template for my Web Content and not with the custom template I've created for it.
But if I remove the version from the URL, my web content is rendered correctly with my template.
How to remove that version number from entry URLs?
I have to do it with Freemarker string built-ins, or are there methods better than getURLViewInContext() to obtain my goal?
Thank you.
thumbnail
Andrew Jardine, modified 6 Years ago. Liferay Legend Posts: 2416 Join Date: 12/22/10 Recent Posts
Hi Alessandro --

Looks like you are stuck with it, sort of. If you look at the JournalArticleAssetRenderer you will see this code (and where the version is added).

[code]@Override
public String getURLViewInContext(
      LiferayPortletRequest liferayPortletRequest,
      LiferayPortletResponse liferayPortletResponse,
      String noSuchEntryRedirect)
   throws Exception {

   ThemeDisplay themeDisplay =
      (ThemeDisplay)liferayPortletRequest.getAttribute(
         WebKeys.THEME_DISPLAY);

   Layout layout = _article.getLayout();

   if (layout == null) {
      layout = themeDisplay.getLayout();
   }

   Group group = themeDisplay.getScopeGroup();

   if (!_isShowDisplayPage(group.getGroupId(), _article)) {
      String hitLayoutURL = getHitLayoutURL(
         layout.isPrivateLayout(), noSuchEntryRedirect, themeDisplay);

      if (Objects.equals(hitLayoutURL, noSuchEntryRedirect)) {
         hitLayoutURL = getHitLayoutURL(
            !layout.isPrivateLayout(), noSuchEntryRedirect,
            themeDisplay);
      }

      return hitLayoutURL;
   }

   if (group.getGroupId() != _article.getGroupId()) {
      group = GroupLocalServiceUtil.getGroup(_article.getGroupId());
   }

   if (_assetDisplayPageFriendlyURLProvider != null) {
      String friendlyURL =
         _assetDisplayPageFriendlyURLProvider.getFriendlyURL(
            getClassName(), getClassPK(), themeDisplay);

      if (Validator.isNotNull(friendlyURL)) {
         return friendlyURL;
      }
   }

   String groupFriendlyURL = PortalUtil.getGroupFriendlyURL(
      LayoutSetLocalServiceUtil.getLayoutSet(
         group.getGroupId(), layout.isPrivateLayout()),
      themeDisplay);

   StringBundler sb = new StringBundler(5);

   sb.append(groupFriendlyURL);
   sb.append(JournalArticleConstants.CANONICAL_URL_SEPARATOR);
   sb.append(_article.getUrlTitle(themeDisplay.getLocale()));
   sb.append(StringPool.SLASH);
   sb.append(_article.getVersion());

   return PortalUtil.addPreservedParameters(themeDisplay, sb.toString());
}


Unfortunately, there is no logic around the version (something like a configuration setting or a property to toggle it on and off) which means I think the out of the box is with the version and that's it.  There is something you could try though, if you don't want to use the freemarker bult-ins. You COULD choose to create an override for the JournalArticleAssetRendererFactory and then in that override return you own CustomJournalArticleRenderer, which would basically be identifcal, except remove the line where the version is added to the string bundler. 

Unfortunately, I don't think that you can extend the JournalArticleAssetRenderer that Liferay provides out of the box (it doesn't appear to be exported?) which means you either have to copy the code, or use David's trick for forcing packages to be exported. Either way, you should give some thought to the approach because it means that future changes to the JournalArticleAssetRenderer may require you to intervene to take advantage of them. Freemaker string functions, as you are doing right now, maybe in fact be your safest bet.
thumbnail
Alessandro Candini, modified 6 Years ago. Regular Member Posts: 130 Join Date: 10/17/15 Recent Posts
Thank you, Andrew, I see...but this appended version was added recently, because in version 7.0 it was not present.
I solved with the following snippet:
&lt;#list entries as curEntry&gt;
&nbsp; &lt;#assign urlViewInContext = curEntry.getAssetRenderer().getURLViewInContext(renderRequest, renderResponse, currentURL) /&gt;
&nbsp; &lt;#assign versionCharIdx = urlViewInContext?last_index_of("/") - 1 /&gt;
&nbsp; &lt;#assign urlView = urlViewInContext?string[0..versionCharIdx] /&gt;
&nbsp; <a href="${urlView}" target="_self">${curEntry.getTitle(locale)}</a>
<!--#list-->
thumbnail
Andrew Jardine, modified 6 Years ago. Liferay Legend Posts: 2416 Join Date: 12/22/10 Recent Posts
Hi Alessandro,


Yeah, I believe versioning was added to a few more of the models since 7.0 was released, which probably has something to do with the changes. Odd though that Liferay didn't include a configuration for such a change. At any rate, your solution is much simpler that is for sure. One thing though, on the off chance that the version number is NOT there again (like in the next release or something emoticon), it might be an idea to use a regex for the version number so that you don't accidentally lop-off the end of the url emoticon