Forums

Home » Liferay Portal » English » 3. Development

Combination View Flat View Tree View
Threads [ Previous | Next ]
toggle
DarshanKumar N Bhatia
How to get Custom Field of Web Content in Web Content's Template....??
July 12, 2012 4:12 AM
Answer

DarshanKumar N Bhatia

Rank: Junior Member

Posts: 85

Join Date: March 2, 2010

Recent Posts

Hi ,
Liferay Expert ...
My Use case is :

1) Go to Control Panel --> custom Filed --> web content
Add custom Field : Name : MyData Type : Text

2) Now I want to create a new Web Content Template ...
In this template ,
I need value of custom Field using some velocity variable logic

Please help, any Idea ,,,,How to get Custom Fields of web content in Velocity Templete ???

Thanks in Advance ...
Subhasis Roy
RE: How to get Custom Field of Web Content in Web Content's Template....??
July 12, 2012 5:07 AM
Answer

Subhasis Roy

Rank: Regular Member

Posts: 234

Join Date: January 19, 2012

Recent Posts

Hi,

you have to put your custom variable in "WebKeys.VM_VARIABLES" to make it available in velocity template.

you can try following steps-

supposed you want to check the last login time in your theme velocity template.

1. write a simple java project which will return you your desired result(lastlogin time).
2. now set the result in "WebKeys.VM_VARIABLES" .

example snippet:

class MyLoginTime extends Action{
public void run(HttpServletRequest request, HttpServletResponse response)
throws ActionException {

Map result= new HashMap();
//code to get last login time
key = "my_logintime ";
result.put(key, logintimeValue); //logintime is your desired result
}
if(!result.isEmpty())
{
request.setAttribute(WebKeys.VM_VARIABLES, result);
}
}


3. now make a jar of the class and put it under liferay\tomcat\webapps\ROOT\WEB-INF
4. you should be able to access your custom variable in theme using $my_logintime

hope this gonna work for you
DarshanKumar N Bhatia
RE: How to get Custom Field of Web Content in Web Content's Template....??
July 12, 2012 5:15 AM
Answer

DarshanKumar N Bhatia

Rank: Junior Member

Posts: 85

Join Date: March 2, 2010

Recent Posts

Hi...

I m looking for solution like something :

Please Check this : http://blogs.xtivia.com/home/-/blogs/19104

$menuID = $nav_item.getLayout().getExpandoBridge().getAttribute("menu-article-id")

Here they are accessing custom Field of page in portal_normal.vm file.
So I need ....something like this ???

I mean : Using Expando BRidge , How to get custom fields/attribute defined for Web content in Web COntent Template:

Any idea ...???
Sagar A Vyas
RE: How to get Custom Field of Web Content in Web Content's Template....??
September 14, 2013 11:55 AM
Answer

Sagar A Vyas

Rank: Liferay Master

Posts: 655

Join Date: April 17, 2009

Recent Posts

Hi Darshan,

You can service class in your template,

Expando service you can use as below.

E.x
#set($layoutLocalServiceUtil = $serviceLocator.findService("com.liferay.portal.service.LayoutLocalService"))

Thanks,
Sagar Vyas
Hi I am Liferay
Judith Sambs
RE: How to get Custom Field of Web Content in Web Content's Template....??
December 4, 2012 7:03 AM
Answer

Judith Sambs

Rank: New Member

Posts: 13

Join Date: December 3, 2012

Recent Posts

A litte late to contribute to this discussion, however I am still bothered by this problem.

In my navigation I would like to teaser the subpages with image and text (not just the page name). I am an absolute liferay beginner and don't know how to achieve that. First I wanted to add 2 fields to the structure used by the web content in those subpages that hold the teaser image and text. However I an no idea how to access that information from within the theme (portal_normal.vm) through the NavItem references?
What about custom fields? I won't ask now how they work and what they are for, but could I access their content from within the theme through the NavItem references?

I am grateful for any hint!

best regards, Judith
Simon Youngs
RE: How to get Custom Field of Web Content in Web Content's Template....??
August 19, 2013 3:23 AM
Answer

Simon Youngs

Rank: New Member

Posts: 21

Join Date: July 12, 2011

Recent Posts

DarshanKumar N Bhatia:
Hi ,
Liferay Expert ...
My Use case is :

1) Go to Control Panel --> custom Filed --> web content
Add custom Field : Name : MyData Type : Text

2) Now I want to create a new Web Content Template ...
In this template ,
I need value of custom Field using some velocity variable logic

Please help, any Idea ,,,,How to get Custom Fields of web content in Velocity Templete ???

Thanks in Advance ...


Hi,

To help people along I have also been trying to achieve this outcome. It can be done in Liferay 6.1 in the following way.

First you need to make a change to your protal.properties file
1
2journal.template.velocity.restricted.variables=


This will remove the restriction on the $serviceLocator.findService function.
http://www.liferay.com/community/wiki/-/wiki/Main/Access+Liferay+Services+in+Velocity?_36_version=1.6

There are issues to think about as this makes all liferay services available through velocity web templates which your users may have permissions to create.

In my example I have two custom fields a event start date and event end date, I also have used the dateTool to format the dates retrieved from the custom fields.

Here is the velocity code
 1
 2#set ($journalArticleLocalService = $serviceLocator.findService("com.liferay.portlet.journal.service.JournalArticleLocalService"))
 3#set ($ja = $journalArticleLocalService.getArticle($getterUtil.getLong($groupId),$getterUtil.getString($reserved-article-id.data)))
 4#set ($resourceprimKey = $ja.getResourcePrimKey())
 5
 6#set ($assetEntryLocalService = $serviceLocator.findService("com.liferay.portlet.asset.service.AssetEntryLocalService"))
 7#set ($assetEntry = $assetEntryLocalService.getEntry("com.liferay.portlet.journal.model.JournalArticle", $resourceprimKey))
 8
 9#set($JournalArticleResourceLocalService = $serviceLocator.findService("com.liferay.portlet.journal.service.JournalArticleResourceLocalService"))
10
11#set ($journalArticleResource = $JournalArticleResourceLocalService.getArticleResource($assetEntry.getClassPK()))
12
13#set($JournalArticleLocalService = $serviceLocator.findService("com.liferay.portlet.journal.service.JournalArticleLocalService"))
14
15#set ($journalArticle = $JournalArticleLocalService.getArticle($getterUtil.getLong($groupId), "$journalArticleResource.getArticleId()"))
16
17#set ($startDate = $journalArticle.getExpandoBridge().getAttribute("Event 1 Start Date"))
18#set ($endDate = $journalArticle.getExpandoBridge().getAttribute("Event 2 End Date"))
19
20<h1>Start Raw date : - $startDate </h1>
21<h2>Formated date : - $dateTool.format('dd MMMM yyyy',$startDate)</h2>
22<h2>Formated date : - $dateTool.format('HH:mm dd MMMM yyyy',$startDate)</h2>
23
24<h1> End Raw Date : - $endDate </h1>
25<h2>Formated date : - $dateTool.format('dd MMMM yyyy',$endDate)</h2>
26<h2>Formated date : - $dateTool.format('HH:mm dd MMMM yyyy',$endDate)</h2>

I hope this saves someone the day I have spent trying to find out how to do this.

Regards

Simon
Simon Youngs
RE: How to get Custom Field of Web Content in Web Content's Template....??
August 30, 2013 3:45 AM
Answer

Simon Youngs

Rank: New Member

Posts: 21

Join Date: July 12, 2011

Recent Posts

A Second method to do this if you don't want to enable ServiceLocator for webcontent. Create a custom velocity util in a liferay EXT and add the following functions.


 1
 2
 3/**
 4 * @author Simon Youngs
 5 * @version 1.0
 6 * @since 2013-08-30
 7 *
 8 * Velocity util to return the custom field for web content without enabling ServiceLocator for web content.
 9 *
10 * Use the following in your velocity for it to work:- 
11 * $ueaVelocityUtils.getJournalCustomField($getterUtil.getLong($groupId),$getterUtil.getString($reserved-article-id.data),'Event 1 Start Date'))
12 *
13 * @param groupId         Group Id
14 * @param articleId        Article Id
15 * @param customField    Name of the custom field you want to retrieve
16 * @return                String value of the content of the custom field
17 */
18    public static String getJournalCustomField(Long groupId, String articleId, String customField)
19    {
20       
21        AssetEntry assetEntry = null;
22        JournalArticleResource journalArticleResource = null;
23        JournalArticle journalArticle = null;
24       
25        String expandoText = "";
26       
27        try {
28            JournalArticle ja = JournalArticleLocalServiceUtil.getArticle(groupId,articleId);
29            long resourceprimKey = ja.getResourcePrimKey();
30            assetEntry = AssetEntryLocalServiceUtil.getEntry("com.liferay.portlet.journal.model.JournalArticle", resourceprimKey);
31            journalArticleResource = JournalArticleResourceLocalServiceUtil.getArticleResource(assetEntry.getClassPK());
32            journalArticle = JournalArticleLocalServiceUtil.getArticle(assetEntry.getGroupId(), journalArticleResource.getArticleId());
33
34        } catch (PortalException e2) {
35            // TODO Auto-generated catch block
36            e2.printStackTrace();
37        } catch (SystemException e2) {
38            // TODO Auto-generated catch block
39            e2.printStackTrace();
40        }
41   
42        if (Validator.isNotNull(journalArticle.getExpandoBridge().getAttribute(customField))){
43            expandoText = journalArticle.getExpandoBridge().getAttribute(customField).toString();
44        }
45       
46        return expandoText;
47    }
48   
49    /**
50     * @author Simon Youngs
51     * @version 1.0
52     * @since 2013-08-30
53     *
54     * Velocity util to return the custom field date formated for web content without enabling ServiceLocator for web content.
55     *
56     * Use the following in your velocity for it to work:- 
57     * $ueaVelocityUtils.getJournalCustomFieldDateFormat($getterUtil.getLong($groupId),$getterUtil.getString($reserved-article-id.data),'Event 1 Start Date','dd MMMM yyyy'))
58     *
59     * @param groupId         Group Id
60     * @param articleId        Article Id
61     * @param customField    Name of the custom field you want to retrieve
62     * @param dateFormate     String format of how you want the date returned
63     * @return                String value of the content of the date custom field formated
64     */
65    public static String getJournalCustomFieldDateFormat(Long groupId, String articleId, String customField, String dateFormat)
66    {
67       
68        AssetEntry assetEntry = null;
69        JournalArticleResource journalArticleResource = null;
70        JournalArticle journalArticle = null;
71       
72        String expandoText = "";
73       
74        try {
75            JournalArticle ja = JournalArticleLocalServiceUtil.getArticle(groupId,articleId);
76            long resourceprimKey = ja.getResourcePrimKey();
77            assetEntry = AssetEntryLocalServiceUtil.getEntry("com.liferay.portlet.journal.model.JournalArticle", resourceprimKey);
78            journalArticleResource = JournalArticleResourceLocalServiceUtil.getArticleResource(assetEntry.getClassPK());
79            journalArticle = JournalArticleLocalServiceUtil.getArticle(assetEntry.getGroupId(), journalArticleResource.getArticleId());
80
81        } catch (PortalException e2) {
82            // TODO Auto-generated catch block
83            e2.printStackTrace();
84        } catch (SystemException e2) {
85            // TODO Auto-generated catch block
86            e2.printStackTrace();
87        }
88       
89           if (Validator.isNotNull(journalArticle.getExpandoBridge().getAttribute(customField))){
90               
91               expandoText = new SimpleDateFormat(dateFormat).format(journalArticle.getExpandoBridge().getAttribute(customField)).toString();
92            
93           }
94      
95        return expandoText;
96    }
97