• Important Code Snippets for Liferay Asset Publisher ADT

    Posted on October 25, 2016 by Hamidul Islam in liferay 7.

    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.  

3 Responses so far.

  1. Harsh Kanakhara says:

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

    I want to display that categories only in my ADT.

  2. ibrahim Mohsen says:

    Could you tell me how to get the content by local in liferay 6.2

  3. Rahul Joshi says:

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

Leave a Reply

Your email address will not be published. Required fields are marked *

Are you human? * Time limit is exhausted. Please reload CAPTCHA.

Top
%d bloggers like this:

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close