Search code examples
liferayvelocityliferay-6

Access Liferay Custom Field from Velocity template


I'm trying to emit the value of a custom field from within one of the Liferay templates.

Using the admin UI, I have defined a new Organization-level custom field named "org-home-page", with a default value of "tom rules".

I would like to emit this value in portal_normal.vm

I've cobbled this code together based on some posts and samples sent by a co-worker, along with a ton of my own experimentation:

$page.getGroup().getExpandoBridge().getAttribute("org-home-page")

Unfortunately, Velocity can't resolve the expression, and leaves it untouched.

The following expressions do evaluate within portal_normal, but obviously none of these statements does the whole job:

$page                               ## seems to represent the current page
$page.getGroup()                    ## seems to represent the current Org
$page.getGroup().getExpandoBridge() ## seems to give me an "Expando bridge" object

Only the very last step -- where I identify by name the specific custom field whose value I'd like to retrieve -- is failing.

I'm not allowed to write any custom Java to facilitate this, so don't bother firing up Eclipse. 8) Only solutions that can be implemented entirely within Velocity templates are acceptable.

Any help is appreciated.


Solution

  • I was able to get the value of custom field of an organization using the following approach in Liferay Portal 6.1.0. Maybe it's too verbose but at least it works. :)

    init_custom.vm

    ...
    ## Null variable
    #set($null = $some-never-used-variable-name)
    ...
    #set($organization = $null)
    #if ($layout.getGroup().isOrganization())
        ## Get organization by id
        #set($organizationLocalService = $serviceLocator.findService("com.liferay.portal.service.OrganizationLocalService"))
        #set($organizationId = $layout.getGroup().getOrganizationId())
        #set($organization = $organizationLocalService.getOrganization($organizationId))
    #end
    ...
    

    portal_normal.vm

    ...
    #if ($organization != $null)
        ## Use value of custom field of organization
        $organization.getExpandoBridge().getAttribute("org-home-page")
    #end    
    ...