We used cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it. What For?

« Back to Blogs

Application Display Template - Accessing Web Content and Structure fields

As a Liferay guy, we know that web content is the most crucial part of Liferay CMS. With the help of ADT we can define custom display templates for given portlet to publish many types of contents.

 

In this article, you can find that how we can access web content and structure fields in ADT. But for accessing some of the web content fields in ADT, first we have to access Liferay service locator. Using service locator, we can actually get web content fields (i.e. Tags, Categories).

 

In this article, I’m going to show you how to access Liferay services, Tags, Categories, Related Assets, Custom Fields and Structure fields in ADT in context of web content. For that I have created a web content article with some sample structure fields, tags, categories and custom fields.

 

Here, I am assuming that you have created your web contents with required structure fields to display in asset publisher portlet.

 
Access Liferay Services in ADT : 
 
To access Liferay services in ADT you have to change default portal property which is
 
velocity.engine.restricted.variables=serviceLocator

What it does is, it restrict access of any liferay services in ADT. To enable accessibility of Liferay services you have to remove serviceLocator from above mentioned property.

 

To access service locator in ADT add below properties in portal-ext.properties file and restart the server.

velocity.engine.restricted.classes=
velocity.engine.restricted.variables=

 

Example : Let’s say we want to access site name or any other site information of web content using service we can do as below :

#set ($groupLocalService = $serviceLocator.findService("com.liferay.portal.service.GroupLocalService"))
#set( $siteName = $groupLocalService.getGroup($entry.getGroupId()).getName())

 

Let’s create ADT which display web content article tags, categories, custom fields, etc.

 

  • Tags

 

To access tags of web content article apply below code in your ADT :

 

#set($renderer = $entry.getAssetRenderer())
#set($article = $renderer.getArticle())
#set($articlePrimKey = $article.getResourcePrimKey())
#set($tagLocalService = $serviceLocator.findService("com.liferay.portlet.asset.service.AssetTagLocalService"))
#set($articleTagNames = $tagLocalService.getTagNames("com.liferay.portlet.journal.model.JournalArticle", $getterUtil.getLong($articlePrimKey)))

 

  • Categories

 

To access categories of web content article apply below code :

 

// $renderer, $article and $articlePrimKey same as above 
#set($catLocalService = $serviceLocator.findService("com.liferay.portlet.asset.service.AssetCategoryLocalService"))
#set($articleCatNames = $catLocalService.getCategoryNames("com.liferay.portlet.journal.model.JournalArticle", $getterUtil.getLong($articlePrimKey)))

 

  • Related Assets

To access related assets of web content article use below code. Here we are accessing related assets of Document type :

 

#set($assetLinkLocalService = $serviceLocator.findService("com.liferay.portlet.asset.service.AssetLinkLocalService" ))
#set($assetEntryLocalService = $serviceLocator.findService("com.liferay.portlet.asset.service.AssetEntryLocalService" ))
#set($dLFileEntryLocalService = $serviceLocator.findService("com.liferay.portlet.documentlibrary.service.DLFileEntryLocalService"))
#set($currentArticleRelatedLinks = $assetLinkLocalService.getDirectLinks($entry.getEntryId()))
     #foreach( $link in $currentArticleRelatedLinks )
         #set($linkedAssetEntryId = $link.getEntryId2())
         #set($linkedAssetEntry = $assetEntryLocalService.getEntry($linkedAssetEntryId))
         #set($linkedAssetEntryPrimaryKey = $linkedAssetEntry.getClassPK())
         #set($fileEntry = $dLFileEntryLocalService.getFileEntry($linkedAssetEntryPrimaryKey))
         #set ($title= $httpUtil.encodeURL($htmlUtil.unescape($fileEntry.getTitle())))
         #set ($thumbnail = "/documents/" + $fileEntry.getRepositoryId() + "/" + $fileEntry.getFolderId() + "/" + $title + "/" + $fileEntry.getUuid())
     #end

 

  • Structure Fields

 

To access structure fields of web content article use below code :

 

// $renderer same as above 
        #set($className = $renderer.getClassName() )
        #if( $className == "com.liferay.portlet.journal.model.JournalArticle" )
            #set( $journalArticle = $renderer.getArticle() )
            #set( $document = $saxReaderUtil.read($journalArticle.getContent()) )
            #set( $rootElement = $document.getRootElement() )    
            #foreach( $dynamicElement in $rootElement.elements() )
                #if( "subTitle" == $dynamicElement.attributeValue("name") )
                    #set( $subTitleValue = $dynamicElement.element("dynamic-content").getText() )
                    $subTitleValue <br/>
                #end
            #end
      #end

 

  • Custom Fields

 

To access custom fields use below code :

// $renderer and $article same as above 
// ‘topNewsHeadline’ is the key of custom field
$article.getExpandoBridge().getAttribute("topNewsHeadline")
 

Now you can apply styling on it and make it usable.

 

As web content article we can access tags, categories, related assets and custom fields for other content types like blog entry, calendar event, message board messages, etc…

 

 

contact-us Request a callback WhatsApp