Important Code Snippets for Liferay Asset Publisher ADT

aseet-publisher-adt-code-snippets

aimAsset Publisher Portlet is one of the most important and widely used portlet in Liferay Portal. By Asset Publisher Portlet we can display different assets in the Portal. This portlet is highly configurable and can be customized using Application Display Template (ADT). This article provides some important code snippets for writing Application Display Template (ADT) for Asset Publisher Portlet.


Prerequisite:

To follow this article you should  have some basic knowledge on

i) Liferay Asset Publisher Portlet

ii) Understanding of Application Display Template (ADT)

iii) Velocity Template or Free Marker Template


For your information:

By default Liferay Asset Publisher Portlet can be configured to display below Assets

asset-type


Iterate All asset Entries

#if (!$entries.isEmpty())
	#foreach ($curEntry in $entries)
		$curEntry.getTitle($locale)
        #end
#end

Display read more link

#if (!$entries.isEmpty())
	#foreach ($curEntry in $entries)
		$curEntry.getTitle($locale)
	<a href="$assetPublisherHelper.getAssetViewURL($renderRequest, $renderResponse, $curEntry, true)">...readMore</a> 
	#end
#end 

Get user related information

##The object $user is ready to use in template  
First Name:$user.getFirstName();

Last Name:$user.getLastName();

Full Name:$user.getFullName();

Date utility

##Get current Date in dd-MM-yyyy format 
$dateUtil.getCurrentDate("dd-MM-yyyy", $locale)

## Format date object where $lastLoginDate is date object
## The date object is formatted to dd MMM yyyy format 

#set ($lastLoginDate = $user.getLastLoginDate())

$dateUtil.getDate($lastLoginDate, "dd MMM yyyy", $locale)

##Convert String to date object 
$dateUtil.parseDate("yyyy-MM-dd","some-date-string", $locale)

Using $locale object

##Get key value from resource bundle. Here asset-services is the key 

$languageUtil.get($locale, "asset-services")

##Get Current Language Id
$locale.toString()

##Compare current Language Id with another. 
##Use the below in if condition  
$locale.toString().equals("en_US")

Identifying Asset Type


##Identify Journal Article
#if (!$entries.isEmpty())
	#foreach ($curEntry in $entries)
	
	#set($renderer = $curEntry.getAssetRenderer() )
	
    #set($className = $renderer.getClassName() )
    
    #if( $className == "com.liferay.journal.model.JournalArticle" )
    
       ## Some other logic goes here 
    
    #end 
	
	#end
#end

In the above source code the line if( $className == “com.liferay.journal.model.JournalArticle” ) identifies the Journal Article. Similarly we can compare the below default Asset Types
com.liferay.wiki.model.WikiPage
com.liferay.document.library.kernel.model.DLFolder
com.liferay.document.library.kernel.model.DLFileEntry
com.liferay.blogs.kernel.model.BlogsEntry
com.liferay.bookmarks.model.BookmarksFolder
com.liferay.bookmarks.model.BookmarksEntry
com.liferay.message.boards.kernel.model.MBMessage
com.liferay.dynamic.data.lists.model.DDLRecord
com.liferay.calendar.model.CalendarBooking
com.liferay.journal.model.JournalFolder

Iterate Journal Article Structure Data

#if (!$entries.isEmpty())
#foreach ($curEntry in $entries)
	
	#set($renderer = $curEntry.getAssetRenderer() )
	
    #set($className = $renderer.getClassName() )
   
 
    #if( $className == "com.liferay.journal.model.JournalArticle" )
   
    #set( $journalArticle = $renderer.getArticle() )
           
    #set( $document = $saxReaderUtil.read($journalArticle.getContent()) )
	
	#set( $rootElement = $document.getRootElement() )
            
	#foreach( $dynamicElement in $rootElement.elements() )
	
		 ##Say that in Web Content Structure there is a field name newsLocation
		  #if( "newsLocation" == $dynamicElement.attributeValue("name") )
		  
			##Retrieve the data from newsLocation field 
			#set( $newsLocation = $dynamicElement.element("dynamic-content").getText())
			
		  #end
	#end
       
	  
    #end
    	
#end
#end

Miscellaneous

## Get Company ID
$themeDisplay.getCompanyId()


## Get Scope Group ID 
$scopeGroupId


##Get Current plId 
$plid

##Get Current Page Title 
$themeDisplay.getLayout().getName($locale)

##Iterate all the current menu items (i.e., navigation items) 
#foreach( $item in $navItems ) 
 $item.getName()   
#end

Summery

All the above codes are velocity code and has been tested in Liferay 7. If you need more code snippets related to Asset Publisher ADT then write your comment in the below.  

About The Author

6 thoughts on “Important Code Snippets for Liferay Asset Publisher ADT”

  1. Harsh Kanakhara

    I am passing asset categories in the Asset Publisher Configuration Page.

    I want to display that categories only in my ADT.

  2. Could you please tell me how I can use ADT to sort Calendar Events in an Asset Publisher by Event Date?

  3. can you tell me please which is code snippet to show image from article? i want implement in rich-summary ADT like in abstract. Also where i can find abstract ADT in source code liferay 7? …thank you

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top