How to get Liferay freeMarkerPortletPreferences in nested Webcontent templates? - liferay

I am trying to use portletpreference in my nested webcontent in Liferay 7.4. In the 7.4 version, there are two tables [PortletPreferences] and [PortletPreferenceValue] for Preferences.
I am able to set the Preferences values but, having trouble in retrieving it in the template.
I have tried below code in my template for retrieving the preferences. I am not able to find any method which retrieves PortletPreferenceValues.
<#assign portletPreferencesService = serviceLocator.findService("com.liferay.portal.kernel.service.PortletPreferencesLocalService") />
<#assign portletPreferncesValueService = serviceLocator.findService("com.liferay.portal.kernel.service.PortletPreferenceValueLocalService") />
<#assign portletKeys = staticUtil["com.liferay.portal.kernel.util.PortletKeys"]>
<#assign ownerId = groupId />
<#assign ownerType = portletKeys.PREFS_OWNER_TYPE_LAYOUT />
<#assign portletId = "com_liferay_journal_content_web_portlet_JournalContentPortlet" />
<#assign plid = 0/>
<#assign portletPreferences = portletPreferencesService.fetchPortletPreferences(ownerId, ownerType, plid, portletId) />

Related

How to embed a portlet in Asset Publisher?

I want to embed a portlet in an Asset Publisher in Liferay 6.2.
I did this:
<#assign siteMapPortletId = "85" />
<#assign PortletPreferencesFactoryUtil = staticUtil["com.liferay.portlet.PortletPreferencesFactoryUtil"] />
<#assign portletSetupFooter = PortletPreferencesFactoryUtil.getLayoutPortletSetup(layout, siteMapPortletId) />
<#if portletSetupFooter.getValue("portletSetupShowBorders", "") != "false">
<#assign myPreferences = portletSetupFooter.setValue("portletSetupShowBorders", "false") />
</#if>
<#assign myPreferences = portletSetupFooter.setValue("rootLayoutUuid", getPageID("Pages")) />
<#assign myPreferences = portletSetupFooter.setValue("displayStyle", getTemplateID("Sitemap")) />
<#assign myPreferences = portletSetupFooter.store() />
<#assign liferay_portlet = taglibLiferayHash["/WEB-INF/tld/liferay-portlet.tld"] />
<#liferay_portlet["runtime"]
defaultPreferences=myPreferences
portletName=siteMapPortletId
/>
but I get this error:
liferay_portlet["runtime"] not found.

How can I get repeatable field values of a Document Type (WCM) via Application Display Template (ADT)?

I have this Document Type Structure:
I can get the value of the fields that do not have a children (in this example the field with the name Field):
<#if imageMimeTypes?seq_contains(entry.getMimeType()) >
<#assign fileEntryType = DLFileEntryTypeService.getFileEntryType(fileEntryTypeId) />
<#assign dlFileVersion = DLFileVersionService.getLatestFileVersion(fileEntry.getUserId(), fileEntry.getFileEntryId()) />
<#assign fieldsMap = fileEntry.getFieldsMap(dlFileVersion.getFileVersionId()) />
<#list fieldsMap?keys as structureKey>
<#list fieldsMap[structureKey].iterator() as field>
<#if field.getName() == 'Field'>
<#assign Field = field.getValue() />
</#if>
</#list>
</#list>
Value: ${Field}
</#if>
But when I have a repeatable field with children fields I can't get neither the value of the repeatable field nor the value of its children.
<#list fieldsMap?keys as structureKey>
<#list fieldsMap[structureKey].iterator() as field>
<#if field.getName() == 'Repeatable_field'>
<#assign RepeatableField = field.getName() />
</#if>
</#list>
</#list>
Value: ${RepeatableField}
I get this error:
Expecting a string, date or number here, Expression RepeatableField
is instead a freemarker.template.SimpleSequence
UPDATE:
I managed to get the value of Repeatable_field using field.getValues(locale) method.
But I still can't get the value of its children:
<#list fieldsMap?keys as structureKey>
<#list fieldsMap[structureKey].iterator() as field>
<#if field.getName() == 'Repeatable_field'>
<#assign repeatableFieldValues = field.getValues(locale) />
<#list repeatableFieldValues as val>
<#assign Field = val /><#-- parent Value -->
<#-- assign childrenField = val.Children_field.getValue() --><#-- children Value -->
</#list>
</#if>
</#list>
</#list>
In Liferay 7.3, I get the repeatable field values within the ADT in this way:
<#if entries?has_content>
<#list entries as entry>
<#if entry.getClassName() == "com.liferay.journal.model.JournalArticle" >
<#assign assetRenderer = entry.getAssetRenderer() />
<#assign article = assetRenderer.getArticle() />
<#assign articleStructure = article.getDDMStructure() />
<#assign articleStructureFields = articleStructure.getRootFieldNames() />
<#assign document = saxReaderUtil.read(article.getContent()) />
<#assign rootElement = document.getRootElement() />
<#list articleStructureFields as field>
<#attempt>
<#assign xPathSelectorDocument = saxReaderUtil.createXPath("dynamic-element[#name='${field}']") />
<#assign fieldContent = xPathSelectorDocument.selectSingleNode(rootElement).getStringValue() />
<#assign fieldNodes = xPathSelectorDocument.selectNodes(rootElement) />
<#if field == 'User'> <#-- Field - Simple -->
<#assign user = fieldContent />
<#elseif field == 'Actions'> <#-- Field - Repeatable -->
<#assign listActions = [] />
<#list fieldNodes as node>
<#assign listActions = listActions + [node.getStringValue()] />
</#list>
</#if>
<#recover>Error</#attempt>
</#list>
<#-- Field - Simple -->
${user}
<#-- Field - Repeatable -->
<#list listActions as action>
${action}
</#list>
</#if>
</#list>
</#if>

Can I get a list of templates of a Liferay Site?

I want to get a list of all templates (JournalArticle templates in VM or FTL) of a Liferay Site in a WCM Template or Theme template.
This is what I tried:
<#assign DDMTemplateLocalServiceUtil = objectUtil("com.liferay.portlet.dynamicdatamapping.service.DDMTemplateLocalServiceUtil") />
<#assign ClassNameLocalServiceUtil = objectUtil("com.liferay.portal.service.ClassNameLocalServiceUtil")/>
<#assign className = ClassNameLocalServiceUtil.getClassName("com.liferay.portlet.journal.model.JournalArticle") />
<#assign classNameId = className.getClassNameId() />
<#assign groupId = themeDisplay.getScopeGroupId() />
<#assign ddmTemplates = DDMTemplateLocalServiceUtil.getTemplates(groupId, classNameId) />
<#list ddmTemplates as template>
${template.getName()}
</#list>
But I don't get any value and no error.
When I try to get the name of a template based on its id, it works:
<#assign ddmTemplate = DDMTemplateLocalServiceUtil.getTemplate(50367) />
${ddmTemplates.getName()}
I don't know the templates id, so how can I get all templates of a Liferay Site without knowing their id?

How to open URL in maximized mode in Application Display Template?

I have created and ADT (Application Display Template) for Asset Publisher which displays web-contents. If a link is clicked the web-content should open in maximized mode.
<#if entries?has_content>
<#list entries as entry>
<#assign renderer = entry.getAssetRenderer() />
<#assign className = renderer.getClassName() />
<#if className == "com.liferay.portlet.journal.model.JournalArticle">
<#assign journalArticle = renderer.getArticle() />
<#assign document = saxReaderUtil.read(journalArticle.getContentByLocale(locale.toString())) />
<#assign assetURL = assetPublisherHelper.getAssetViewURL(renderRequest, renderResponse,entry) />
<div class="molisana-product">
<a href="${assetURL}">
<h3 class="product-title">
${entry.getTitle(locale)}
</h3>
</a>
</div>
</#if>
</#list>
</#if>
The important bit is :
<#assign assetURL = assetPublisherHelper.getAssetViewURL(renderRequest, renderResponse,entry) />
I tried with setWindowState <a href="${assetURL.setWindowState(WindowState.MAXIMIZED)}"> but doesn't work.

Liferay Freemarker - How to get tags in a Content Template

I have content with a structure and a template and I want to access the tags of the content to show it in the template.
In a previous Application Display Template of an Asset Publisher, i got the tags with the ServiceLocator like this:
<#list entries as entry>
<#assign assetEntryLocalService = serviceLocator.findService("com.liferay.portlet.asset.service.AssetEntryLocalService") />
<#assign assetTagLocalService = serviceLocator.findService("com.liferay.portlet.asset.service.AssetTagLocalService") />
<#assign assetTags = assetTagLocalService.getEntryTags(entry.getEntryId()) />
<#list assetTags as tag>
<#if tag.getName() != "startseite">
${tag.getName()}
</#if>
</#list>
</#list>
In my Template, I swapped entry with .vars['reserved-article-id'].data but then I get an error:
Method public final java.util.List com.sun.proxy.$Proxy562.getEntryTags(long) throws com.liferay.portal.kernel.exception.SystemException threw an exception when invoked on com.liferay.portlet.asset.service.impl.AssetTagLocalServiceImpl#6bc73e2b
How do you get this to work in a template?
Tags are associated with the assetEntry using it's resourcePrimKey, so you can do this:
<#assign assetEntryLocalService = serviceLocator.findService("com.liferay.portlet.asset.service.AssetEntryLocalService") />
<#assign journalArticleLocalService = serviceLocator.findService("com.liferay.portlet.journal.service.JournalArticleLocalService") />
<#assign article = journalArticleLocalService.getArticle(getterUtil.getLong(scopeGroupId), .vars['reserved-article-id'].data)>
<#assign asset = assetEntryLocalService.getEntry('com.liferay.portlet.journal.model.JournalArticle', article.resourcePrimKey) >
<#list asset.getTags() as tag>
<code>${tag.name}</code>
</#list>
As of Liferay 7, you should be able to use the following:
<#list entries as entry>
<#assign
entry = entry
/>
<#list entry.tagNames as tag>
${tag}
</#list>
</#list>

Resources