掲示板

Refer to webcontent in a webcontent template in Liferay 7

7年前 に André Peric Tavares によって更新されました。

Refer to webcontent in a webcontent template in Liferay 7

New Member 投稿: 4 参加年月日: 16/09/23 最新の投稿
Hi.

Suppose there's a webcontent A whose structure has a webcontent field. This field links it to a webcontent B. How can I get B's fields in A's template? I know I can use getData() in the template to get the variable that represents the webcontent. But after that, how do I refer to one of its fields?

Alternatively, I could use appropriate service to get the article and its fields. This is my attempt:

<#assign configGeraisArticleId = 92574 />
<#assign journalService = serviceLocator.findService("com.liferay.journal.service.JournalArticleService") />
<#assign article = journalService.getArticle(configGeraisArticleId)/>


It doesn't work. The webcontent doesn't even get rendered when I include the third line.


Thanks.
thumbnail
7年前 に Andrew Jardine によって更新されました。

RE: Refer to webcontent in a webcontent template in Liferay 7

Liferay Legend 投稿: 2416 参加年月日: 10/12/22 最新の投稿
Hi Andre,

I'm assuming that your template code is much longer than the 3 lines you posted here -- I just want to make sure I understand -- are you saying that line 3 executes, but that your "article" variable has nothing assigned to it?
7年前 に André Peric Tavares によって更新されました。

RE: Refer to webcontent in a webcontent template in Liferay 7

New Member 投稿: 4 参加年月日: 16/09/23 最新の投稿
Hi Andrew.

It's working now emoticon

Here's the relevant excerpt:


<#assign configurationArticleId = "92574">
<#assign JournalArticleService = serviceLocator.findService("com.liferay.journal.service.JournalArticleService")>
<#assign article = JournalArticleService.getArticle(groupId, configurationArticleId)>


    <#assign document = saxReaderUtil.read(article.getContentByLocale(locale))>
    <#assign rootElement = document.getRootElement()>
        <#list rootElement.elements() as dynamicElement>
            <#if dynamicElement.attributeValue("name") =="grouid">
                <#assign grouid = dynamicElement.element( 'dynamic-content').getText()>
            <!--#if-->
            &lt;#if dynamicElement.attributeValue("name") =="parentfolderid"&gt;
                &lt;#assign parentfolderid = dynamicElement.element( 'dynamic-content').getText()&gt;
            <!--#if-->
            &lt;#if dynamicElement.attributeValue("name") =="folderpathdesktop"&gt;
                &lt;#assign folderpathdesktop = dynamicElement.element( 'dynamic-content').getText()&gt;
            <!--#if-->
            &lt;#if dynamicElement.attributeValue("name") =="folderpathmobile"&gt;
                &lt;#assign folderpathmobile = dynamicElement.element( 'dynamic-content').getText()&gt;
            <!--#if-->
        <!--#list-->

etc


Before that the webcontent would vanish from the page if I included the third line. It would show up again if I included only the first two lines.

I'll create a text field for the hardcoded ID.
Accessing the webcontent fields through a webcontent field would be nicer, though (I don't really know if that's possible).

Thanks!
thumbnail
7年前 に Andrew Jardine によって更新されました。

RE: Refer to webcontent in a webcontent template in Liferay 7

Liferay Legend 投稿: 2416 参加年月日: 10/12/22 最新の投稿
Personally, I hate using the SAXReader as a solution. It just feels backwards to me because you are basically circumventing the templating engine. I realize that sometimes you don't have a choice .. but if the goal is just to render the second content in a certain way, I would also consider trying this...

1. Create a WCM template for Web Content B that matches what you want it to look like when it is embedded in Web Content A. In this template of course you have the fields available to you, so you don't need to use the SAXReader.

2. In your template for Web Content A (where you want to embed the Web Content emoticon, so are you have and retrieve the Journal Article. But then, instead of parsing it, use another method on the JournalArticleServer to get the content (HTML) resulting from the article + template.

JournalArticleLocalServiceUtil.getArticleContent(article, ddmTemplate.getTemplateKey(), "", themeDisplay.getLanguageId(), themeDisplay)


.. of course you need to know your template, but you can use the DDMTemplateLocalService to look it up and plug it in that way. I use this technique all the time now and I much prefer it to manually parsing jorunal articles to get data fields. Truthfully, I have not yet tried this in LR7 -- but I know for sure it works in 6.2 and I doubt that API has change between the two versions.

Just some food for thought emoticon
7年前 に André Peric Tavares によって更新されました。

RE: Refer to webcontent in a webcontent template in Liferay 7

New Member 投稿: 4 参加年月日: 16/09/23 最新の投稿
Hi Andrew.

You are right - this is a very elegant way of embedding a webcontent.

In my case, I just needed the value from some fields for using them as parameters of a function - they won't get displayed, actually. By the way, the webcontent that has those fields is just for this kind of configuration - it's not shown anywhere in the site and its template is blank. Perhaps there's a better pattern for that.

Thanks.
thumbnail
7年前 に Andrew Jardine によって更新されました。

RE: Refer to webcontent in a webcontent template in Liferay 7

Liferay Legend 投稿: 2416 参加年月日: 10/12/22 最新の投稿
I think if we went down this path we'd leave the context of help and enter into a more philosophical discussion about best practices and how Web Content should be used. I don't know about you, but my time these days is stretched pretty thin so I don't think I want to go down that road.

Let's just say that in the real world? sometimes you gotta do what you gotta do and leave it at that emoticon.

Glad your problem was solved -- thanks for sharing with the community.