Disable borders in Web Content Display via template - liferay

I'm creating a set of custom structures (and matching templates) for Web Content Displays on a Liferay site.
To make things more convenient for site maintainers, I would like one of these templates to suppress look-and-feel borders by default.
I've found documentation and samples online showing how to disable borders for portlets that are embedded in the theme, but I haven't had any luck applying those techniques inside a WCD template.
Here's the code I've seen embedded in a theme:
#set ($VOID = $velocityPortletPreferences.setValue("portlet-setup-show-borders", "false"))
#set ($VOID = $theme.runtime("customportletname_WAR_pluginname", "", $velocityPortletPreferences.toString()))
$velocityPortletPreferences.reset()
Here is how I've applied it to my template:
#set ($VOID = $velocityPortletPreferences.setValue("portlet-setup-show-borders", "false"))
<div class="custom-wcd wcdnormal">
<h2>$title.getData()</h2>
$content.getData()
</div>
$velocityPortletPreferences.reset()
I'm not surprised that it didn't work -- there's nothing analogous to the $theme.runtime call that applies the prefs to a portlet, but I also don't know whether $theme.runtime is appropriate at this layer, and if it is, what value to supply as the first argument. I suspect this last is the most likely alternative, but I don't know how to identify the WCD portlet to $theme.runtime, nor do I know how to track that information down (I'm not a Java dev by any stretch).
And just to clarify: it is not acceptable to disable borders on all portlets by default, nor am I in a position to modify the Java controller that consumes journal-article components at render-time.
Any advice is appreciated.

Simple and direct:
$velocityPortletPreferences.setValue("portletSetupShowBorders", "false")
$velocityPortletPreferences.setValue("languageIds", "pt_BR,en_US")
$velocityPortletPreferences.setValue("displayStyle", "1")
$theme.runtime("82", "", $velocityPortletPreferences.toString())
$velocityPortletPreferences.reset()
This example is for embed the language portlet (Portlet ID = 82), direct in one of .vm theme files, in my case this code is on navigation.vm.

Liferay property/preference names have a tendency to change sometimes. I created a hook this week to create a site, populate it with pages containing portlets and also to provision the CMS with a default set of structures, templates and articles. I too had to turn off the borders for my portlets and needed to set the following preference name to false to achieve that: portletSetupShowBorders
I java code it did this as follows:
PortletPreferences prefs = PortletPreferencesFactoryUtil.getLayoutPortletSetup(page, portletInstanceId);
prefs.setValue("groupId", String.valueOf(page.getGroupId()));
prefs.setValue("articleId", article.name());
prefs.setValue("portletSetupShowBorders", "false");
prefs.store();
From a Liferay Journal Template you should be able to use the following code to turn of the borders of the portlet that will show an article that uses the template:
#set ($portletPreferencesService = $serviceLocator.findService('com.liferay.portal.service.PortletPreferencesLocalService'))
#set ($companyId = $getterUtil.getLong($companyId))
#set ($ownerId = 0) ## PortletKeys.PREFS_OWNER_ID_DEFAULT
#set ($ownerType = 3) ## PortletKeys.PREFS_OWNER_TYPE_LAYOUT
#set ($plid = $getterUtil.getLong($request.theme-display.plid))
#set ($portletId = $request.theme-display.portlet-display.id)
#set ($portletPreferences = $portletPreferencesService.getPreferences($companyId, $ownerId, $ownerType, $plid, $portletId))
#set ($VOID = $portletPreferences.setValue('portlet-setup-show-borders', 'false'))
#set ($VOID = $portletPreferences.store())
Just remember that you also need to add the following line to your portal-ext.properties:
journal.template.velocity.restricted.variables=

Related

Liferay - get folder ID using folder name in velocity

I know how to get groupid in liferay velocity theme using:
#set ($scopeGroupId = $getterUtil.getLong($group_id))
However I also need to get a folder's ID using its name.
I have been researching for a while and don't seem to find a way to do that.
Q: Is this enabled and feasible in liferay 6.2?
Make sure that you are allowed to use serviceLocator or add in your portal-ext.properties this entry:
velocity.engine.restricted.variables=
After that you can use this code in your template:
#set ($folderLocalService =$serviceLocator.findService("com.liferay.portlet.documentlibrary.service.DLFolderLocalService"))
#set ($folderId = $folderLocalService.getFolder($groupId,0,"folder_name").getFolderId())
$folderId
Or you can use another method from DLFolderLocalService.

Liferay Custom field from web content in velocity template

I need to set some custom tag in portal_normal.vm which define in each Web Content.
My case:
Go to Control Panel --> custom Field --> web content
Create a new custom field called "custom_metas"
Put this code in portal_normal.vm <meta property="og:title" content="$themeDisplay.getScopeGroup().getExpandoBridge().getAttribute('custom_metas')" />
This code only works if custom field is created in site (not in web content). When I create in web content show as plain text.
In my portal-ext.properties put:
journal.template.velocity.restricted.variables=
Nothing change.
Finally I try this solution given in liferay forums but doesn't work. Notice: $reserved-article-id.data doesn't print nothing.
#set ($journalArticleLocalService = $serviceLocator.findService("com.liferay.portlet.journal.service.JournalArticleLocalService"))
#set ($ja = $journalArticleLocalService.getArticle($getterUtil.getLong($groupId),$getterUtil.getString($reserved-article-id.data)))
#set ($resourceprimKey = $ja.getResourcePrimKey())
#set ($assetEntryLocalService = $serviceLocator.findService("com.liferay.portlet.asset.service.AssetEntryLocalService"))
#set ($assetEntry = $assetEntryLocalService.getEntry("com.liferay.portlet.journal.model.JournalArticle", $resourceprimKey))
#set($JournalArticleResourceLocalService = $serviceLocator.findService("com.liferay.portlet.journal.service.JournalArticleResourceLocalService"))
#set ($journalArticleResource = $JournalArticleResourceLocalService.getArticleResource($assetEntry.getClassPK()))
#set($JournalArticleLocalService = $serviceLocator.findService("com.liferay.portlet.journal.service.JournalArticleLocalService"))
#set ($journalArticle = $JournalArticleLocalService.getArticle($getterUtil.getLong($groupId), "$journalArticleResource.getArticleId()"))
#set ($custom = $journalArticle.getExpandoBridge().getAttribute("custom_metas"))
$custom display as plain text
You are confusing theme templates with web content templates. In a theme template you have neither a single attached web content article, nor do you have access to any web content specific variable like $reserved-article-id.
And you mix up pages and portlets. Your web content article is attached to a portlet on your page - and as you can have more than one portlet with a web content article on a page, you don't have the article that is loaded.
But if you attach a tag to your webcontent (not a custom field - just a tag in the Categorization section), that tag will be added to <meta name="keywords"> in the head automatically.
If you need the tags somewhere else - they are stored in the request attribute WebKeys.PAGE_KEYWORDS (= "LIFERAY_SHARED_PAGE_KEYWORDS").

Change language of web content in liferay

I would like to change language of web content in liferay. So I tried to change language programatically from en_EN to sk_SK (Slovak) this way:
$themeDisplay.getLanguageId() // there it is en_EN
$themeDisplay.setLanguageId('sk_SK')
$themeDisplay.getLanguageId() // there it is sk_SK
same it is with this code
#set ($locale=$localeUtil.fromLanguageId("sk_SK"))
$themeDisplay.setLocale($locale)
$themeDisplay.getLocale()
But the web content didn't change. Do you know why?
If this is not clear I will try to give you an example: On the top of web-page are usually some language buttons, so you can switch between more languages. I want to do same in liferay. So I have one WebContent with more translations and with this buttons I want to change language/translation of webContent.
On the top of web-page are usually some language buttons, so you can
switch between more languages. I want to do same in liferay. So I have
one WebContent with more translations and with this buttons I want to
change language/translation of webContent.
If i'm not wrong Liferay already provide such functionality out-of-the-box. You don't need to do it with pragmatically.
Use Liferay's navigation portlet to switch the languages.
Thank you, but I would like to add it into my code, into *.vm file. So this is my solution (finally I found it)
<div id="language-portlet">
#set ($VOID = $velocityPortletPreferences.setValue('portlet-setup-show-borders', 'true'))
#set ($VOID = $theme.runtime("82", '', $velocityPortletPreferences.toString()))
#set ($VOID = $velocityPortletPreferences.reset())
</div>
It show possible translations on your web-page. (For me it is flags)
The best solution is to override this property
locale.default.request=false
company.default.locale=sk_SK
is portal-ext.proprties file

How to get the page title in a Liferay velocity template

How can I get the title of current page in a CMS velocity template ?
I need the same String as is shown in the last part of the breadcrump, in other words, the page title.
Finally I found out how to do it.
It's necessary to access through the $themeDisplay
##Take layout id
#set ($layoutId = $request.get("theme-display").get("plid"))
## get the service for layout
#set($layoutService = $serviceLocator.findService("com.liferay.portal.service.LayoutLocalService"))
##convert the layout id into long
#set ($layoutLong = $getterUtil.getLong($layoutId))
##take a layout object
#set($layout = $layoutService.getLayout($layoutLong))
#set ($pageName = $layout.getName($locale))
That's it.
In a theme template: $page.getHTMLTitle($locale)
From a CMS template I'll have to dig a bit deeper for the answer... let me know if the theme is sufficient
In addition with larrytron code, It's usefull to declare this line in portal-ext.properties to use restricted variables in CMS Template
journal.template.velocity.restricted.variables=

How to embed WebContent in Liferay

Is that it is possible embed web content in a template velocity?
I have two web contents and I want to unite the two into a single one.
I tried this:
#set ($webcontent-id = "13054")
#set ($webcontent=$journalContentUtil.getContent($group_id, $webcontent-id,null,"$locale",$theme_display))
<div> $webcontent </div>
#set ($webcontent-id = "13065")
#set ($webcontent=$journalContentUtil.getContent($group_id, $webcontent-id,null,"$locale",$theme_display))
<div> $webcontent </div>
However, it displays the variable. I think I did not access the service in a webcontent.
Web content templates do not have access to the ThemeDisplay directly. They can access request information via the $request map, which contains among other things variables from ThemeDisplay. This wiki page lists the variables that can be used from templates.
Also, when calling Liferay services from velocity templates you need to make sure that all arguments have the correct type. You can use $getterUtil to accomplish this, for example to convert a String to Long.
Here's a revised version of your example:
#set ($group_id = $getterUtil.getLong($request.theme-display.scope-group-id))
#set ($webcontent-id = "58007")
#set ($webcontent=$journalContentUtil.getContent($group_id, $webcontent-id, "", "$locale", ""))
$webcontent

Resources