2
votes

I want to figure out how to make one thing in Liferay. I am using a lot of custom portlets and faced a problem. I want to have my custom portlet as a through element in my portal. For example custom "searchbar" in my portal header as a portlet. But the problem is that I need the same instance for it, because every time when I will surf new page, every time It will be a new instance. I'm right? I need something like singleton. How can I do it with liferay? And, of course, how can I make portlet as a through element?

1
I don't see a reason for the portlet needing to be "the same instance". There is none. Did you mean that you want the portlet to appear on every page without explicitly placing it there? Otherwise, please rephrase your question to give a few more clues about what you want to achieve (not how you want to achieve it)Olaf Kock
I don't see what he means with "through element" either. Maybe he is speaking of embedded portlets?Tobias Liefke
Thank you for answers! I am speaking about emdedded portlets (because of my terrible English sometimes I can't describe correctly what I mean). But when I will refresh page or wiil go to a new page, every time new copy of portlet will be created? @TobiasLiefkejahra

1 Answers

2
votes

Take a look at liferay-portlet.xml and properties

<preferences-company-wide>
<preferences-unique-per-layout>
<preferences-owned-by-group>
<instanceable>

Following description is taken from http://www.liferay.com/dtd/liferay-portlet-app_6_1_0.dtd

Element : preferences-company-wide
Set the preferences-company-wide value to true if the preferences for the portlet are across the entire company. Setting 
 this value to true means the value for preferences-unique-per-layout and preferences-owned-by-group are not used. 
 The default value is false. For example, an administrator could set the preferences to an Announcements portlet that 
 would save a message in the portlet's preferences. This message would then be used across all pages for that company. 
 The portlet must not be instanceable because instanceable portlets have uniquely generated portlet ids. The default 
 behavior of the bundled Announcements portlet sets the instanceable value to true so that normal users cannot create 
 company wide messages. A future release would include permissions for the edit mode versus the view mode which 
 would allow an administrator to set the message while users would just view the message.

Element : preferences-unique-per-layout
Set the preferences-unique-per-layout value to true if the preferences for the portlet are unique across all pages. If set 
 to false, the preferences for the portlet are shared across all pages. The default value is true. The preferences-unique-
 per-layout element is used in combination with the preferences-owned-by-group element. See the comments for the 
 preferences-owned-by-group element for more information.

Element : preferences-owned-by-group
Set the preferences-owned-by-group value to true if the preferences for the portlet are owned by the group when the 
 portlet is shown in a group page. If set to false, the preferences are owned by the user at all times. The default value is 
 true. Suppose the Stocks portlet has preferences-unique-per-layout set to true and preferences-owned-by-group set to 
 false. Users can set a different list of stocks for every personal page. Users can set a different list of stocks for every 
 community page. Suppose the Stocks portlet has preferences-unique-per-layout set to false and preferences-owned-by-
 group set to false. Users can set one list of stocks to be shared across all personal pages. Users can set one list of stocks 
 to be shared across a community's set of pages. Suppose the Stocks portlet has preferences-unique-per-layout set to 
 true and preferences-owned-by-group set to true. Users can set a different list of stocks for every personal page. 
 Administrators set the portlet preferences for users in a community page. Administrators can set a different list of stocks 
 for every community page that are then shared by all users within a community. Suppose the Stocks portlet has 
 preferences-unique-per-layout set to false and preferences-owned-by-group set to true. Users can set one list of stocks 
 to be shared across all personal pages. Administrators set the portlet preferences for users in a community page. 
 Administrators can set one list of stocks to be shared by all users across a community's set of pages.

Element : instanceable
Set the instanceable value to true if the portlet can appear multiple times on a page. If set to false, the portlet can only 
 appear once on a page. The default value is false.

For embeding portlet in a theme take a look at (assuming Velocity is used) class com.liferay.taglib.util.VelocityTaglib

com.liferay.taglib.util.VelocityTaglib.runtime(String portletName)
com.liferay.taglib.util.VelocityTaglib.runtime(String portletName, String queryString)
com.liferay.taglib.util.VelocityTaglib.runtime(String portletName, String queryString, String defaultPreferences)

Example usage in velocity theme template would be

$theme.runtime("myportlet")

EDIT (more info per comment):

if your portlet has, in liferay-portlet.xml this combination of properties

    <preferences-unique-per-layout>false</preferences-unique-per-layout>
    <preferences-owned-by-group>true</preferences-owned-by-group>
    <instanceable>false</instanceable>

it will be non instanceable and will have single set of preferences across all pages (preferences-unique-per-layout=false) in whole site (preferences-owned-by-group=true). You can include it in your theme, assuming war name is myportlets.war and portlet name is search, with

$theme.runtime("search_WAR_myportlets")

You can confugre preferences at any given page in your site.

If your portlet must be instanceable than you can even made up instance id

$theme.runtime("search_WAR_myportlets_INSTANCE_MYMADEUPINSTANCEID")

For both cases answer to "every time new copy of portlet will be created?" is no.

It would be best to try abowe method(s) and see for yourself what happens.